使用<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;
}
}