PropertyPlaceholderConfigurer获取属性文件指定key的value值
来源:互联网 发布:盟军苏军将领知乎 编辑:程序博客网 时间:2024/05/22 01:34
今天花了一天的时间,研究了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,如有错误,也希望大家指出,谢谢
- PropertyPlaceholderConfigurer获取属性文件指定key的value值
- Spring PropertyPlaceholderConfigurer获取属性文件值
- jsp页面获取map集合的指定key的value
- PropertyPlaceholderConfigurer在spring获取属性文件
- java根据map指定的value值获取对应的key值使用Gson实现
- php获取二维数组value的最小值key为指定非整型值
- 根据key从Properties文件中加载指定的value
- ibatis获取对象map属性的对应key的value值
- js获取对象,数组所有属性值(key)和对应值(value)的方法
- js获取对象,数组所有属性值(key)和对应值(value)的方法
- 获取Map<key,value>的key值方法如下:
- 将属性文件以key-value值形式输出的工具类
- 使用PropertyPlaceholderConfigurer读取.properties文件(3)-公共类获取属性
- jstl 获取map的key 和 value的值
- c语言 读取文件的每一行; 查找指定key的value
- 获取Map的key值和value值
- 根据值(value)获取泛型Dictionary的键(key)
- java 获取 Map中所有的 key 和 value 值
- LightOJ 1028 Trailing Zeroes (I)
- Kalman滤波的多种实现方式浅析
- ORA-14037: 分区 "P8" 的分区界限过高
- SQL注入原理
- Android_ViewDragHelper
- PropertyPlaceholderConfigurer获取属性文件指定key的value值
- android编译分析之3—pathmap.mk
- 在项目中使用AngularJS+UI-Grid
- web开发乱码问题
- OracleOraDb11g_home1TNSListener服务启动后停止
- 剑指Offer-31-连续子数组的最大和
- 顺序折线遍历二维数组
- jQuery Mobile 学习四
- 绕过CONFIG_MODVERSIONS