Java中如何获取spring中配置文件.properties中属性值

摘要:
通过spring配置properties文件123456789

通过spring配置properties文件

1
2
3
4
5
6
7
8
9
<bean id="propertyConfigurer"
class="com.hapishop.util.ProjectDBinfoConfigurer">
<property name="ignoreResourceNotFound"value="true"/>
<property name="locations">
<list>
<value>/WEB-INF/config/dbinfo.properties</value>
</list>
</property>
</bean>

其中class为自己定义的类

自定义类ProjectDBinfoConfigurer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Properties;
importorg.springframework.beans.BeansException;
importorg.springframework.beans.factory.config.ConfigurableListableBeanFactory;
importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**
* 自定义ProjectDBinfoConfigurer返回properties内容
* @ClassName: ProjectDBinfoConfigurer
* @Description: TODO (请用一句话描述该类做什么)
* @author ZhangYQ iitshare@itblood.com
* @date: 2012-11-20 下午11:48:32
*/
publicclassProjectDBinfoConfigurer extendsPropertyPlaceholderConfigurer {
privatestaticMap ctxPropertiesMap;
@Override
protectedvoidprocessProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throwsBeansException {
super.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap = newHashMap();
for(Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}
publicstaticObject getContextProperty(String name) {
returnctxPropertiesMap.get(name);
}
}

这样就可以通过ProjectDBinfoConfigurer类来获取properties属性文件中的内容了

如何获取属性文件的内容

String url= (String) ProjectDBinfoConfigurer.getContextProperty(“jdbc.url”);

摘自 : http://blog.itblood.com/java-to-get-the-value-of-the-spring-configuration-file.html

免责声明:文章转载自《Java中如何获取spring中配置文件.properties中属性值》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇shell脚本启动java程序ffmpeg简介下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

iOS Document Interaction(预览和打开文档) 编程指南

原文:http://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Introduction/Introduction.html   关于 DocumentInteraction   iOS支持...

zip4j实现多线程压缩

使用的jar包:zip4j_1.3.2.jar 基本功能: 针对ZIP压缩文件创建、添加、分卷、更新和移除文件 (读写有密码保护的Zip文件) (支持AES 128/256算法加密) (支持标准Zip算法加密) (支持zip64格式) (支持Store(仅打包,默认不压缩,不过可以手动设置大小)和Deflate压缩方法 (针对分块zip文件...

AndroidManifest.xml配置文件详解 (转)

AndroidManifest.xml配置文件对于Android应用开发来说是非常重要的基础知识,本文旨在总结该配置文件中重点的用法,以便日后查阅。下面是一个标准的AndroidManifest.xml文件样例。     [html] view plaincopy   <?xml version="1.0" encoding="utf-8"?>...

使用ShareSDK完成Facebook第三方登录和Facebook分享时没办法跳转到Facebook应用

楼主是通过cocoapod接入ShareSDK, 后来发现无论是使用fb分享还是登录, 都是跳出了网页认证(即使我的手机有安装了fb) 后来mob的技术客服小哥告诉我在构造分享参数的时候, 执行参数字典的SSDKEnableUseClientShare方法, 也就是 NSMutableDictionary *shareParams =[NSMutableD...

Android中Parcel的分析以及使用

简单点来说:Parcel就是一个存放读取数据的容器, Android系统中的binder进程间通信(IPC)就使用了Parcel类来进行客户端与服务端数据的交互,而且AIDL的数据也是通过Parcel来交互的。在Java空间和C++都实现了Parcel,由于它在C/C++中,直接使用了内存来读取数据,因此,它更有效率。 分析Binder机制中的客户端与服务...

分布式调度任务管理——Dkron(2)运行配置

一、Dkron——服务器配置信息 dkron 的命令行操作都以dkron 开头,后面可以跟着不同的配置信息,例如在命令行敲出以下命令 :dkron agent --server --bootstrap-expect= 1 表示dkron以代理服务器形式启动,并且提供数据中心中预期的服务器数量为1。 其他相关配置dkron agent信息如下: –adver...