惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
Martin Fowler
Martin Fowler
B
Blog
The GitHub Blog
The GitHub Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
DataBreaches.Net
月光博客
月光博客
人人都是产品经理
人人都是产品经理
IT之家
IT之家
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog
C
Check Point Blog
罗磊的独立博客

博客园 - 爱车龟速兔

Kylin使用笔记-1: 安装 Kylin使用笔记-0: kylin介绍 jquery validate不用submit提交,用js提交的 NPOI 读取单元格的格式 一些列开源的验证组件 .NET的轻量级模板引擎 JNTemplate Aspose.OCR for .NET 高速集群监控系统 SuperMon c#获得HTTP header中location的方法 Redis on Windows jQuery 工具提示插件 DTooltip 轻量级jQuery网格插件——ParamQuery 矢量 开源JS仪表盘 JustGage C#的HTTP开发包 HttpLib Query 图像放大镜插件 Mlens sparky 一个趋势图的JavaScript插件 Windows SDK PGM相关文章翻译之三PGM Sender Options Windows SDK PGM相关文章翻译之一Reliable Multicast Programming
如何通过Spring读取Properties文件
爱车龟速兔 · 2016-08-23 · via 博客园 - 爱车龟速兔

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文件的方法参考一下链接