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

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks 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文件的方法参考一下链接