写在文章前
问题1:
假如我的spring boot项目配置了 application.yml,application-dev.yml,application.yml 三个文件分别配置服务端口为8080,8081,8082
spring:
profiles:
active: pro,dev
spring:
profiles:
active: dev,pro
问上面这两个配置启动后服务的端口是什么?
问题2:
有一个依赖的jar中采用的Properties进行取值的,而且写死了文件路径classpath:…
但是有一个init初始化方法,想将其改造为支持application.yml取值的并且优先级是
application.yml>target.properties
Spring Boot 版本
因为网上好多讲这部分的都没有列出spring boot版本,导致大家不能debug源码,遂标出版本号
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
大致流程
1.项目通过run方法启动
2.找到核心位置
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
3.仔细阅读
4.enviroment创建完毕后会执行的事件
ApplicationPreparedEvent
扩展
核心是了解Spring的事件顺序,根据事件进行切入
开始扩展
1.切入EnvironmentPostProcessor 设置Order,设置为在application.yml前还是后
2.监听Spring事件,我大致看了下代码,在Environment准备完毕后会进行ApplicationPreparedEvent事件遂对其进行切入
3.学习Environment的getProperty处理流程
4.MutablePropertySources加载资源流程
注意点
事件需要在META-INF/spring.factories进行注册
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.context.ApplicationListener=\
因项目涉及到公司就不放出来了,有问题发留言即可。