spring 配置文件 获取变量(PropertyPlaceholderConfigurer)
转自:https://hbiao68.iteye.com/blog/2031006
1.Spring的框架中,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类可以将.properties(key/value形式)文件中一些动态设定的值(value),在XML中替换为占位该键($key$)的值,.properties文件可以根据客户需求,自定义一些相关的参数,这样的设计可提供程序的灵活性。
2.在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码,如:
- <bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location">
- <value>classpath:/spring/include/dbQuery.properties</value>
- </property>
- <property name="fileEncoding">
- <value>UTF-8</value>
- </property>
- </bean>
其中classpath是引用src目录下的文件写法,当存在多个Properties文件时,配置就需使用locations了:
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:/spring/include/jdbc-parms.properties</value>
- <value>classpath:/spring/include/base-config.properties</value>
- <value>classpath*:config/jdbc.properties</value>
- </list>
- </property>
- </bean>
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location">
- <value>classpath:hb.properties</value>
- <value>/WEB-INF/config/project/project.properties</value>
- </property>
- </bean>
接下来我们要使用多个PropertyPlaceholderConfigurer来分散配置,达到整合多工程下的多个分散的Properties文件,其配置如下
- <bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="order" value="2" />
- <property name="ignoreUnresolvablePlaceholders" value="true" />
- <property name="locations">
- <list>
- <value>classpath:/spring/include/jdbc-parms.properties</value>
- <value>classpath:/spring/include/base-config.properties</value>
- </list>
- </property>
- </bean>
其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true
3.譬如,jdbc.properties的内容为:
- jdbc.driverClassName=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=round;
- jdbc.username=root
- jdbc.password=123456
备注:一定要在properties文件中&写为&因为在xml文件中不识别&,必须是&
4.那么在spring配置文件中,我们就可以这样写:
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath: conf/sqlmap/jdbc.properties </value>
- </list>
- </property>
- </bean>
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
这样,一个简单的数据源就设置完毕了。可以看出:PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。
查看源代码,可以发现,locations属性定义在PropertyPlaceholderConfigurer的祖父类PropertiesLoaderSupport中,而location只有setter方法。类似于这样的配置,在spring的源程序中很常见的。
PropertyPlaceholderConfigurer如果在指定的Properties文件中找不到你想使用的属性,它还会在Java的System类属性中查找。
我们可以通过System.setProperty(key, value)或者java中通过-Dnamevalue来给Spring配置文件传递参数。
我们还可以使用注解的方式实现,如:<context:property-placeholder location="classpath:jdbc.properties" />,效果跟PropertyPlaceholderConfigurer是一样的。
原理分析
PropertyPlaceholderConfigurer
类是如何做到xml配置的bean的配置熟悉的运行时替换的呢?这个离不开Spring框架提供的许多扩展点。这个类依赖的扩展点就是BeanFactoryPostProcessors
见下面的类继承图:
BeanFactoryPostProcessors
类有一个方法,提供了在Bean创建前对Bean的Definition进行处理的钩子机制。
//在Bean初始化前被调用(例如在InitializingBean的afterPropertiesSet前)
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
从上面的继承图我们知道PropertyPlaceholderConfigurer
继承自类PropertyResourceConfigurer
, 而该类实现了接口BeanFactoryPostProcessors
public abstract class PropertyResourceConfigurer extends PropertiesLoaderSupport
implements BeanFactoryPostProcessor, PriorityOrdered {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
try {
Properties mergedProps = mergeProperties(); // Convert the merged properties, if necessary.
convertProperties(mergedProps); // Let the subclass process the properties.
processProperties(beanFactory, mergedProps);
}
catch (IOException ex) {
throw new BeanInitializationException("Could not load properties", ex);
}
}
protected abstract void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
throws BeansException;
}
我们的主角PropertyPlaceholderConfigurer
实现了父类中的方法processProperties
, 在该类中对xml配置文件中的展位符进行替换处理
/**
* Visit each bean definition in the given bean factory and attempt to replace ${...} property
* placeholders with values from the given properties.
*/
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException { StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props);
doProcessProperties(beanFactoryToProcess, valueResolver);
} protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
StringValueResolver valueResolver) { BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver); String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
for (String curName : beanNames) {
// Check that we're not parsing our own bean definition,
// to avoid failing on unresolvable placeholders in properties file locations.
if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);
try {
visitor.visitBeanDefinition(bd);
}
catch (Exception ex) {
throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage(), ex);
}
}
} // New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
beanFactoryToProcess.resolveAliases(valueResolver); // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
}
这样就解开了Spring中xml配置文件中占位符的替换魔法功能。
现在想一下,如果我们的配置文件中对某些属性进行了加密,这时再使用 PropertyPlaceholderConfigurer 读取配置文件我们想要加密前的内容该怎么办?
答案就是重写 PropertyPlaceholderConfigurer。除了数据库的配置信息我们放在配置文件,然后可以通过 druid 进行加解密。但是配置的邮箱信息呢?
这时重写它就显得很有必要。PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。
下面来看一个通过 PropertyPlaceholderConfigurer读取加解密配置文件的案例:
package com.xttblog.plugin;
import com.zheng.common.util.AESUtil;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
//支持加密配置文件插件
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
private String[] propertyNames = {
"master.jdbc.password", "slave.jdbc.password", "generator.jdbc.password", "master.redis.password"
};
//解密指定propertyName的加密属性值
@Override
protected String convertProperty(String propertyName, String propertyValue) {
for (String p : propertyNames) {
if (p.equalsIgnoreCase(propertyName)) {
return AESUtil.AESDecode(propertyValue);
}
}
return super.convertProperty(propertyName, propertyValue);
}
}
我们只需重写 PropertyPlaceholderConfigurer 类的 convertProperty 方法即可,然后在该方法中实现解密工作。
spring 配置文件 获取变量(PropertyPlaceholderConfigurer)的更多相关文章
- 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
spring管理属性配置文件properties--使用PropertiesFactoryBean 对于属性配置,一般采用的是键值对的形式,如:key=value属性配置文件一般使用的是XXX.pr ...
- Spring使用环境变量控制配置文件加载
项目中需要用到很多配置文件,不同环境的配置文件是不一样的,因此如果只用一个配置文件,势必会造成配置文件混乱,这里提供一种利用环境变量控制配置文件加载的方法,如下: 一.配置环境变量 如果是window ...
- 监听器如何获取Spring配置文件(加载生成Spring容器)
Spring容器是生成Bean的工厂,我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个 ...
- Spring使用环境变量控制配置文件加载(转)
项目中需要用到很多配置文件,不同环境的配置文件是不一样的,因此如果只用一个配置文件,势必会造成配置文件混乱,这里提供一种利用环境变量控制配置文件加载的方法,如下: 一.配置环境变量 如果是window ...
- 监听器如何获取Spring配置文件
我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个监听器,在项目启动时将首页的数据查询出 ...
- spring boot中配置文件中变量的引用
配置文件中 变量的自身引用 ${名称} java文件中引用:非静态变量 之间在变量上面注释@Value("${名称}") 静态变量 在set方法上注释@Value("$ ...
- Spring配置文件外部化配置及.properties的通用方法
摘要:本文深入探讨了配置化文件(即.properties)的普遍应用方式.包括了Spring.一般的.远程的三种使用方案. 关键词:.properties, Spring, Disconf, Java ...
- spring源码分析系列 (5) spring BeanFactoryPostProcessor拓展类PropertyPlaceholderConfigurer、PropertySourcesPlaceholderConfigurer解析
更多文章点击--spring源码分析系列 主要分析内容: 1.拓展类简述: 拓展类使用demo和自定义替换符号 2.继承图UML解析和源码分析 (源码基于spring 5.1.3.RELEASE分析) ...
- spring配置文件引入properties文件:<context:property-placeholder>标签使用总结
一.问题描述: 1.有些参数在某些阶段中是常量,比如: (1)在开发阶段我们连接数据库时的连接url.username.password.driverClass等 (2)分布式应用中client端访问 ...
随机推荐
- 005.KVM日常管理2-virt管理
一 安装管理工具 [root@kvm-host ~]# rpm -qa|grep libguestfs-tools #查看相关管理工具,若没安装,可使用yum安装. 二 日常管理 2.1 命令格式 ...
- SpringBoot详细研究-02数据访问
Springboot对数据访问部分提供了非常强大的集成,支持mysql,oracle等传统数据库的同时,也支持Redis,MongoDB等非关系型数据库,极大的简化了DAO的代码,尤其是Spring ...
- 基于CommonsCollections4的Gadget分析
基于CommonsCollections4的Gadget分析 Author:Welkin 0x1 背景及概要 随着Java应用的推广和普及,Java安全问题越来越被人们重视,纵观近些年来的Java安全 ...
- 一键安装LNMP/LAMP
安装步骤:1.使用putty或类似的SSH工具登陆VPS或服务器: 登陆后运行:yum install screen安装 screen screen -S lnmp创建一个名字为lnmp的会话 2. ...
- POP3_使用SSL链接邮箱并获取邮件
Gmail目前已经启用了POP3和SMTP服务,与其他邮箱不同的是Gmail提供的POP3和SMTP是使用安全套接字层SSL的,因此常规的JavaMail程序是无法收发邮件的,下面是使用JavaMai ...
- Temporary ASP.Net Files探究
了解.net平台的兄弟都知道,.net也是采用动态编译的也就是说我们常说的build生成的dll只是中间代码而在web第一次请求的时候才是真正意义上的编译生成二进制代码这也就是为什么刚编译完第一次打开 ...
- Snmp学习总结(四)——WinServer2003安装和配置SNMP
一.安装SNMP 今天讲解一下在WinServer2003安装和配置SNMP,具体操作步骤如下: 找到[控制面板]→[添加或删除程序]
- 解决Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COER
今天在用java与mysql数据库时发现Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COER ...
- Git 的 WindowsXP安装
文章1: http://blog.sina.com.cn/s/blog_5063e4c80100sqzq.html 一.安装必要客户端 1. TortoiseGit http://tortoisegi ...
- C#中泛型容器Stack<T>的用法,以及借此实现”撤销/重做”功能
.Net为我们提供了众多的泛型集合.比如,Stack<T>先进后出,Queue<T>先进先出,List<T>集合元素可排序,支持索引,LinkedList<T ...