SpringBoot之浅析配置项解析(四)

简介:

我们在之前的文章中简单的说了一下SpringBoot对于默认的配置文件的解析过程,在这一篇文章中我们再简单的分析一下SpringBoot是怎么将解析到的配置属性信息设置到相应的Bean上的。既然是用SpringBoot的属性配置方式,那么我们在这里会在对应的类上加上ConfigurationProperties和Component(或是和Component相同功能的)注解。我们定义的Bean如下:

@Component
@ConfigurationProperties(prefix ="person.info")
public class PersonInfoDomain {
    /**
     * 用户名
     */
    private String userName;
}

配置文件如下:
application
其内容依次如下:

person.info.user-name=lisi

person:
  info:
    user-name: zhangzhang

person.info.user-name=zhangsan

person:
  info:
    user-name: lilisisi

首先问一个问题,如果是你来实现这样的功能的话,你会在什么时机来进行属性值设置的功能呢?在创建Bean的时候进行属性的设置应该是一个比较合适的时机吧?Spring也是这样做的。在Spring中提供了各种不同功能的接口来让我们在Bean创建的过程中做一些不同功能的扩展,我们先称为"生命周期"的接口(可以在这里进行查看:Spring Bean的生命周期小析(一) Spring Bean的生命周期小析(二)),在Spring在有这样一个类:AbstractAutowireCapableBeanFactory这个类主要的一个功能是串联Spring的生命周期。我们先看一下这个类中的这个方法:applyBeanPostProcessorsBeforeInitialization(不要问我为什么知道这个方法。。。放一下调用链的截图)
applyBeanPostProcessorsBeforeInitialization

    @Override
    public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
            throws BeansException {
        Object result = existingBean;
        //这里的getBeanPostProcessors()这个方法获取到的是Spring 容器中实现BeanPostProcessor接口的Bean 在这个Bean中有一个Bean叫ConfigurationPropertiesBindingPostProcessor 从这个Bean的名字我们可以感觉这个Bean应该是和ConfigurationProperties相关的类,而事实也确是如此 其他的我们先不说了,放一下SpringBoot中内置的一些BeanPostProcessor 的Bean,我们直接进入到 ConfigurationPropertiesBindingPostProcessor 这个类中
        for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
            result = beanProcessor.postProcessBeforeInitialization(result, beanName);
            if (result == null) {
                return result;
            }
        }
        return result;
    }

getBeanPostProcessors

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        //获取类上的ConfigurationProperties注解 用AnnotationUtils来对类上的注解进行处理是很方便,有兴趣的可以看一下AnnotationUtils中的方法的源码实现
        ConfigurationProperties annotation = AnnotationUtils
                .findAnnotation(bean.getClass(), ConfigurationProperties.class);
        //如果类上有ConfigurationProperties 注解
        if (annotation != null) {
            //主要处理方法
            postProcessBeforeInitialization(bean, beanName, annotation);
        }
        //方法上的ConfigurationProperties注解
        annotation = this.beans.findFactoryAnnotation(beanName,
                ConfigurationProperties.class);
        if (annotation != null) {
            postProcessBeforeInitialization(bean, beanName, annotation);
        }
        return bean;
    }

org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor#postProcessBeforeInitialization

    private void postProcessBeforeInitialization(Object bean, String beanName,
            ConfigurationProperties annotation) {
        //我们需要设置属性的目标bean
        Object target = bean;
        //新建一个PropertiesConfigurationFactory类 这个类来完成属性的赋值的工作
        PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>(
                target);
        //propertySources  1)
        factory.setPropertySources(this.propertySources);
        //验证器
        factory.setValidator(determineValidator(bean));
        //转换服务
        factory.setConversionService(this.conversionService == null
                ? getDefaultConversionService() : this.conversionService);
        if (annotation != null) {
            //类上 ConfigurationProperties注解的信息
            factory.setIgnoreInvalidFields(annotation.ignoreInvalidFields());
            factory.setIgnoreUnknownFields(annotation.ignoreUnknownFields());
            factory.setExceptionIfInvalid(annotation.exceptionIfInvalid());
            factory.setIgnoreNestedProperties(annotation.ignoreNestedProperties());
            //属性前缀
            if (StringUtils.hasLength(annotation.prefix())) {
                factory.setTargetName(annotation.prefix());
            }
        }
        try {
            //主要方法
            factory.bindPropertiesToTarget();
        }
    }

