我们都知道,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文件的更多相关文章

  1. (转)Spring用代码来读取properties文件

    转至http://www.cnblogs.com/Gyoung/p/5507063.html 我们都知道,Spring可以@Value的方式读取properties中的值,只需要在配置文件中配置org ...

  2. spring使用@Value注解读取.properties文件时出现中文乱码问题的解决

    解决办法 在spring中我们常常使用.properties对一些属性进行一个提前配置, spring 在读取*.properties文件时, 默认使用的是asci码, 这时 我们需要对其编码进行转换 ...

  3. spring boot --- 使用 注解 读取 properties 文件 信息

    1.前言 以前使用spring MVC框架 ,读取properties 配置文件需要写一大堆东西 ,也许 那时候 不怎么使用注解 ,现在使用spring boot ,发现了非常简便的办法---使用注解 ...

  4. java代码如何读取properties文件

    我们在开发工程中,有时候需要在Java代码中定义一些在部署生产环境时容易改变的变量,还需要我们单独放在一个外部属性文件中,方便我们将来修改.这里列出了两种比较方便的方式. 一.在Spring配置文件中 ...

  5. Spring 中 用 ${xxx} 读取properties文件的说明

    properties 如果在 spring 中通过 PropertyPlaceholderConfigurer 加载,当spring 中需要 用到 properties 中的一些 key 和value ...

  6. spring使用@Value标签读取.properties文件的中文乱码问题的解决

    最近测试某个老系统的时候,启动的时候发@Value注入的中文是乱码,文件使用GBK/UTF-8的时候均会出现乱码问题,但是spring配置文件里面注入的占位符并没有这个问题,bean文件设置了file ...

  7. 【Spring源码分析】.properties文件读取及占位符${...}替换源码解析

    前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.pr ...

  8. spring 框架的xml文件如何读取properties文件数据

    spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...

  9. 如何通过Spring读取Properties文件

    1 在Spring中配置文件中, 配置配置文件的引用     <util:properties id="settings" location="/WEB-INF/c ...

随机推荐

  1. python基础补漏-05-生成器和装饰器

    [1]生成器 很难用简单的语言描述生成器. 生成器:从字面上来理解,就是以某种规则为基础,不断的生成数据的工具 生成器函数: 在函数中如果出现了yield关键字,那么该函数就不再是普通函数,而是生成器 ...

  2. C#-WebForm-复合控件

    学习顺序:1.如何绑定数据 2.如何设置绑定项 3.如何取出数据 1.RadioButton - 单选按钮 RadioButtonList - 单选按钮组 控件中的ID生成了相同名字的 ID.Name ...

  3. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  4. 基于redis 实现分布式锁的方案

    在电商项目中,经常有秒杀这样的活动促销,在并发访问下,很容易出现上述问题.如果在库存操作上,加锁就可以避免库存卖超的问题.分布式锁使分布式系统之间同步访问共享资源的一种方式 基于redis实现分布式锁 ...

  5. a版本十日冲刺总汇

    DAY ONE: http://www.cnblogs.com/aruba/p/6041243.html 2016-11-08 DAY TWO: http://www.cnblogs.com/arub ...

  6. [信安presentation]Fight against GFW

    Section1:加密 加密算法分为:对称加密算法.非对称加密算法.Hash 1.1对称加密算法 加密解密使用相同的密钥 eg:DES,AES,RC4,RC5,Triple DES 缺点:1.因为加密 ...

  7. SHELL编写NGINX服务控制脚本

    使用源码包安装的Nginx没办法使用"service nginx start"或"/etc/init.d/nginx start"进行操作和控制,所以写了以下的 ...

  8. dom 节点篇---模块

    改写成如下代码: var creatTag=(function(){ //var count=5; var oUl=document.createElement('ul'); var oDiv=doc ...

  9. MyEclipse10--的使用经验

    MyEclipse10--的使用经验总结 ------------------ 1.MyEclipse中的验证validation----->>用MyEclipse做ExtJs项目研发的时 ...

  10. ThinkphpCMF笔记

    1.模板js,css文件__PUBLIC__ <link href="__TMPL__Public/style.css" rel="stylesheet" ...