Spring boot外部配置(配置中心化)详解
作者:BlogJava-专家区 发布时间:2022-07-11 23:13:26
前言
在项目中为了灵活配置,我们常采用配置文件,常见的配置文件就比如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;
}
来源:http://www.blogjava.net/miaoyachun/archive/2017/12/08/432940.html


猜你喜欢
- Spring课程工程构建 在IDEA 工作空间里面创建Spring文件夹。打开IDEA,file–>Open&am
- 该方法把该字符串转换成一个新的字符数组。 String str="abcdefg"; char a[]; a=str.t
- tomcat中文乱码问题这几天测试的兄弟发现了项目中存在乱码问题 经过排查发现是tomcat中的问题 于是在server.xml中添加了如下
- 【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1
- 背景朋友想从XX超市app购买一些物美价廉的东西,但是因为人多货少经常都是缺货的状态,订阅了到货通知也没什么效果,每次收到短信通知进入app
- public class MainActivity extends Activity { @Override protected void
- 从一个Stream中过滤null值复习一个Stream 包含 null 数据的例子.Java8Examples.javapackage co
- 摘要在使用java做后台站点的开发张,图表和报表功能都是不可或缺 的。本文推荐了8款最精彩实用的Java图表应用,大部分图表应用的功能都类似
- C#反射技术主要基于System.Type类和System.Reflection.Assemble类,通过Type类可以访问关于任何数据类型
- 具体不做详细介绍了,直接上代码/// <summary> /// 功能:FileStream文件流读取文件 &nbs
- 如题,记录一些平常开发用的pom文件细节1.使用parent父类引用,解决依赖版本号不确定时自动匹配的问题<parent> &n
- Android ListView仿聊天界面效果图的具体代码,供大家参考,具体内容如下1.首先页面总布局(ListView + LinearL
- 环境springboot1.5.9完整代码,内有sql,先建库,在运行sql建表,sql中已插入测试的数据。https://github.c
- Apache Shiro官网:https://shiro.apache.org/spring-boot.html.一、依据官网快速搭建Qui
- JVM内部结构图Java虚拟机主要分为五个区域:方法区、堆、Java栈、PC寄存器、本地方法栈。下面来看一些关于JVM结构的重要问题。1.哪
- Java 如何将String转化为Int在 Java 中要将 String 类型转化为 int 类型时,需要使用 Integer 类中的 p
- 目录主要功能介绍客户端main方法入口主窗体代码更新帮助类版本xml文件解析服务端版本xml文件自动升级服务Controller版本文件自动
- 面试中可能会被问到为什么我们调用start()方法时会执行run()方法,为什么我们不能直接调用run()方法?Java 创建线程的方法实际
- 一、题目给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑
- 先上代码,再来分析public class FileDownloadList {/**上下文*/ private Context mCont