对于1)处的PropertySources我们应该不陌生了,前面我们一直在提到这个类。我们看一下这个PropertySources是在什么时候进行赋值的。在ConfigurationPropertiesBindingPostProcessor中有一个这样的方法afterPropertiesSet:

    public void afterPropertiesSet() throws Exception {
        if (this.propertySources == null) {
            //寻找PropertySources
            this.propertySources = deducePropertySources();
        }
        //validator的赋值
        if (this.validator == null) {
            this.validator = getOptionalBean(VALIDATOR_BEAN_NAME, Validator.class);
        }
        //转换服务
        if (this.conversionService == null) {
            this.conversionService = getOptionalBean(
                    ConfigurableApplicationContext.CONVERSION_SERVICE_BEAN_NAME,
                    ConversionService.class);
        }
    }
    private PropertySources deducePropertySources() {
        //从Spring 容器中获取 PropertySourcesPlaceholderConfigurer 
        PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer();
        if (configurer != null) {
            //configurer.getAppliedPropertySources() 这里获取到的就是我们之前一直提到的MutablePropertySources
            return new FlatPropertySources(configurer.getAppliedPropertySources());
        }
        //如果Spring 容器中 没有PropertySourcesPlaceholderConfigurer的话 则从ConfigurableEnvironment中获取
        if (this.environment instanceof ConfigurableEnvironment) {
            MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment)
                    .getPropertySources();
            return new FlatPropertySources(propertySources);
        }
        //还获取不到的话,则新建一个MutablePropertySources
        return new MutablePropertySources();
    }
    private PropertySourcesPlaceholderConfigurer getSinglePropertySourcesPlaceholderConfigurer() {
        if (this.beanFactory instanceof ListableBeanFactory) {
            //从Spring 容器中获取 PropertySourcesPlaceholderConfigurer 
            ListableBeanFactory listableBeanFactory = (ListableBeanFactory) this.beanFactory;
            Map<String, PropertySourcesPlaceholderConfigurer> beans = listableBeanFactory
                    .getBeansOfType(PropertySourcesPlaceholderConfigurer.class, false,
                            false);
            if (beans.size() == 1) {
                return beans.values().iterator().next();
            }
            //如果有多于一个存在的话,则返回null
            if (beans.size() > 1 && logger.isWarnEnabled()) {
            }
        }
        return null;
    }

OK,我们进入到PropertiesConfigurationFactory看一下factory.bindPropertiesToTarget();这个方法的内容:

    public void bindPropertiesToTarget() throws BindException {
        //propertySources不能为null
        Assert.state(this.propertySources != null, "PropertySources should not be null");
        try {
            this.hasBeenBound = true;
            doBindPropertiesToTarget();
        }
    }
    private void doBindPropertiesToTarget() throws BindException {
        //targetName PropertiesConfigurationFactory注解上的前缀值
        //target 目标bean
        RelaxedDataBinder dataBinder = (this.targetName != null
                ? new RelaxedDataBinder(this.target, this.targetName)
                : new RelaxedDataBinder(this.target));
        //校验器
        if (this.validator != null
                && this.validator.supports(dataBinder.getTarget().getClass())) {
            dataBinder.setValidator(this.validator);
        }
        //转换服务
        if (this.conversionService != null) {
            dataBinder.setConversionService(this.conversionService);
        }
        //集合的最大值
        dataBinder.setAutoGrowCollectionLimit(Integer.MAX_VALUE);
        dataBinder.setIgnoreNestedProperties(this.ignoreNestedProperties);
        dataBinder.setIgnoreInvalidFields(this.ignoreInvalidFields);
        dataBinder.setIgnoreUnknownFields(this.ignoreUnknownFields);
        //自定义Binder 这里是一个空实现
        customizeBinder(dataBinder);
        //下面这两段代码是获取 Spring支持的配置的名字 支持格式超出你的想象 可以调试自己看一下
        Iterable<String> relaxedTargetNames = getRelaxedTargetNames();
        Set<String> names = getNames(relaxedTargetNames);
        //获取PropertyValues 重点要分析的
        PropertyValues propertyValues = getPropertySourcesPropertyValues(names,
                relaxedTargetNames);
        //属性值的绑定工作
        dataBinder.bind(propertyValues);
        if (this.validator != null) {
        //属性值的校验
            dataBinder.validate();
        }
        checkForBindingErrors(dataBinder);
    }
