1. 程式人生 > >spring的context:property-placeholder的使用與優化

spring的context:property-placeholder的使用與優化

使用<context:property-placeholder>的原因,載入不到某個配置檔案,可以不報錯。

<context:property-placeholder 
                         location="classpath*:conf/token.properties,
                                  classpath*:conf/solr.properties,
                                  classpath*:conf/systempara.properties 
                                  ignore-resource-not-found="true"
                                  ignore-unresolvable="true"/>

location:表示屬性檔案位置,多個之間通過如逗號/分號等分隔;
file-encoding:檔案編碼;
ignore-resource-not-found:如果屬性檔案找不到,是否忽略,預設false,即不忽略,找不到將丟擲異常
ignore-unresolvable:是否忽略解析不到的屬性,如果不忽略,找不到將丟擲異常
properties-ref:本地java.util.Properties配置
local-override:是否本地覆蓋模式,即如果true,那麼properties-ref的屬性將覆蓋location載入的屬性
system-properties-mode:系統屬性模式,ENVIRONMENT(預設),NEVER,OVERRIDE
ENVIRONMENT:將使用Spring 3.1提供的PropertySourcesPlaceholderConfigurer,其他情況使用Spring 3.1之前的PropertyPlaceholderConfigurer
OVERRIDE: PropertyPlaceholderConfigurer使用,因為在spring 3.1之前版本是沒有Enviroment的,所以OVERRIDE是spring 3.1之前版本的Environment
NEVER:只查詢properties-ref、location;

優化:當配置檔案是基礎模組時候,controller獲取配置檔案就比較麻煩。可以在每個springmvc中新增配置,重新獲取。但如果多個模組的時候,就比較麻煩。

可通過獲取applicationContext物件,獲取並設定到基礎類 property類中。獲取配置:Property

通過Property的getValue,就能在多個web project中的controller中正確獲取到配置資訊。

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;


public class SpringContextUtil implements ApplicationContextAware {
	private static ApplicationContext applicationContext;
	private static final Logger logger = LogManager.getLogger(SpringContextUtil.class);
	// 實現
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringContextUtil.applicationContext = applicationContext;
		// 獲取配置載入類
		String[] beans = applicationContext.getBeanDefinitionNames();
		for (String beanName : beans) {
			Class<?> beanType = applicationContext.getType(beanName);
			if ("org.springframework.context.support.PropertySourcesPlaceholderConfigurer"
					.equals(beanType.getName())) {
				PropertySourcesPlaceholderConfigurer placeholderConfigurer = 
						(PropertySourcesPlaceholderConfigurer)applicationContext.getBean(beanName);
				Property.setPlaceholderConfigurer(placeholderConfigurer); 
				break;
			}
		}
		
		if(Property.getPlaceholderConfigurer() == null){
			logger.error("Property.getPlaceholderConfigurer() is null,but it can not be null");
		}
		
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public static <T> T getBean(Class<T> clazz) throws BeansException {
		return applicationContext.getBean(clazz);
	}

	public static Object getBean(String name) throws BeansException {
		try {
			return applicationContext.getBean(name);
		} catch (Exception e) {
			throw new RuntimeException("獲取的Bean不存在!");
		}
	}

	public static <T> T getBean(String name, Class<T> requiredType)
			throws BeansException {
		return applicationContext.getBean(name, requiredType);
	}

	public static boolean containsBean(String name) {
		return applicationContext.containsBean(name);
	}

	public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
		return applicationContext.isSingleton(name);
	}

	public static Class<? extends Object> getType(String name) throws NoSuchBeanDefinitionException {
		return applicationContext.getType(name);
	}

	public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
		return applicationContext.getAliases(name);
	}


}
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

public class Property {
	
	private static PropertySourcesPlaceholderConfigurer placeholderConfigurer ;
	
	public static String getValue(String key){
		if(key==null||"".equals(key.trim()) ||placeholderConfigurer == null){
			return "";
		}		
		
		String value = (String) 
				placeholderConfigurer.getAppliedPropertySources().get("localProperties").getProperty(key);
		
	
		value=value==null?"":value;
		return value;
	}


	public static void setPlaceholderConfigurer(
			PropertySourcesPlaceholderConfigurer placeholderConfigurer) {
		Property.placeholderConfigurer = placeholderConfigurer;
	}

	public static PropertySourcesPlaceholderConfigurer getPlaceholderConfigurer() {
		return placeholderConfigurer;
	}

}