spring读取配置文件优化
来源:互联网 发布:独立分销商城源码 编辑:程序博客网 时间:2024/05/01 18:15
1、达到目的:根据tomcat的启动参数中配置不同的spring.profiles.active的值,读取不同环境的配置参数。
4、然后添加不同环境下的配置文件,便于减少因调整配置文件导致的失误。
2、spring配置文件的代码:
<!-- 定义受环境影响易变的变量 --><bean id="propertyPlaceholderConfigurer" class="com.zhou..spring.util.config.DynamicServerConfig"><property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /><property name="ignoreResourceNotFound" value="true" /><property name="locations"><list> <value>classpath:config/system.devjunit.properties</value><value>classpath:config/system.development.properties</value><value>classpath:config/system.devzcb.properties</value><value>classpath:config/system.development02.properties</value><value>classpath:config/system.test.properties</value><value>classpath:config/system.testzcb.properties</value><value>classpath:config/system.test02.properties</value><value>classpath:config/system.uat.properties</value><value>classpath:config/system.uatzcb.properties</value><value>classpath:config/system.uattask.properties</value><value>classpath:config/system.run.properties</value><value>classpath:config/system.runzcb.properties</value><value>classpath:config/system.runtask.properties</value></list></property></bean>3、DynamicServerConfig类的代码:
public class DynamicServerConfig extends PropertyPlaceholderConfigurer {/** * Set a location of a properties file to be loaded. * <p> * Can point to a classic properties file or to an XML file that follows JDK 1.5's properties XML format. */private boolean localOverride = false;private Properties[] localProperties;private Resource[] locations;private final PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();private boolean ignoreResourceNotFound = false;/** * Set if failure to find the property resource should be ignored. True is appropriate if the properties file is * completely optional. Default is "false". */@Overridepublic void setIgnoreResourceNotFound(boolean ignoreResourceNotFound) {this.ignoreResourceNotFound = ignoreResourceNotFound;}/** * Set local properties, e.g. via the "props" tag in XML bean definitions. These can be considered defaults, to be * overridden by properties loaded from files. */@Overridepublic void setProperties(Properties properties) {this.localProperties = new Properties[] { properties };super.setProperties(properties);}/** * Set local properties, e.g. via the "props" tag in XML bean definitions, allowing for merging multiple properties * sets into one. */@Overridepublic void setPropertiesArray(Properties[] propertiesArray) {this.localProperties = propertiesArray;}@Overridepublic void setLocation(Resource location) {this.locations = new Resource[] { location };super.setLocation(location);}/** * Set locations of properties files to be loaded. * <p> * Can point to classic properties files or to XML files that follow JDK 1.5's properties XML format. */@Overridepublic void setLocations(Resource[] locations) {this.locations = locations;super.setLocations(locations);}/** * Set whether local properties override properties from files. Default is "false": properties from files override * local defaults. Can be switched to "true" to let local properties override defaults from files. */@Overridepublic void setLocalOverride(boolean localOverride) {this.localOverride = localOverride;}@Overridepublic Properties mergeProperties() throws IOException {Properties result = new Properties();if (this.localOverride) {// Load properties from file upfront, to let local properties// override.loadProperties(result);}if (this.localProperties != null) {for (int i = 0; i < this.localProperties.length; i++) {CollectionUtils.mergePropertiesIntoMap(this.localProperties[i], result);}}if (!this.localOverride) {// Load properties from file afterwards, to let those properties// override.loadProperties(result);}return result;}/** * Load properties into the given instance. * * @param props * the Properties instance to load into * @throws java.io.IOException * in case of I/O errors * @see #setLocations */@Overrideprotected void loadProperties(Properties props) throws IOException {if (this.locations != null) {for (int i = 0; i < this.locations.length; i++) {Resource location = this.locations[i];if (logger.isInfoEnabled()) {logger.info("Loading properties file from " + location);}InputStream is = null;try {/** * 读取启动参数中的环境变量,默认为devjunit */String runEnv = System.getProperty("spring.profiles.active", "devjunit");if (location.getFilename().indexOf("." + runEnv + ".") > 0) {is = location.getInputStream();this.propertiesPersister.load(props, is);break;}} catch (IOException ex) {if (this.ignoreResourceNotFound) {if (logger.isWarnEnabled()) {logger.warn("Could not load properties from " + location + ": " + ex.getMessage());}} else {throw ex;}} finally {if (is != null) {is.close();}}}}}}
4、然后添加不同环境下的配置文件,便于减少因调整配置文件导致的失误。
0 0
- spring读取配置文件优化
- 优化spring的配置文件的配置读取方式
- 读取Spring配置文件
- dom4j读取spring配置文件
- Spring配置文件读取方式
- spring 读取properties配置文件
- Spring读取配置文件
- Spring读取配置文件路径
- spring读取配置文件
- spring resourceLoader 读取配置文件
- Spring读取配置文件
- Spring boot读取配置文件
- Spring读取properties配置文件
- spring 读取自定义配置文件
- spring test读取配置文件
- spring读取配置文件
- spring-boot 配置文件读取
- 读取Spring配置文件
- solution Of 1014. Waiting in Line (30)
- Python爬虫实践(九):第一个爬虫实例(简书首页)
- 成语、典故的出处
- Java mail乱码
- reactiveCocoa实践一
- spring读取配置文件优化
- 配置AndroidStudio:IDE和VM选项,JDK,等等
- java 基础安装和Tomcat8配置
- mac中apache开启https功能
- linux 进程单例
- "Java消息回收机制"之源码+图文完全解析
- 【Java小品系列】 一、从类的初始化谈起
- HDU 3007 【随机增量法】
- iOS 应用 集成了Message(极光IM) 适配IPv6环境 提交应用时注意事项