相关文章
|
2月前
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
|
1月前
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
1月前
|
XML JSON Java
【SpringBoot(三)】从请求到响应再到视图解析与模板引擎,本文带你领悟SpringBoot请求接收全流程!
Springboot专栏第三章,从请求的接收到视图解析,再到thymeleaf模板引擎的使用! 本文带你领悟SpringBoot请求接收到渲染的使用全流程!
198 3
|
2月前
|
Java 数据库 数据安全/隐私保护
Spring Boot四层架构深度解析
本文详解Spring Boot四层架构(Controller-Service-DAO-Database)的核心思想与实战应用,涵盖职责划分、代码结构、依赖注入、事务管理及常见问题解决方案,助力构建高内聚、低耦合的企业级应用。
796 1
|
8月前
|
存储 Java 文件存储
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— logback.xml 配置文件解析
本文解析了 `logback.xml` 配置文件的详细内容,包括日志输出格式、存储路径、控制台输出及日志级别等关键配置。通过定义 `LOG_PATTERN` 和 `FILE_PATH`,设置日志格式与存储路径;利用 `&lt;appender&gt;` 节点配置控制台和文件输出,支持日志滚动策略(如文件大小限制和保存时长);最后通过 `&lt;logger&gt;` 和 `&lt;root&gt;` 定义日志级别与输出方式。此配置适用于精细化管理日志输出,满足不同场景需求。
2121 1
|
7月前
|
前端开发 安全 Java
Spring Boot 便利店销售系统项目分包设计解析
本文深入解析了基于Spring Boot的便利店销售系统分包设计,通过清晰的分层架构(表现层、业务逻辑层、数据访问层等)和模块化设计,提升了代码的可维护性、复用性和扩展性。具体分包结构包括`controller`、`service`、`repository`、`entity`、`dto`、`config`和`util`等模块,职责分明,便于团队协作与功能迭代。该设计为复杂企业级应用开发提供了实践参考。
290 0
|
4月前
|
前端开发 Java 数据库连接
SpringBoot参数校验底层原理和实操。深度历险、深度解析(图解+秒懂+史上最全)
SpringBoot参数校验底层原理和实操。深度历险、深度解析(图解+秒懂+史上最全)
SpringBoot参数校验底层原理和实操。深度历险、深度解析(图解+秒懂+史上最全)
|
4月前
|
机器学习/深度学习 XML Java
【spring boot logback】日志logback格式解析
在 Spring Boot 中,Logback 是默认的日志框架,它支持灵活的日志格式配置。通过配置 logback.xml 文件,可以定义日志的输出格式、日志级别、日志文件路径等。
768 5
|
4月前
|
Java 关系型数据库 数据库连接
Spring Boot项目集成MyBatis Plus操作PostgreSQL全解析
集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步骤与 MyBatis 类似,只不过在 MyBatis Plus 中提供了更多的便利功能,如自动生成 SQL、分页查询、Wrapper 查询等。
408 3

推荐镜像

更多
  • DNS