spring配置文件加载新增了在数据库中配置参数文件的支持,这样把环境相关的参数配置在数据中,解决了同一套代码包可以不用修改配置文件运行在任何环境中。
方法1、编写类继承org.springframework.beans.factory.config.PropertyPlaceholderConfigurer实现mergeProperties 方法,在该方法总添加 从数据库读取参数代码。
如: 参照了 http://www.hidehai.com/html/y2012/776.html 做了实现 DataSourceOverridePropertyPlaceholderConfigurer
spring 配置文件
<bean id="propertyConfigurer" class="com.xx.commons.config.DataSourceOverridePropertyPlaceholderConfigurer"> <property name="nullValue" value="[null]" /> <property name="locations"> <list> <value>classpath*:properties/appConfig.properties</value> </list> </property> <property name="dataBasePropertyOverride" value="true" /> <property name="dataSource" ref="dataSource"></property> <property name="paramSql" value="select param_code, param_value from app_params order by param_id"></property> <property name="paramKeyColumn" value="param_code"></property> <property name="paramValueColumn" value="param_value"></property> </bean>
public class DataSourceOverridePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { //数据库覆盖properties文件 private boolean dataBasePropertyOverride = false; private DataSource dataSource; private String paramSql; private String paramKeyColumn; private String paramValueColumn; /** * Return a merged Properties instance containing both the * loaded properties and properties set on this FactoryBean. */ protected 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 (Properties localProp : this.localProperties) { CollectionUtils.mergePropertiesIntoMap(localProp, result); } } if (!this.localOverride) { // Load properties from file afterwards, to let those properties override. loadProperties(result); } // Load config property from database if(this.dataBasePropertyOverride){ Properties dbprop = loadAllParamProperties(); CollectionUtils.mergePropertiesIntoMap(dbprop, result); } return result; } protected Properties loadAllParamProperties(){ Properties prop = new Properties(); if(dataBasePropertyOverride){ logger.info("--- launch dataBase config property ----"); validParam(); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); List<Map<String, Object>> list = jdbcTemplate.queryForList(paramSql); for(Map<String, Object> colMap : list){ String key = StringUtils.trimAllWhitespace(colMap.get(paramKeyColumn) != null ? colMap.get(paramKeyColumn).toString() : ""); String value = StringUtils.trimAllWhitespace(colMap.get(paramValueColumn) != null ? colMap.get(paramValueColumn).toString() : ""); prop.put(key, value); logger.info("--- load database param key:["+key+"] value:["+value+"]"); } } return prop; } private void validParam(){ if(dataBasePropertyOverride){ if(dataSource == null){ throw new IllegalArgumentException("DataBase Property Override launch, DataSource is null"); } if(StringUtils.isEmpty(paramSql)){ throw new IllegalArgumentException("DataBase Property Override launch, paramSql is null!"); } if(StringUtils.isEmpty(paramKeyColumn)){ throw new IllegalArgumentException("DataBase Property Override launch, paramKeyColumn is null!"); } if(StringUtils.isEmpty(paramValueColumn)){ throw new IllegalArgumentException("DataBase Property Override launch, paramValueColumn is null!"); } } } public boolean isDataBasePropertyOverride() { return dataBasePropertyOverride; } public void setDataBasePropertyOverride(boolean dataBasePropertyOverride) { this.dataBasePropertyOverride = dataBasePropertyOverride; } public DataSource getDataSource() { return dataSource; } public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public String getParamSql() { return paramSql; } public void setParamSql(String paramSql) { this.paramSql = paramSql; } public String getParamKeyColumn() { return paramKeyColumn; } public void setParamKeyColumn(String paramKeyColumn) { this.paramKeyColumn = paramKeyColumn; } public String getParamValueColumn() { return paramValueColumn; } public void setParamValueColumn(String paramValueColumn) { this.paramValueColumn = paramValueColumn; } }
方法2、在配置文件中使用直接使用spring EL 调用 类的方法 #{class.method} 如: #{serviceManager.getParam('serviceName')}
如:
<bean id="serviceManager" class="com.xxx.commons.helper.ServiceManager" /> <bean name="authenticationFilter" class="org.jasig.cas.client.authentication.AuthenticationFilter"> <property name="casServerLoginUrl" value="#{serviceManager.getParam('casServerLoginUrl')}"></property> <property name="serverName" value="#{serviceManager.getParam('serverName')}" ></property> </bean> <bean name="ticketValidationFilter" class="org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter"> <property name="serverName" value="serviceManager.getParam('serverName')}"></property> <property name="ticketValidator"> <bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator"> <constructor-arg index="0" value="serviceManager.getParam('casServerUrlPrefix')}" /> </bean> </property> </bean>
在serviceManager的getParam 做参数加载