<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 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    2017年12月8日

    在Spring cloud config出來之前, 自己實現(xiàn)了基于ZK的配置中心, 杜絕了本地properties配置文件, 原理很簡單, 只是重載了PropertyPlaceholderConfigurer的mergeProperties():

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

    這個實現(xiàn)在spring項目里用起來還是挺順手的, 但是近期部分spring-boot項目里發(fā)現(xiàn)這種placeholder的實現(xiàn)跟spring boot的@ConfigurationProperties(prefix = "xxx") 不能很好的配合工作,
    也就是屬性沒有被resolve處理, 用@Value的方式確可以讀到, 但是@Value配置起來如果屬性多的話還是挺繁瑣的, 還是傾向用@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)其會檢查Java system propeties里的屬性, 也就是說, 只要把mergerProperties讀到的屬性寫入Java system props里即可, 看了下源碼, 找到個切入點

    /**
    * 重載處理屬性實現(xiàn)
    * 根據(jù)選項, 決定是否將合并后的props寫入系統(tǒng)屬性, Spring boot需要
    *
    * @param beanFactoryToProcess
    * @param props 合并后的屬性
    * @throws BeansException
    */
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
    // 原有邏輯
    super.processProperties(beanFactoryToProcess, props);
    // 寫入到系統(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);
    }
    }
    }
    為避免影響過大, 設(shè)置了個開關(guān), 是否寫入系統(tǒng)屬性, 如果是spring boot的項目, 就開啟, 這樣對線上非spring boot項目做到影響最小, 然后spring boot的@ConfigurationProperties完美讀到屬性;

    具體代碼見: 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;
    }

    posted @ 2017-12-08 14:13 Milo的海域 閱讀(909) | 評論 (0)編輯 收藏

    主站蜘蛛池模板: 一级做a爰片久久毛片免费看| 国产成人亚洲综合一区| 麻豆91免费视频| 高清国语自产拍免费视频国产| 亚洲人成人77777在线播放| 亚洲网站免费观看| 亚洲精品美女视频| 成人免费在线看片| 亚洲精品一二三区| 在线视频免费国产成人| 黄色三级三级免费看| 伊在人亚洲香蕉精品区麻豆| 一区二区三区AV高清免费波多| 久久精品亚洲男人的天堂| 成人精品一区二区三区不卡免费看| 国产亚洲一区二区三区在线观看| 最新亚洲成av人免费看| 亚洲国产美国国产综合一区二区| 2019中文字幕在线电影免费 | 美女视频黄a视频全免费网站色窝| 精品亚洲永久免费精品| 日韩免费无码视频一区二区三区| 亚洲婷婷综合色高清在线| 免费观看毛片视频| 久久免费香蕉视频| 久久亚洲精品成人无码网站| 天天看片天天爽_免费播放| 深夜a级毛片免费无码| 久久精品国产亚洲香蕉| 中文字幕人成无码免费视频| 青青视频免费在线| 久久亚洲AV成人出白浆无码国产| 岛国大片免费在线观看| a在线视频免费观看在线视频三区| 亚洲欧洲日韩国产综合在线二区| 免费看国产成年无码AV片| 日本高清不卡中文字幕免费| 亚洲色图综合网站| 免费人妻av无码专区| 无码国产精品一区二区免费式芒果| 亚洲欧好州第一的日产suv|