<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 88, comments - 3, trackbacks - 0, articles - 0
      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    Spring boot外部配置-配置中心化

    Posted on 2017-12-08 14:13 Milo的海域 閱讀(898) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Java
    在Spring cloud config出來(lái)之前, 自己實(shí)現(xiàn)了基于ZK的配置中心, 杜絕了本地properties配置文件, 原理很簡(jiǎn)單, 只是重載了PropertyPlaceholderConfigurer的mergeProperties():

    /**
    * 重載合并屬性實(shí)現(xiàn)
    * 先加載file properties, 然后并入ZK配置中心讀取的properties
    *
    * @return 合并后的屬性集合
    * @throws IOException 異常
    */
    @Override
    protected Properties mergeProperties() throws IOException {
    Properties result = new Properties();
    // 加載父類(lèi)的配置
    Properties mergeProperties = super.mergeProperties();
    result.putAll(mergeProperties);
    // 加載從zk中讀取到的配置
    Map<String, String> configs = loadZkConfigs();
    result.putAll(configs);
    return result;
    }

    這個(gè)實(shí)現(xiàn)在spring項(xiàng)目里用起來(lái)還是挺順手的, 但是近期部分spring-boot項(xiàng)目里發(fā)現(xiàn)這種placeholder的實(shí)現(xiàn)跟spring boot的@ConfigurationProperties(prefix = "xxx") 不能很好的配合工作,
    也就是屬性沒(méi)有被resolve處理, 用@Value的方式確可以讀到, 但是@Value配置起來(lái)如果屬性多的話(huà)還是挺繁瑣的, 還是傾向用@ConfigurationProperties的prefix, 于是看了下spring boot的文檔發(fā)現(xiàn)PropertySource order:
       * Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
       * @TestPropertySource annotations on your tests.
       * @SpringBootTest#properties annotation attribute on your tests.
       * Command line arguments.
       * Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property)
       * ServletConfig init parameters.
       * ServletContext init parameters.
       * JNDI attributes from java:comp/env.
       * Java System properties (System.getProperties()).
       * OS environment variables.
       * A RandomValuePropertySource that only has properties in random.*.
       * Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
       * Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)
       * Application properties outside of your packaged jar (application.properties and YAML variants).
       * Application properties packaged inside your jar (application.properties and YAML variants).
       * @PropertySource annotations on your @Configuration classes.
       * Default properties (specified using SpringApplication.setDefaultProperties).
    不難發(fā)現(xiàn)其會(huì)檢查Java system propeties里的屬性, 也就是說(shuō), 只要把mergerProperties讀到的屬性寫(xiě)入Java system props里即可, 看了下源碼, 找到個(gè)切入點(diǎn)

    /**
    * 重載處理屬性實(shí)現(xiàn)
    * 根據(jù)選項(xiàng), 決定是否將合并后的props寫(xiě)入系統(tǒng)屬性, Spring boot需要
    *
    * @param beanFactoryToProcess
    * @param props 合并后的屬性
    * @throws BeansException
    */
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
    // 原有邏輯
    super.processProperties(beanFactoryToProcess, props);
    // 寫(xiě)入到系統(tǒng)屬性
    if (writePropsToSystem) {
    // write all properties to system for spring boot
    Enumeration<?> propertyNames = props.propertyNames();
    while (propertyNames.hasMoreElements()) {
    String propertyName = (String) propertyNames.nextElement();
    String propertyValue = props.getProperty(propertyName);
    System.setProperty(propertyName, propertyValue);
    }
    }
    }
    為避免影響過(guò)大, 設(shè)置了個(gè)開(kāi)關(guān), 是否寫(xiě)入系統(tǒng)屬性, 如果是spring boot的項(xiàng)目, 就開(kāi)啟, 這樣對(duì)線(xiàn)上非spring boot項(xiàng)目做到影響最小, 然后spring boot的@ConfigurationProperties完美讀到屬性;

    具體代碼見(jiàn): org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
    throws BeansException {
    ConfigurationProperties annotation = AnnotationUtils
    .findAnnotation(bean.getClass(), ConfigurationProperties.class);
    if (annotation != null) {
    postProcessBeforeInitialization(bean, beanName, annotation);
    }
    annotation = this.beans.findFactoryAnnotation(beanName,
    ConfigurationProperties.class);
    if (annotation != null) {
    postProcessBeforeInitialization(bean, beanName, annotation);
    }
    return bean;
    }

    主站蜘蛛池模板: 永久免费毛片手机版在线看| 香蕉成人免费看片视频app下载| 亚洲国产成人精品久久久国产成人一区二区三区综 | 四虎永久在线精品免费网址 | 亚洲熟妇AV一区二区三区宅男| 国产最新凸凹视频免费| 美女啪啪网站又黄又免费| 亚洲高清专区日韩精品| 无码人妻久久一区二区三区免费丨| 色五月五月丁香亚洲综合网| 亚洲日韩精品一区二区三区| 91免费播放人人爽人人快乐| 老湿机一区午夜精品免费福利 | 亚洲AV无码成人精品区天堂| 成年女人永久免费观看片| 成人性做爰aaa片免费看| 亚洲一卡2卡三卡4卡无卡下载 | 成a人片亚洲日本久久| 亚洲成亚洲乱码一二三四区软件| 成人毛片免费视频| 免费成人在线视频观看| 久久亚洲欧美国产精品| 久久久婷婷五月亚洲97号色| 免费国产成人高清视频网站| 亚州免费一级毛片| 国产黄片不卡免费| 亚洲av中文无码字幕色不卡| 亚洲人成依人成综合网| 亚洲精品国产精品乱码不卞| 成年女人色毛片免费看| 无码人妻一区二区三区免费n鬼沢| 色吊丝免费观看网站| 国产成人精品日本亚洲直接| 亚洲成a人片77777老司机| 亚洲AV中文无码乱人伦| 免费观看无遮挡www的小视频| 在线观看片免费人成视频播放| 美女无遮挡免费视频网站| 亚洲国产综合精品中文第一| 亚洲午夜未满十八勿入| 亚洲日韩欧洲无码av夜夜摸|