加载数据库配置信息到spring容器中

package com.sf.wop.common.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

public class PropertyPlaceholderConfigurerBD extends PropertyPlaceholderConfigurer {
	private static final Logger logger = LoggerFactory.getLogger(PropertyPlaceholderConfigurerBD.class);

	private JdbcTemplate jdbcTemplate;

	@Transactional
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		try {
			Properties mergedProps = loadIKafkaProp();

			if (mergedProps == null || mergedProps.size() == 0) {
				mergedProps = mergeProperties();
			}
			mergedProps.putAll(loadQuartzProp());
			mergedProps.putAll(loadSystemConfig());
			
			convertProperties(mergedProps);
			processProperties(beanFactory, mergedProps);

			Properties prop = System.getProperties();
			prop.putAll(mergedProps);

		} catch (Exception ex) {
			throw new BeanInitializationException("Could not load properties", ex);
		}
	}

	/**
	 * @see 从数据库加载配置项
	 **/
	private Properties loadIKafkaProp() {
		Properties properties = new Properties();
		List<Map<String, Object>> result = jdbcTemplate
				.queryForList("select * from tb_kafka_config where is_delete = 0 ");
		if (result == null || result.size() == 0) {
			logger.error("no property found.");
			return properties;
		}
		logger.info(" query KafkaConfig  OK !");

		Map<String, String> map = new HashMap<String, String>();

		for (Map<String, Object> record : result) {
			String paramkey = String.valueOf(record.get("kafka_key"));
			map.put(paramkey + ".url", String.valueOf(record.get("url")));
			map.put(paramkey + ".clusterName", String.valueOf(record.get("cluster_name")));
			map.put(paramkey + ".topic", String.valueOf(record.get("topic")));
			map.put(paramkey + ".topicToken", String.valueOf(record.get("topic_token")));
			map.put(paramkey + ".threadCount", String.valueOf(record.get("thread_count")));
			if (null != record.get("fetch_data_queue_size")) {
				map.put(paramkey + ".fetchDataQueueSize", String.valueOf(record.get("fetch_data_queue_size")));
			}
			if (null != record.get("message_group_size")) {
				map.put(paramkey + ".messageGroupSize", String.valueOf(record.get("message_group_size")));
			}
			if (null != record.get("pack_data_queue_size")) {
				map.put(paramkey + ".packDataQueueSize", String.valueOf(record.get("pack_data_queue_size")));
			}
		}
		properties.putAll(map);
		return properties;
	}

	/**
	 * @see 从数据库加载quartz CronExpression表达式
	 **/
	private Properties loadQuartzProp() {
		Properties properties = new Properties();
		List<Map<String, Object>> result = jdbcTemplate.queryForList("select * from tb_quartz_cronexpression_config");
		if (result == null || result.size() == 0) {
			logger.error("no QuartzConfig property found.");
			return properties;
		}
		logger.info("query QuartzConfig OK !");

		for (Map<String, Object> record : result) {
			String triggerName = String.valueOf(record.get("trigger_name"));
			properties.put(triggerName + ".cronExpression", String.valueOf(record.get("cron_expression")));
		}
		return properties;
	}

	@SuppressWarnings("null")
	private Properties loadSystemConfig() {
		Properties properties = new Properties();
		List<Map<String, Object>> list = jdbcTemplate
				.queryForList("select t.config_code,t.config_value from ts_system_config t where t.`status`=1");
		if (list != null && list.size() > 0) {
			for (Map<String, Object> map : list) {
				properties.put(map.get("config_code"), map.get("config_value"));
			}
		}

		return properties;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
}

猜你喜欢

转载自wddpwzzhao123.iteye.com/blog/2320184
今日推荐