浏览 3083 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2005-04-14
处理关于不同环境下不同配置的 http://www.jroller.com/comments/kbaum/Weblog/cut_down_on_system_properties 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2005-04-14
因为PropertyOverrideConfigurer的语法只能beanName.beanProperties,不适合下面比如说属性是Properties的,
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean"> <!--<property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property>--> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="mappingResources"> <list> <value>com/jason/organization/domain/Organization.hbm.xml</value> <value>com/jason/organization/domain/User.hbm.xml</value> <value>com/jason/entapps/install/Constant.hbm.xml</value> <value>com/jason/hire/domain/Position.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</prop> <!--<prop key="hibernate.cache.use_query_cache">true</prop>--> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> </props> </property> </bean> 于是干脆,扩展PropertyPlaceholderConfigurer public class EnvPropertyResourceConfigurer extends PropertyPlaceholderConfigurer implements InitializingBean { private String env; private String defaultEnv = "prod"; private String basePropertyFile; private String envSystemProperty = "env"; /** * @return Returns the basePropertyFile. */ public String getBasePropertyFile() { return basePropertyFile; } /** * @param basePropertyFile The basePropertyFile to set. */ public void setBasePropertyFile(String basePropertyFile) { this.basePropertyFile = basePropertyFile; } /** * @return Returns the envSystemProperty. */ public String getEnvSystemProperty() { return envSystemProperty; } /** * @param envSystemProperty The envSystemProperty to set. */ public void setEnvSystemProperty(String envSystemProperty) { this.envSystemProperty = envSystemProperty; } /* * (non-Javadoc) * * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ public void afterPropertiesSet() throws Exception { if (basePropertyFile == null) { return; } String env = getEnv(); String fileName = basePropertyFile; if (TextUtils.stringSet(env)) { fileName += "." + env; } setLocation(new DefaultResourceLoader().getResource(fileName)); } /** * @return Returns the defaultEnv. */ public String getDefaultEnv() { return defaultEnv; } /** * @param defaultEnv The defaultEnv to set. */ public void setDefaultEnv(String defaultEnv) { this.defaultEnv = defaultEnv; } /** * @return Returns the env. */ public String getEnv() { return env; } /** * @param env The env to set. */ public void setEnv(String env) { this.env = env; } } 原文的SystemPropertyFactoryBean,在无值时返回null spring会报错, 于是我改了一下 public class SystemPropertyFactoryBean implements FactoryBean { private String propertyName; /** * @return Returns the propertyName. */ public String getPropertyName() { return propertyName; } /** * @param propertyName The propertyName to set. */ public void setPropertyName(String propertyName) { this.propertyName = propertyName; } /* (non-Javadoc) * @see org.springframework.beans.factory.FactoryBean#getObject() */ public Object getObject() throws Exception { String value = System.getProperty(getPropertyName()); if(value == null) { value = ""; } return value; } /* (non-Javadoc) * @see org.springframework.beans.factory.FactoryBean#getObjectType() */ public Class getObjectType() { return String.class; } /* (non-Javadoc) * @see org.springframework.beans.factory.FactoryBean#isSingleton() */ public boolean isSingleton() { return true; } } spring中的配置 <bean id="propertyConfigurer" class="com.jason.core.EnvPropertyResourceConfigurer"> <property name="basePropertyFile"> <value>classpath:jdbc.properties</value> </property> <property name="env"> <ref local="env"/> </property> </bean> <bean id="env" class="com.jason.core.SystemPropertyFactoryBean"> <property name="propertyName"> <value>env</value> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean> |
|
返回顶楼 | |