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

 更新时间:2017年12月09日 09:48:57   作者:BlogJava-专家区  
这篇文章主要给大家介绍了关于Spring boot外部配置(配置中心化)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

前言

在项目中为了灵活配置,我们常采用配置文件,常见的配置文件就比如xml和properties,springboot允许使用properties和yaml文件作为外部配置。现在编译器对于yaml语言的支持还不够好,目前还是使用properties文件作为外部配置。

在Spring cloud config出来之前, 自己实现了基于ZK的配置中心, 杜绝了本地properties配置文件, 原理很简单, 只是重载了PropertyPlaceholderConfigurer的mergeProperties() :

/**
 * 重载合并属性实现
 * 先加载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项目里发现这种placeholder的实现跟spring boot的@ConfigurationProperties(prefix = "xxx") 不能很好的配合工作,

也就是属性没有被resolve处理, 用@Value的方式确可以读到, 但是@Value配置起来如果属性多的话还是挺繁琐的, 还是倾向用@ConfigurationProperties的prefix, 于是看了下spring boot的文档发现 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).

不难发现其会检查Java system propeties里的属性, 也就是说, 只要把mergerProperties读到的属性写入Java system props里即可, 看了下源码, 找到个切入点

/**
 * 重载处理属性实现
 * 根据选项, 决定是否将合并后的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

@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;
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

相关文章

  • Java程序去调用并执行shell脚本及问题总结(推荐)

    Java程序去调用并执行shell脚本及问题总结(推荐)

    这篇文章主要介绍了Java程序去调用并执行shell脚本及问题总结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • Java String类详解_动力节点Java学院整理

    Java String类详解_动力节点Java学院整理

    这篇文章主要介绍了Java String类详解,本文经多方资料的收集整理和归纳,最终撰写成文,非常不错,值得收藏,需要的的朋友参考下
    2017-04-04
  • 解决Java字符串JSON转换异常:cn.hutool.json.JSONException: Mismatched hr and body

    解决Java字符串JSON转换异常:cn.hutool.json.JSONException: Mismatched 

    这篇文章主要给大家介绍了关于如何解决Java字符串JSON转换异常:cn.hutool.json.JSONException: Mismatched hr and body的相关资料,文中将解决的办法通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • 详解Java设计模式之桥接模式

    详解Java设计模式之桥接模式

    桥接,顾名思义,就是用来连接两个部分,使得两个部分可以互相通讯。桥接模式将系统的抽象部分与实现部分分离解耦,使他们可以独立的变化。本文通过示例详细介绍了桥接模式的原理与使用,需要的可以参考一下
    2022-06-06
  • Java中Runnable与Callable接口的区别详解

    Java中Runnable与Callable接口的区别详解

    这篇文章主要为大家详细介绍了Java中Runnable与Callable接口的区别,文中的示例代码讲解详细,对我们学习Java有一定的帮助,需要的可以参考一下
    2023-03-03
  • 浅析Java异常处理中断言的使用

    浅析Java异常处理中断言的使用

    这篇文章主要介绍了Java异常处理中断言的使用,是Java入门学习中的基础知识,需要的朋友可以参考下
    2015-09-09
  • mybatis-plus使用问题小结

    mybatis-plus使用问题小结

    这篇文章主要介绍了mybatis-plus使用问题汇总,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • Java 8系列之Stream中万能的reduce用法说明

    Java 8系列之Stream中万能的reduce用法说明

    这篇文章主要介绍了Java 8系列之Stream中万能的reduce用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • Java中如何利用Set判断List集合中是否有重复元素

    Java中如何利用Set判断List集合中是否有重复元素

    在开发工作中,我们有时需要去判断List集合中是否含有重复的元素,这时候我们不需要找出重复的元素,我们只需要返回一个 Boolean 类型就可以了,下面通过本文给大家介绍Java中利用Set判断List集合中是否有重复元素,需要的朋友可以参考下
    2023-05-05
  • Java的Comparable,Comparator和Cloneable三大接口详解

    Java的Comparable,Comparator和Cloneable三大接口详解

    这篇文章主要为大家详细介绍了Java的Comparable,Comparator和Cloneable的接口,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03

最新评论