package com.gxx.record_app.config; import java.io.File; import java.util.Properties; import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.json.JSONArray; import org.json.JSONObject; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import com.gxx.record_app.utils.HttpClientUtils; import com.gxx.record_app.utils.PropertyUtil; /** * *
*
Title:
*
* none *
*
Description:自建属性文件读取子类BEAN 构造方法读取配置中心
*
*

none *

*
* * @author Gxx * @version 1.0, 2015年12月30日 * @since record_app * */ public class MinePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { /** * 日志处理器 */ private final Logger logger = Logger.getLogger(MinePropertyPlaceholderConfigurer.class); /** * 本地配置文件位置 */ private static final String CONFIG_FILE_LOCATION = "CONFIG_FILE_LOCATION"; /** * 配置中心地址 */ private static final String CONFIG_CENTER_URL = "config.center.url"; /** * 构造方法 */ public MinePropertyPlaceholderConfigurer(){ /** * 读取配置 */ readConfig(); } /** * 读取配置 */ public void readConfig(){ try{ /** * 1.读取配置文件位置 */ String configFileLocation = PropertyUtil.getInstance().getProperty(CONFIG_FILE_LOCATION); String configFile = StringUtils.EMPTY; for(String location : configFileLocation.split(",")){ File file = new File(location); if(file.exists()){ configFile = location; break; } } logger.info("本地配置文件路径:" + configFile); /** * 2.找不到配置文件报错 */ if(StringUtils.isBlank(configFile)){ throw new RuntimeException("该环境不存在配置文件"); } /** * 3.载入本地环境中的配置文件 */ logger.info("载入本地环境中的配置文件"); PropertyUtil.getInstance().refreshRealPath(configFile); /** * 4.读取配置中心地址 */ String configCenterUrl = PropertyUtil.getInstance().getProperty(CONFIG_CENTER_URL); logger.info("读取配置中心地址:" + configCenterUrl); /** * 5.从配置中心读取配置 */ String content = HttpClientUtils.getWebContentByGet(configCenterUrl); logger.info("从配置中心读取配置,返回内容:" + content); /** * 6.json解析 */ JSONObject jsonObject = new JSONObject(content); logger.info("contentMap:isSuccess=" + (jsonObject.getBoolean("isSuccess")?"Y":"N") + ",message=" + jsonObject.getString("message")); /** * 7.判断是否读取配置成功 */ logger.info("判断是否读取配置成功:[" + (jsonObject.getBoolean("isSuccess")?"成功":"失败") + "]"); if(!jsonObject.getBoolean("isSuccess")){ throw new RuntimeException("读取配置中心配置失败!"); } /** * 8.将配置中心读取的配置塞入本地Properties,如果本地配置文件有该配置则以本地优先级高,再把本地Properties塞入System properties */ logger.info("将配置中心读取的配置塞入本地Properties,如果本地配置文件有该配置则以本地优先级高,再把本地Properties塞入System properties。"); JSONArray jsonArray = new JSONArray(jsonObject.getString("list")); for(int index = 0, length = jsonArray.length(); index < length; index++) { JSONObject tempObject = jsonArray.getJSONObject(index); logger.info("id=" + tempObject.getLong("id") + ",env=" + tempObject.getString("env") + ",project=" + tempObject.getString("project") + ",name=" + tempObject.getString("name") + ",value=" + tempObject.getString("value") + ",remark=" + tempObject.getString("remark")); String name = tempObject.getString("name"); String localValue = PropertyUtil.getInstance().getProperty(name); if(localValue == null){ localValue = tempObject.getString("value"); PropertyUtil.getInstance().setProperty(name, localValue); } else { logger.info("本地已经配置,优先级较高:" + tempObject.getString("name") + "=" + localValue); } } Properties properties = PropertyUtil.getInstance().getProperties(); Set set = properties.keySet(); for(Object key : set){ String value = properties.getProperty((String)key); System.setProperty((String)key, value); logger.info("设置System Property[" + key + "]=[" + value + "]"); } } catch (Exception ex){ logger.error("异常发生!", ex); } } }