继承PropertyPlaceholderConfigurer 实现动态加载配置文件
2017-03-25 18:27
621 查看
在项目开发的过程中,往往有多个环境,dev,test,uat,run等.各个环境的配置文件是不同的.改造spring的PropertyPlaceholderConfigurer可以实现为各个不同的环境读取到各自的配置文件.这样为项目的部署省去很多工作.
Spring 的配置文件中,PropertyPlaceholderConfigurer 是占位符配置.为PropertyPlaceholderConfigurer 配置 .properties文件后,在Spring的配置文件中就可以使用${}的方式获取.properties数据.
假设现有dev,test,uat,run四个环境,在每个项目web容器启动参数里面添加不同启动参数,下图以dev为例
创建动态加载配置文件的DynamicServerConfig.java文件,核心方法为loadProperties,如下
在Spring配置文件中applicationContext.xml中配置DynamicServerConfig.
这样在tomcat启动的时候,Spring就会读取system.development.properties文件中的信息,不会读取其他几个文件中的信息.
Spring 的配置文件中,PropertyPlaceholderConfigurer 是占位符配置.为PropertyPlaceholderConfigurer 配置 .properties文件后,在Spring的配置文件中就可以使用${}的方式获取.properties数据.
假设现有dev,test,uat,run四个环境,在每个项目web容器启动参数里面添加不同启动参数,下图以dev为例
创建动态加载配置文件的DynamicServerConfig.java文件,核心方法为loadProperties,如下
package com.iris.spring.config; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.core.io.Resource; import org.springframework.util.DefaultPropertiesPersister; import org.springframework.util.PropertiesPersister; /** * * @author xiami * */ public class DynamicServerConfig extends PropertyPlaceholderConfigurer { /** Logger available to subclasses */ protected final Log logger = LogFactory.getLog(getClass()); protected Properties[] localProperties; protected boolean localOverride = false; private Resource[] locations; private PropertiesPersister propertiesPersister = new DefaultPropertiesPersister(); /** * copy */ public void setLocations(Resource[] locations) { this.locations = locations; } /** * copy */ public void setLocalOverride(boolean localOverride) { this.localOverride = localOverride; } /** * 根据启动参数,动态读取配置文件 */ protected void loadProperties(Properties props) throws IOException { if (locations != null) { logger.info("Start loading properties file....."); for (Resource location : this.locations) { InputStream is = null; try { String filename = location.getFilename(); String env = "system." + System.getProperty("spring.profiles.active", "run") + ".properties"; if (filename.contains(env)) { logger.info("Loading properties file from " + location); is = location.getInputStream(); this.propertiesPersister.load(props, is); } } catch (IOException ex) { logger.info("读取配置文件失败....."); ex.printStackTrace(); throw ex; } finally { if (is != null) { is.close(); } } } } } }
在Spring配置文件中applicationContext.xml中配置DynamicServerConfig.
<bean id="PropertyPlaceholderConfigurer" class="com.iris.spring.config.DynamicServerConfig"> <property name="locations"> <list> <value>classpath:config/system.development.properties</value> <value>classpath:config/system.run.properties</value> <value>classpath:config/system.test.properties</value> <value>classpath:config/system.uat.properties</value> </list> </property> </bean>
这样在tomcat启动的时候,Spring就会读取system.development.properties文件中的信息,不会读取其他几个文件中的信息.
相关文章推荐
- PropertyPlaceholderConfigurer加载属性配置文件:
- Spring环境搭建之:通过PropertyPlaceholderConfigurer加载属性配置文件:
- JFig读取配置文件,实现动态加载
- context:property-placeholder,spring配置文件加载属性信息
- 系统启动PropertyPlaceholderConfigurer加载配置到spring容器中
- Spring中配置和读取多个Properties文件-- PropertyPlaceholderConfigurer
- 动态设置spring配置PropertyPlaceholderConfigurer location的路径
- 利用java反射机制 读取配置文件 实现动态类加载以及动态类型转换
- Spring PropertyPlaceholderConfigurer Usage - 使用系统变量替换spring配置文件中的变量
- spring context:property-placeholder无法加载配置文件,导致属性无效
- 实现ibatis的动态加载sqlmap配置文件
- PropertyPlaceholderConfigurer属性配置文件 资源文件绑定
- 利用java反射机制 读取配置文件 实现动态类加载以及动态类型转换
- Spring PropertyPlaceholderConfigurer Usage - 使用系统变量替换spring配置文件中的变量
- spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
- spring 配置文件 获取变量(PropertyPlaceholderConfigurer)
- spring PropertyPlaceholderConfigurer 找不到配置文件原因
- Spring配置文件中使用PropertyPlaceholderConfigurer配置获取jdbc的配置信息
- spring中的PropertyPlaceholderConfigurer访问properties文件配置
- C#动态加载DLL,通过配置文件实现对程序集的即插即用