Spring用代码来读取properties文件
我们都知道,Spring可以@Value的方式读取properties中的值,只需要在配置文件中配置org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:config.properties</value>
</property>
</bean>
那么在需要用到这些获取properties中值的时候,可以这样使用
@Value("${sql.name}")
private String sqlName;
但是这有一个问题,我每用一次配置文件中的值,就要声明一个局部变量。有没有用代码的方式,直接读取配置文件中的值。
答案就是重写PropertyPlaceholderConfigurer
public class PropertyPlaceholder extends PropertyPlaceholderConfigurer { private static Map<String,String> propertyMap; @Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
propertyMap = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
propertyMap.put(keyStr, value);
}
} //static method for accessing context properties
public static Object getProperty(String name) {
return propertyMap.get(name);
}
}
在配置文件中,用上面的类,代替PropertyPlaceholderConfigurer
<bean id="propertyConfigurer" class="com.gyoung.mybatis.util.PropertyPlaceholder">
<property name="location">
<value>classpath:config.properties</value>
</property>
</bean>
这样在代码中就可以直接用编程方式获取
PropertyPlaceholder.getProperty("sql.name");
如果是多个配置文件,配置locations属性
<bean id="propertyConfigurer"
class="com.gyoung.mybatis.util.PropertyPlaceholder">
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>file:./jdbc.properties</value>
<value>file:./module.config.properties</value>
<value>classpath:jdbc.properties</value>
<value>classpath*:*.config.properties</value>
</list>
</property>
</bean>
Spring用代码来读取properties文件的更多相关文章
- (转)Spring用代码来读取properties文件
转至http://www.cnblogs.com/Gyoung/p/5507063.html 我们都知道,Spring可以@Value的方式读取properties中的值,只需要在配置文件中配置org ...
- spring使用@Value注解读取.properties文件时出现中文乱码问题的解决
解决办法 在spring中我们常常使用.properties对一些属性进行一个提前配置, spring 在读取*.properties文件时, 默认使用的是asci码, 这时 我们需要对其编码进行转换 ...
- spring boot --- 使用 注解 读取 properties 文件 信息
1.前言 以前使用spring MVC框架 ,读取properties 配置文件需要写一大堆东西 ,也许 那时候 不怎么使用注解 ,现在使用spring boot ,发现了非常简便的办法---使用注解 ...
- java代码如何读取properties文件
我们在开发工程中,有时候需要在Java代码中定义一些在部署生产环境时容易改变的变量,还需要我们单独放在一个外部属性文件中,方便我们将来修改.这里列出了两种比较方便的方式. 一.在Spring配置文件中 ...
- Spring 中 用 ${xxx} 读取properties文件的说明
properties 如果在 spring 中通过 PropertyPlaceholderConfigurer 加载,当spring 中需要 用到 properties 中的一些 key 和value ...
- spring使用@Value标签读取.properties文件的中文乱码问题的解决
最近测试某个老系统的时候,启动的时候发@Value注入的中文是乱码,文件使用GBK/UTF-8的时候均会出现乱码问题,但是spring配置文件里面注入的占位符并没有这个问题,bean文件设置了file ...
- 【Spring源码分析】.properties文件读取及占位符${...}替换源码解析
前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.pr ...
- spring 框架的xml文件如何读取properties文件数据
spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...
- 如何通过Spring读取Properties文件
1 在Spring中配置文件中, 配置配置文件的引用 <util:properties id="settings" location="/WEB-INF/c ...
随机推荐
- Java学习笔记(一)
纯属个人学习笔记,有什么不足之处大家留言,谢谢 Java程序打包与JAR运行方法 在Eclipse的"包资源管理器"视图中找到要打包成JAR文件的项目.在项目名称上单击鼠标右键,在 ...
- js定位
1.引入 百度地图js(1.3以后需要key) <script type="text/javascript" src="http://api.map.baidu.c ...
- py-faster-rcnn搭配pycharm使用
先在ubuntu下配置好cuda.cudnn以及py-faster-rcnn,然后安装pycharm. 打开pycharm看py-faster-rcnn代码,import处各种红色下划曲线,提示报错. ...
- 【poj2154】 Color
http://poj.org/problem?id=2154 (题目链接) 题意 n个珠子的项链,可以染上n中颜色,项链可以旋转不能翻转,求染色方案数. Solution 经典的公式: \begin{ ...
- vmware Centos6.6安装64位
Centos6.6安装64位 必须开启BIOS中的虚拟化技术 首先开机进入BIOS,一般机器是按F2,我的T420是按F1,然后进入Security,Virtualization,选择Enable即可 ...
- jsp上传图片实时显示
jsp代码 <div class="form-group" id="caseIma"> <label class="control- ...
- Winform 后台将指定的控件集合添加到制定容器中
/// <summary> /// 把按钮按照行数分割排列 /// </summary> /// <param name="ControlArry"& ...
- android studio关联genymotion模拟器,未显示设备
如以下截图所示,在搭建android studio+genymotion时,遇到android studio关联genymotion时,显示不出模拟器设备,请问有没有遇到此现象的朋友,分享下解决方法, ...
- Swift-代理
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px "Helvetica Neue"; color: #535b60; bac ...
- Spring MVC学习笔记——SiteMesh的使用(转)
转自 SiteMesh的使用 SiteMesh的介绍就不多说了,主要是用来统一页面风格,减少重复编码的. 它定义了一个过滤器,然后把页面都加上统一的头部和底部. 需要先在WEB-INF/lib下引入s ...