Springデータベースから設定データをロードする


Springデータベースから設定データをロードする
springプロファイルのロードは、データベースにパラメータファイルを配置するサポートを追加しました.このように環境関連のパラメータをデータに配置して、同じコードパッケージを解決しました.設定ファイルを変更せずに、任意の環境で実行できます.
方法1、編纂類継承org.springframe ewark.beans.factory.co.fig.ProptyPlaceholderConfigrerによるmergPropertiesを実現し、この方法には常にデータベースからパラメータコードを読み込む方法を追加します.
例えば:  参照しましたhttp://www.hidehai.com/html/y2012/776.html DataSourceOverridePropertyPlaceholder Configrerを実現しました.
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を直接使用してクラスを呼び出す方法〓〓〓(「serviceManager.get Param」)
例えば:
<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でパラメータをロードします.