SpringBoot-ConfigurationProperties
SpringBoot ConfigurationProperties
-
Author: HuiFer
-
源码阅读仓库: SourceHot-spring-boot
-
本文主要对
org.springframework.boot.context.properties.ConfigurationProperties
进行分析
ConfigurationProperties
- 顶部注释
|
|
看到ConfigurationPropertiesScan
去看看这个
ConfigurationPropertiesScan
|
|
- 熟悉的Import注解
ConfigurationPropertiesScanRegistrar
- debug 没有抓到后续补充
EnableConfigurationProperties
|
|
EnableConfigurationPropertiesRegistrar
-
该类会读取spring.factories
-
中
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
这样的org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletConfiguration org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration
|
|
registerInfrastructureBeans
|
|
- 此处操作逻辑基本相同,是否存在这个 beanName 存在直接注册,不存在补充
ConfigurationPropertiesBindingPostProcessor.register(registry)
|
|
ConfigurationPropertiesBeanDefinitionValidator.register(registry)
|
|
getTypes(metadata).forEach(beanRegistrar::register)
- 先看输入参数 metadata
- getTypes 结果
-
源码开始,先找出刚才的对象
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
1 2 3 4 5 6 7 8 9 10
@Configuration(proxyBeanMethods = false) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @ConditionalOnClass(ServletRequest.class) @ConditionalOnWebApplication(type = Type.SERVLET) @EnableConfigurationProperties(ServerProperties.class) @Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, ServletWebServerFactoryConfiguration.EmbeddedTomcat.class, ServletWebServerFactoryConfiguration.EmbeddedJetty.class, ServletWebServerFactoryConfiguration.EmbeddedUndertow.class }) public class ServletWebServerFactoryAutoConfiguration {}
|
|
- 这里我们可以直接知道返回的是
@EnableConfigurationProperties(ServerProperties.class)
的数据值:ServerProperties.class
循环注册
|
|
ConfigurationPropertiesBindingPostProcessor
postProcessBeforeInitialization
|
|
-
get
1 2 3 4 5 6
public static ConfigurationPropertiesBean get(ApplicationContext applicationContext, Object bean, String beanName) { // 寻找工厂方法 Method factoryMethod = findFactoryMethod(applicationContext, beanName); // 创建 ConfigurationPropertiesBean return create(beanName, bean, bean.getClass(), factoryMethod); }
|
|
|
|
create
org.springframework.boot.context.properties.ConfigurationPropertiesBean#create
|
|
-
第一个需要做的类:
org.springframework.boot.autoconfigure.web.ServerProperties
-
annotation
bindType
- 返回对象
- 此时数据还没有进去
bind
- 数据绑定
直接看结果
- 上述配置和我在配置文件中写的配置一致
|
|
- 具体方法:
org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor#bind
|
|
|
|
findProperty
|
|
-
org.springframework.boot.context.properties.source.SpringConfigurationPropertySource#getConfigurationProperty
1 2 3 4 5
@Override public ConfigurationProperty getConfigurationProperty(ConfigurationPropertyName name) { PropertyMapping[] mappings = getMapper().map(name); return find(mappings, name); }
1 2 3 4 5 6 7 8 9 10 11 12
protected final ConfigurationProperty find(PropertyMapping[] mappings, ConfigurationPropertyName name) { for (PropertyMapping candidate : mappings) { if (candidate.isApplicable(name)) { ConfigurationProperty result = find(candidate); if (result != null) { return result; } } } return null; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
private ConfigurationProperty find(PropertyMapping mapping) { // 需要读取的配置信息的key String propertySourceName = mapping.getPropertySourceName(); // 信息的value Object value = getPropertySource().getProperty(propertySourceName); if (value == null) { return null; } // 创建对象 ConfigurationPropertyName configurationPropertyName = mapping.getConfigurationPropertyName(); Origin origin = PropertySourceOrigin.get(this.propertySource, propertySourceName); // 包装返回 return ConfigurationProperty.of(configurationPropertyName, value, origin); }
getBindHandler
|
|
- 最终获取得到的处理器
- 最后的 bind
|
|
配置信息到此绑定成功,关于如何处理集合相关的配置请各位读者自行学习
- 原文作者:知识铺
- 原文链接:https://index.zshipu.com/geek/post/code/docs/SpringBoot/SpringBoot-ConfigurationProperties/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。
- 免责声明:本页面内容均来源于站内编辑发布,部分信息来源互联网,并不意味着本站赞同其观点或者证实其内容的真实性,如涉及版权等问题,请立即联系客服进行更改或删除,保证您的合法权益。转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。也可以邮件至 sblig@126.com