一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring boot外部配置(配置中心化)詳解

Spring boot外部配置(配置中心化)詳解

2021-03-02 10:55BlogJava-專家區(qū) Java教程

這篇文章主要給大家介紹了關于Spring boot外部配置(配置中心化)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。

前言

在項目中為了靈活配置,我們常采用配置文件,常見的配置文件就比如xml和properties,springboot允許使用properties和yaml文件作為外部配置。現在編譯器對于yaml語言的支持還不夠好,目前還是使用properties文件作為外部配置。

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * 重載合并屬性實現
 * 先加載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;
}

這個實現在spring項目里用起來還是挺順手的, 但是近期部分spring-boot項目里發(fā)現這種placeholder的實現跟spring boot的@ConfigurationProperties(prefix = "xxx") 不能很好的配合工作,

也就是屬性沒有被resolve處理, 用@Value的方式確可以讀到, 但是@Value配置起來如果屬性多的話還是挺繁瑣的, 還是傾向用@ConfigurationProperties的prefix, 于是看了下spring boot的文檔發(fā)現 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ā)現其會檢查Java system propeties里的屬性, 也就是說, 只要把mergerProperties讀到的屬性寫入Java system props里即可, 看了下源碼, 找到個切入點

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * 重載處理屬性實現
 * 根據選項, 決定是否將合并后的props寫入系統屬性, Spring boot需要
 *
 * @param beanFactoryToProcess
 * @param props    合并后的屬性
 * @throws BeansException
 */
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
 // 原有邏輯
  super.processProperties(beanFactoryToProcess, props);
 // 寫入到系統屬性
 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);
  }
 }
}

為避免影響過大, 設置了個開關, 是否寫入系統屬性, 如果是spring boot的項目, 就開啟, 這樣對線上非spring boot項目做到影響最小, 然后spring boot的@ConfigurationProperties完美讀到屬性;

具體代碼見: org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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;
}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://www.blogjava.net/miaoyachun/archive/2017/12/08/432940.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国语自产拍在线播放不卡 | 国产成人盗摄精品 | 精品四虎 | 欧美成a人片免费看久久 | 亚洲免费高清视频 | 国产香蕉一区二区在线网站 | 国产亚洲高清国产拍精品 | 亚洲瑟瑟网 | dasd817黑人在线播放 | 欧美一级艳片视频免费观看 | 国产小视频在线免费观看 | 18日本人 | 国产成人综合视频 | 91交换论坛 | 国模李丽莎大尺度啪啪 | 国产靠逼视频 | 四虎影视永久在线 | 欧美日韩1区2区 | 久久免费看少妇高潮A片特爽 | 欧美精品超清在线播放 | 精品小视频在线观看 | 美女被躁了在线观看视频 | 欧产日产国产精品专区 | 亚洲一区二区三区深夜天堂 | 国产探花视频 | 欧美伊人久久久久久久久影院 | 骚虎tv| 久久这里都是精品 | 青草青青在线视频 | 污斗罗大陆 | 高肉h护士办公室play | 高h射尿 | 欧美日本一道高清免费3区 欧美人做人爱a全程免费 | 3d动漫美女被吸乳羞羞视频 | 亚洲精品国产成人中文 | 亚洲波多野结衣日韩在线 | 国产精品青青青高清在线密亚 | 国产亚洲sss在线播放 | 四虎影视4hutv最新地址在线 | 日韩视频一区二区三区 | 国产精品第2页 |