参考:准备Spring Boot的环境
1 核心原理
⭐️1 在SpringBoot的环境准备阶段的后期, 发布一个ApplicationEnvironmentPreparedEvent事件
⭐️2 ConfigFileApplicationListener 监听器监听到这个事件, 使用配置文件加载器(Loader)开始处理这个事件
⭐️3 调用配置加载器,加载配置文件,比如默认的就是application.properties, application 是Spring中定义的, 加载的逻辑大概就是application.xml, application.yaml ,这个源码没仔细看
2 debug源码过程
(1) 加载application.properties 文件?
全文搜索 application.properties
,在 org.springframework.boot.context.config.ConfigFileApplicationListener类的注释中有这个关键字,点开这个类 , 发现实例变量中有 application 字符串
private static final String DEFAULT_NAMES = "application";
继续搜索看看用这个变量的地方, 发现 getSearchNames
方法, 可他是个private方法, 那么就该再看看哪个地方调用了这个方法呢?
只有一个地方:ConfigFileApplicationListener.Loader#load()
方法调用了该方法 , 我们在load() 方法第一行打上断点 🌟🌟🌟🌟🌟 , 启动程序,调用栈如下:
// "配置文件监听器"(ConfigFileApplicationListener) 使用 "配置加载器"(Loader) 加载配置文件内容
load:385, ConfigFileApplicationListener$Loader (o.s.boot.context.config)
addPropertySources:225, ConfigFileApplicationListener (o.s.boot.context.config)
postProcessEnvironment:195, ConfigFileApplicationListener (o.s.boot.context.config)
//-----------------------------------下面为"栈内容1"------------------------------------------
onApplicationEnvironmentPreparedEvent:182, ConfigFileApplicationListener (o.s.boot.context.config)
// ConfigFileApplicationListener(配置文件监听器) 监听了这个事件,开始处理这个事件
onApplicationEvent:168, ConfigFileApplicationListener (o.s.boot.context.config)
doInvokeListener:172, SimpleApplicationEventMulticaster (o.s.context.event)
invokeListener:165, SimpleApplicationEventMulticaster (o.s.context.event)
multicastEvent:139, SimpleApplicationEventMulticaster (o.s.context.event)
multicastEvent:122, SimpleApplicationEventMulticaster (o.s.context.event)
// EventPublishingRunListener.environmentPrepared方法,使用SimpleApplicationEventMulticaster
// 发布一个ApplicationEnvironmentPreparedEvent类型的事件
environmentPrepared:74, EventPublishingRunListener (o.s.boot.context.event)
environmentPrepared:54, SpringApplicationRunListeners (o.s.boot)
prepareEnvironment:325, SpringApplication (o.s.boot)
run:296, SpringApplication (o.s.boot)
main:28, PropertyTestMainSpringApplication (com.yh.stu.springboot.property)
点击调用栈,浏览各个方法的代码 (各个方法的功能注释在上面的调用栈中)
参考:load
参考:postProcessEnvironment
⭐️ 在准备环境阶段(prepareEnvironment)阶段 , "配置文件监听器"中的"配置文件加载器"加载了配置文件
(2)
这里我们发现 load方法调用了PropertySourcesLoader.getPropertySources() 方法
这个方法仅仅返回了一个变量, 那么我们就要搜一些哪里给这个变量赋值的? 我们搜索到 addPropertySource, 他是个private方法, 那么谁调用这个方法? 只有一个load方法, 在该方法上打上断点,🌟🌟🌟🌟🌟重新启动程序,调用栈如下:
// 从配置文件中解析出来的 PropertySource 放入PropertySourcesLoader的变量 propertySources中
load:123, PropertySourcesLoader (o.s.boot.env)
doLoadIntoGroup:490, ConfigFileApplicationListener$Loader (o.s.boot.context.config)
loadIntoGroup:473, ConfigFileApplicationListener$Loader (o.s.boot.context.config)
load:465, ConfigFileApplicationListener$Loader (o.s.boot.context.config)
load:386, ConfigFileApplicationListener$Loader (o.s.boot.context.config)
addPropertySources:225, ConfigFileApplicationListener (o.s.boot.context.config)
postProcessEnvironment:195, ConfigFileApplicationListener (o.s.boot.context.config)
---------------------------------------------------------------------------------------------------
同"栈内容1"
⭐️⭐️⭐️⭐️⭐️
从配置文件中解析出来的 PropertySource 放入PropertySourcesLoader的变量 propertySources中
🌟🌟🌟🌟🌟
那么什么时候有从这里取出propertySources 呢? 我们在PropertySourcesLoader#getPropertySources方法上打上断点, 调用栈如下:
getPropertySources:195, PropertySourcesLoader (o.s.boot.env)
// load方法中调用getPropertySources后获取了配置文件内容,然后调用addConfigurationProperties方法将其放入环境变量
load:393, ConfigFileApplicationListener$Loader (o.s.boot.context.config)
addPropertySources:225, ConfigFileApplicationListener (o.s.boot.context.config)
postProcessEnvironment:195, ConfigFileApplicationListener (o.s.boot.context.config)
---------------------------------------------------------------------------------------------------
同"栈内容1"
load:393
方法中调用getPropertySources后获取了配置文件内容,然后调用addConfigurationProperties方法将其放入环境变量中
addConfigurationProperties:669, ConfigFileApplicationListener$Loader (o.s.boot.context.config)
addConfigurationProperties:660, ConfigFileApplicationListener$Loader (o.s.boot.context.config)
load:393, ConfigFileApplicationListener$Loader (o.s.boot.context.config)
addPropertySources:225, ConfigFileApplicationListener (o.s.boot.context.config)
postProcessEnvironment:195, ConfigFileApplicationListener (o.s.boot.context.config)
---------------------------------------------------------------------------------------------------
同"栈内容1"
☕️方法addConfigurationProperties:669
参考:addConfigurationProperties
搜索配置文件的地方
getSearchLocations() = {LinkedHashSet@2087} size = 4
0 = "file:./config/"
1 = "file:./"
2 = "classpath:/config/"
3 = "classpath:/"
(2) 取出配置文件中的值
idea可以从 application.properties文件的server.port
链接到 o.s.b.autoconfigure.web.ServerProperties#setPort
方法 ,在方法上打上断点 ,从而可以看看:🌟🌟🌟🌟🌟
1 在哪个阶段进行赋值操作的?
2 从哪里取到配置文件的值的?
调用栈如下, 我们从上到下追踪port值的最终来源, 最终定位在 doBindPropertiesToTarget:272
setPort:294, ServerProperties (o.s.boot.autoconfigure.web)
invoke0:-1, NativeMethodAccessorImpl (sun.reflect)
invoke:62, NativeMethodAccessorImpl (sun.reflect)
invoke:43, DelegatingMethodAccessorImpl (sun.reflect)
invoke:498, Method (java.lang.reflect)
setValue:358, BeanWrapperImpl$BeanPropertyHandler (o.s.beans)
processLocalProperty:467, AbstractNestablePropertyAccessor (o.s.beans)
setPropertyValue:290, AbstractNestablePropertyAccessor (o.s.beans)
setPropertyValue:278, AbstractNestablePropertyAccessor (o.s.beans)
setPropertyValue:699, RelaxedDataBinder$RelaxedBeanWrapper (o.s.boot.bind)
setPropertyValues:95, AbstractPropertyAccessor (o.s.beans)
applyPropertyValues:859, DataBinder (o.s.validation)
doBind:755, DataBinder (o.s.validation)
doBind:128, RelaxedDataBinder (o.s.boot.bind)
bind:740, DataBinder (o.s.validation)
// 在这里getPropertySourcesPropertyValues取到配置文件的值, 我们需要进入该方法
doBindPropertiesToTarget:272, PropertiesConfigurationFactory (o.s.boot.bind)
bindPropertiesToTarget:240, PropertiesConfigurationFactory (o.s.boot.bind)
----------------------------------------下面为"栈内容2"---------------------------------------------------------
postProcessBeforeInitialization:330, ConfigurationPropertiesBindingPostProcessor (o.s.boot.context.properties)
postProcessBeforeInitialization:292, ConfigurationPropertiesBindingPostProcessor (o.s.boot.context.properties)
applyBeanPostProcessorsBeforeInitialization:409, AbstractAutowireCapableBeanFactory (o.s.beans.factory.support)
initializeBean:1620, AbstractAutowireCapableBeanFactory (o.s.beans.factory.support)
doCreateBean:555, AbstractAutowireCapableBeanFactory (o.s.beans.factory.support)
createBean:483, AbstractAutowireCapableBeanFactory (o.s.beans.factory.support)
getObject:306, AbstractBeanFactory$1 (o.s.beans.factory.support)
getSingleton:230, DefaultSingletonBeanRegistry (o.s.beans.factory.support)
doGetBean:302, AbstractBeanFactory (o.s.beans.factory.support)
getBean:202, AbstractBeanFactory (o.s.beans.factory.support)
getBeansOfType:519, DefaultListableBeanFactory (o.s.beans.factory.support)
getCustomizers:82, EmbeddedServletContainerCustomizerBeanPostProcessor (o.s.boot.context.embedded)
postProcessBeforeInitialization:72, EmbeddedServletContainerCustomizerBeanPostProcessor (o.s.boot.context.embedded)
postProcessBeforeInitialization:59, EmbeddedServletContainerCustomizerBeanPostProcessor (o.s.boot.context.embedded)
applyBeanPostProcessorsBeforeInitialization:409, AbstractAutowireCapableBeanFactory (o.s.beans.factory.support)
initializeBean:1620, AbstractAutowireCapableBeanFactory (o.s.beans.factory.support)
doCreateBean:555, AbstractAutowireCapableBeanFactory (o.s.beans.factory.support)
createBean:483, AbstractAutowireCapableBeanFactory (o.s.beans.factory.support)
getObject:306, AbstractBeanFactory$1 (o.s.beans.factory.support)
getSingleton:230, DefaultSingletonBeanRegistry (o.s.beans.factory.support)
doGetBean:302, AbstractBeanFactory (o.s.beans.factory.support)
getBean:202, AbstractBeanFactory (o.s.beans.factory.support)
getEmbeddedServletContainerFactory:199, EmbeddedWebApplicationContext (o.s.boot.context.embedded)
createEmbeddedServletContainer:162, EmbeddedWebApplicationContext (o.s.boot.context.embedded)
onRefresh:134, EmbeddedWebApplicationContext (o.s.boot.context.embedded)
refresh:537, AbstractApplicationContext (o.s.context.support)
refresh:122, EmbeddedWebApplicationContext (o.s.boot.context.embedded)
refresh:693, SpringApplication (o.s.boot)
refreshContext:360, SpringApplication (o.s.boot)
run:303, SpringApplication (o.s.boot)
main:28, PropertyTestMainSpringApplication (com.yh.stu.springboot.property)
进入getPropertySourcesPropertyValues,一路追踪,发现 getProperty
方法中使用的 propertySources
是就是配置文件的值
getProperty:82, PropertySourcesPropertyResolver (o.s.core.env)
getProperty:66, PropertySourcesPropertyResolver (o.s.core.env)
getEnumerableProperty:159, PropertySourcesPropertyValues (o.s.boot.bind)
processEnumerablePropertySource:148, PropertySourcesPropertyValues (o.s.boot.bind)
processPropertySource:127, PropertySourcesPropertyValues (o.s.boot.bind)
<init>:117, PropertySourcesPropertyValues (o.s.boot.bind)
getPropertySourcesPropertyValues:316, PropertiesConfigurationFactory (o.s.boot.bind)
doBindPropertiesToTarget:270, PropertiesConfigurationFactory (o.s.boot.bind)
bindPropertiesToTarget:240, PropertiesConfigurationFactory (o.s.boot.bind)
-------------------------------------------------------------------------------------------------
同"栈内容2"
o.s.core.env.PropertySourcesPropertyResolver#propertySources是个实例变量
而给该变量赋值的只有PropertySourcesPropertyResolver的构造方法 , 那么我们看看现在所处的PropertySourcesPropertyResolver 对象是在哪里实例化的,我们翻看上面的调用栈 ,发现在 <init>:117, PropertySourcesPropertyValues
里面实例化了 PropertySourcesPropertyResolver 对象
然后顺着构造方法的参数propertySources 沿着调用栈找,PropertiesConfigurationFactory
类的 getPropertySourcesPropertyValues:316
方法中创建的 PropertySourcesPropertyValues
对象, 而参数是个实例变量 propertySources
此时又要查找给其赋值的地方,只有一个PropertiesConfigurationFactory.setPropertySources
, 在方法上打上断点,调用栈如下:
🌟🌟🌟🌟🌟
setPropertySources:166, PropertiesConfigurationFactory (o.s.boot.bind)
// 方法中将environment.getPropertySources()结果作为参数调用setPropertySources方法
// 这里就是从环境(environment)中取出配置文件的内容,然后放入
bindToSpringApplication:239, ConfigFileApplicationListener (o.s.boot.context.config)
postProcessEnvironment:197, ConfigFileApplicationListener (o.s.boot.context.config)
-------------------------------------------------------------------------------------------------
同"栈内容1"....
到此我们已经找到了从哪里取的值了.
取值总结
ConfigFileApplicationListener 中
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
// 行号195: 读配置文件放入environment
addPropertySources(environment, application.getResourceLoader());
configureIgnoreBeanInfo(environment);
// 行号197: 从environment取配置文件内容
bindToSpringApplication(environment, application);
}