spring使用PropertyPlaceholderConfigurer扩展来满足不同环境的参数配置,
来自:http://www.javaarch.net/jiagoushi/548.htm
PropertyPlaceholderConfigurer是spring提供我们来把一些环境变量(数据库连接相关参数,文件路径等)统一管理起来,然后在bean中指定对应的变量的。但是往往开发环境,测试环境,生成环境的这些参数配置是不同的,那么我们如何使用PropertyPlaceholderConfigurer扩展来满足不同环境的配置需求,而不需要在不同环境需要修改代码或者配置。
1.我们扩展下PropertyPlaceholderConfigurer,可以通过在properties中production.mode默认配置来或者不同环境的配置,然后有限通过取系统环境变量的这个值来作为我们开发环境,测试环境,生产环境的选择。
import java.io.IOException; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; /** * 可以按照不同的运行模式启用相应的配置 * */ public class MutilPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer implements InitializingBean { private static final String PRODUCTION_MODE = "production.mode"; // 缓存所有的属性配置 private Properties properties; /** * @return the mode */ public String getMode() { return properties.getProperty(PRODUCTION_MODE); } @Override protected Properties mergeProperties() throws IOException { Properties mergeProperties = super.mergeProperties(); // 根据路由原则,提取最终生效的properties this.properties = new Properties(); // 获取路由规则,系统属性设置mode优先 String mode = System.getProperty(PRODUCTION_MODE); if (StringUtils.isEmpty(mode)) { String str = mergeProperties.getProperty(PRODUCTION_MODE); mode = str != null ? str : "ONLINE"; } properties.put(PRODUCTION_MODE, mode); String[] modes = mode.split(","); Set<Entry<Object, Object>> es = mergeProperties.entrySet(); for (Entry<Object, Object> entry : es) { String key = (String) entry.getKey(); int idx = key.lastIndexOf('_'); String realKey = idx == -1 ? key : key.substring(0, idx); if (!properties.containsKey(realKey)) { Object value = null; for (String md : modes) { value = mergeProperties.get(realKey + "_" + md); if (value != null) { properties.put(realKey, value); break; } } if (value == null) { value = mergeProperties.get(realKey); if (value != null) { properties.put(realKey, value); } else { throw new RuntimeException("impossible empty property for " + realKey); } } } } return properties; } /** * 开放此方法给需要的业务 * * @param key * @return */ public String getProperty(String key) { return resolvePlaceholder(key, properties); } @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub } }
然后我们在properties中可以这么配置:也就是默认配置是ONLINE 生产模式,那么只要在系统变量中没有配置production.mode,则我们取ONLINE的配置,也就是下面的参数取后缀为_ONLINE的配置。
production.mode=ONLINE #lucene index data dir lucene.index.dir_DEV=e:\\logs\\lucene lucene.index.dir_ONLINE=/home/admin/data #velocity file.resource.loader.cache_DEV=false file.resource.loader.modificationCheckInterval_DEV=2 file.resource.loader.cache_ONLINE=true file.resource.loader.modificationCheckInterval_ONLINE=-1
对应spring的配置为:
<!-- velocity --> <import resource="classpath*:*.xml" /> <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath"> <value>WEB-INF/velocity/</value> </property> <property name="velocityProperties"> <props> <prop key="directive.foreach.counter.name">velocityCount</prop> <prop key="directive.foreach.counter.initial.value">1</prop> <prop key="input.encoding">GBK</prop> <prop key="output.encoding">GBK</prop> <prop key="file.resource.loader.cache">${file.resource.loader.cache}</prop> <prop key="file.resource.loader.modificationCheckInterval">${file.resource.loader.modificationCheckInterval}</prop> <prop key="velocimacro.library.autoreload">false</prop> <prop key="velocimacro.library">macro.vm</prop> </props> </property> </bean>
这种参数包括数据库连接串,文件路径等都可以这么配,因为velocity在测试环境不需要cache,能够修改即生效,但是线上环境加上cache则能提高性能,所以,默认使用ONLINE的配置,但是在测试环境的VM参数中加上-Dproduction.mode=DEV,则在开发环境用的是_DEV后缀的配置,到了线上代码则不用改。非常方便。
最好就是架上placeholder的自定义配置bean
<bean id="placeholder" class="org.springweb.core.MutilPropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <value>classpath*:*-placeholder.properties</value> </list> </property> </bean>
相关推荐
Spring中PropertyPlaceholderConfigurer的使用
Spring属性占位符PropertyPlaceholderConfigurer的使用,自己写的一个demo
NULL 博文链接:https://wokeke.iteye.com/blog/2214113
主要介绍了Spring如何使用PropertyPlaceholderConfigurer读取文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 ...
使用环境变量进行替换 4.9.2.PropertyOverrideConfigurer类 4.10.使用alias节点为对象添加别名 4.11.IApplicationContext简介 4.12.配置应用程序上下文 4.12.1.注册自定义解析器 4.12.2.创建自定义资源处理器 4.12.3...
管理 Bean 从第一个 Spring 应用程式开始,逐步了解何谓依赖注入,以及如何使用 Spring 的容器功能来管理 Bean,了解 Bean 在 Spring 容器中的生命周期。 第一个 Spring 程式 BeanFactory、 ...
#Spring 属性注释扩展的 PropertyPlaceHolderConfigurer 使用注解将配置属性注入到 Spring 组件中。 注意:Spring 3 现在支持使用 @Value 注释的容器的。 该项目仅用于 Spring 2.5.x 支持。 ##入门Spring房产注解...
在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...
这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...
NULL 博文链接:https://chyy001.iteye.com/blog/1338488
第一部分 Spring framework 概述..5 第1 章 Spring Framework 介绍..6 1.1 依赖注入和控制反转.6 1.2 模块6 1.2.1 核心容器.7 1.2.2 数据访问/ 整合..7 1.2.3 Web ..8 1.2.4 AOP...
前几天搞 Spring + Hibernate + Struts 事务配置 ,网上找了好多资料,不过好无语,大多都是 Ctrl + V,浪费俺的宝贵时间 现在我总结配出一套,给大家参考参考,可能有不足,请大家多多交流。 附:内有弹出...
-- JTA环境的事务配置 <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager or WebLogicJtaTransactionManager"/> --> <!-- 使用annotation定义事务 --> ...
spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................
JDBCTemp+MySQL示例。Spring,PropertyPlaceholderConfigurer,BeanPropertyRowMapper
什么 与通过从属性文件读取其配置来进行自我配置相比,对于应用程序而言,有什么可能更简单? 好吧,对于业务应用程序而言... 首先,我们将使用PropertyPlaceholderConfigurer通过读取存储在.properties文件中的属性来
PropertyPlaceholderConfigurer PropertyResourceConfigurer PropertyValue PropertyValues PropertyValuesEditor PrototypeAspectInstanceFactory PrototypeTargetSource ProxyConfig ProxyFactory ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................