PropertyPlaceholderConfigurer获取属性文件指定key的value值
2015-11-24 15:58
441 查看
今天花了一天的时间,研究了PropertyPlaceholderConfigurer这个类,首先它的作用是一个资源属性的配置器,能够将BeanFactory的里定义的内容放在一个以.propertis后缀的文件中。那么如何在Java类中获取指定key的value值呢!
步骤:
一:首先创建一个属性文件
二:然后自定义一个属性加载类工具类,必须要extends PropertyPlaceholderConfigurer类,(实际是对PropertyPlaceholderConfigurer类的一个扩展)
public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer {
private static Map<String, Object> ctxPropertiesMap;
@Override
protected void processProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap = new HashMap<String, Object>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}
public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}
public static Object setContextProperty(String name,Object value) {
return ctxPropertiesMap.put(name, value);
}
}
三:然后再spring文件中配置如下
<bean id="propertyConfigurer"
class="com.ljzforum.platform.util.CustomizedPropertyConfigurer">
<property name="locations">
<list>
<value>classpath:context/lxh.properties</value>
</list>
</property>
</bean>
四:最后就是怎么使用了,代码如下
String appId = (String) CustomizedPropertyConfigurer.getContextProperty("app_id");
String app_secret =(String) CustomizedPropertyConfigurer.getContextProperty("app_secret");
很容易吧,如果有什么不懂的问题欢迎大家一起交流学习,本人扣扣2739677514,如有错误,也希望大家指出,谢谢
步骤:
一:首先创建一个属性文件
二:然后自定义一个属性加载类工具类,必须要extends PropertyPlaceholderConfigurer类,(实际是对PropertyPlaceholderConfigurer类的一个扩展)
public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer {
private static Map<String, Object> ctxPropertiesMap;
@Override
protected void processProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap = new HashMap<String, Object>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}
public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}
public static Object setContextProperty(String name,Object value) {
return ctxPropertiesMap.put(name, value);
}
}
三:然后再spring文件中配置如下
<bean id="propertyConfigurer"
class="com.ljzforum.platform.util.CustomizedPropertyConfigurer">
<property name="locations">
<list>
<value>classpath:context/lxh.properties</value>
</list>
</property>
</bean>
四:最后就是怎么使用了,代码如下
String appId = (String) CustomizedPropertyConfigurer.getContextProperty("app_id");
String app_secret =(String) CustomizedPropertyConfigurer.getContextProperty("app_secret");
很容易吧,如果有什么不懂的问题欢迎大家一起交流学习,本人扣扣2739677514,如有错误,也希望大家指出,谢谢
相关文章推荐
- Hibernate—Query
- WindowBuilder was not able to show the GUI
- UITableViewCell左滑button的定制
- 产品设计流程及UI设计注意事项
- iOS学习笔记--滚动视图(UIScrollView)
- 世界坐标转UGUI屏幕坐标
- 使用android SpannableStringBuilder实现图文混排源码
- Android UI性能优化
- UIButton图片与文字位置调整
- UITextView - 1
- UIImage 图片处理:截图,缩放,设定大小,存储
- TBB(Intel Threading Building Blocks)学习笔记
- CSS3媒体查询(Media Queries)介绍
- 深入理解dispatch_queue
- 深入了解require.js特性
- iOS开发--毛玻璃效果(UIImage+Blur)直接拿去
- flashback query、flashback drop、flashback table用法总结
- Incorrect string value:"\ \ \ \ \"for column'trurname' at row 1
- 【Quick 3.3】资源脚本加密及热更新(三)热更新模块
- _mysql_exceptions.Warning: Incorrect string value: '\xE5\x95\x86\xE5\x93\x81...' (mysql设置utf8字符集)