
















2016-08-23 16:20 爱车龟速兔 阅读(421) 评论() 收藏 举报
1 在Spring中配置文件中, 配置配置文件的引用
<util:properties id="settings" location="/WEB-INF/conf/custom.properties"></util:properties>
2 实现一个ApplicationContextAware 的接口实现
public class SpringContextHolder implements ApplicationContextAware {private static final Logger logger = Logger.getLogger(SpringContextHolder.class);
private static ApplicationContext ctx=null;
public void setApplicationContext(ApplicationContext context) throws BeansException {
SpringContextHolder.ctx=context;
}private SpringContextHolder(){
}public static ApplicationContext getCtx(){
return ctx;
}public static Object getBean(String name){
return ctx.getBean(name);
}
}
加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的
public void setApplicationContext(ApplicationContext context) throws BeansException
方法,设置ApplicationContext对象。
前提必须在Spring配置文件中指定该类
3 在Spring 的配置文件中配置 ApplicationContextAware 的接口实现类的Bean
<bean id="springContextHolder" class="com.xxx.common.SpringContextHolder"/>
4 调用ApplicationContextAware 接口的实现类的getbean 方法, 转换成properties对象, 即可读取
Properties properties = (Properties) SpringContextHolder.getBean("settings");
Ref
其他读取properties文件的方法参考一下链接
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。