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

推荐订阅源

K
Kaspersky official blog
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
D
Docker
博客园 - 司徒正美
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
U
Unit 42
J
Java Code Geeks
A
About on SuperTechFans
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
H
Hacker News: Front Page
G
Google Developers Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
S
Secure Thoughts
GbyAI
GbyAI
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity 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文件的方法参考一下链接