SpringBoot 刷新上下文7--执行BeanFactoryPostProcessor

SpringBoot刷新上下文流程
本文详细介绍了SpringBoot刷新上下文的过程,特别是BeanFactoryPostProcessor的执行细节,包括ConfigurationClassPostProcessor对@Configuration类的增强、ConfigFileApplicationListener对属性源的排序及PropertySourcesPlaceholderConfigurer对占位符的支持。

SpringBoot刷新上下文一共七篇,基于SpringBoot 2.2.7.RELEASE,Spring 5.2.6.RELEASE
SpringBoot 刷新上下文1–主流程
SpringBoot 刷新上下文2–执行BeanDefinitionRegistryPostProcessor
SpringBoot 刷新上下文3–解析引导类
SpringBoot 刷新上下文4–处理ComponentScan
SpringBoot 刷新上下文5–处理其他注解
SpringBoot 刷新上下文6–加载并注册BeanDefinition
SpringBoot 刷新上下文7–执行BeanFactoryPostProcessor

2.2、调用BeanFactoryPostProcessor

//org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(java.util.Collection<? extends org.springframework.beans.factory.config.BeanFactoryPostProcessor>, org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
private static void invokeBeanFactoryPostProcessors(Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {

    for (BeanFactoryPostProcessor postProcessor : postProcessors) {
        postProcessor.postProcessBeanFactory(beanFactory);
    }
}

2.2.1、ConfigurationClassPostProcessor

org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer.CachingMetadataReaderFactoryPostProcessor 和 org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer.ConfigurationWarningsPostProcessor 的 postProcessBeanFactory 方法都是空实现。

//org.springframework.context.annotation.ConfigurationClassPostProcessor#postProcessBeanFactory
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    //获取factoryId
    int factoryId = System.identityHashCode(beanFactory);
    // 已经处理过,报错
    if (this.factoriesPostProcessed.contains(factoryId)) {
        throw new IllegalStateException(
            "postProcessBeanFactory already called on this post-processor against " + beanFactory);
    }
    this.factoriesPostProcessed.add(factoryId);
    if (!this.registriesPostProcessed.contains(factoryId)) {
        // BeanDefinitionRegistryPostProcessor hook apparently not supported...
        // Simply call processConfigurationClasses lazily at this point then.
        // 如果该 factoryId 没有处理过,调用上面的处理方法进行处理
        processConfigBeanDefinitions((BeanDefinitionRegistry) beanFactory);
    }
	//增强配置类,代理
    enhanceConfigurationClasses(beanFactory);
    beanFactory.addBeanPostProcessor(new ImportAwareBeanPostProcessor(beanFactory));
}
//org.springframework.context.annotation.ConfigurationClassPostProcessor#enhanceConfigurationClasses
public void enhanceConfigurationClasses(ConfigurableListableBeanFactory beanFactory) {
    //被@Configuration标注且proxyBeanMethods是true的类集合
    Map<String, AbstractBeanDefinition> configBeanDefs = new LinkedHashMap<>();
    //获取ioc中注册的所有bean的名称,遍历
    for (String beanName : beanFactory.getBeanDefinitionNames()) {
        // 根据bean名称获取BeanDefinition
        BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName);
        // 获取BeanDefinition的org.springframework.context.annotation.ConfigurationClassPostProcessor.configurationClass 属性
        Object configClassAttr = beanDef.getAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE);
        MethodMetadata methodMetadata = null;
        // 被注解修饰类的BeanDefinition
        if (beanDef instanceof AnnotatedBeanDefinition) {
            // 获取注解元信息
            methodMetadata = ((AnnotatedBeanDefinition) beanDef).getFactoryMethodMetadata();
        }
        // 有configurationClass 属性 或者 被注解修饰
        if ((configClassAttr != null || methodMetadata != null) && beanDef instanceof AbstractBeanDefinition) {
            // Configuration class (full or lite) or a configuration-derived @Bean method
            // -> resolve bean class at this point...
            AbstractBeanDefinition abd = (AbstractBeanDefinition) beanDef;
            if (!abd.hasBeanClass()) {
                try {
                    abd.resolveBeanClass(this.beanClassLoader);
                }
                catch (Throwable ex) {
                    throw new IllegalStateException(
                        "Cannot load configuration class: " + beanDef.getBeanClassName(), ex);
                }
            }
        }
        // configurationClass 属性 是 full
        // 被 @Configuration 标注并且 proxyBeanMethods 是 true 的是 full,其他是 lite
        if (ConfigurationClassUtils.CONFIGURATION_CLASS_FULL.equals(configClassAttr)) {
            if (!(beanDef instanceof AbstractBeanDefinition)) {
                throw new BeanDefinitionStoreException("Cannot enhance @Configuration bean definition '" +
                                                       beanName + "' since it is not stored in an AbstractBeanDefinition subclass");
            }
            // log 级别 并且 单例bean已经创建过了,打个日志
            else if (logger.isInfoEnabled() && beanFactory.containsSingleton(beanName)) {
                logger.info("Cannot enhance @Configuration bean definition '" + beanName +
                            "' since its singleton instance has been created too early. The typical cause " +
                            "is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor " +
                            "return type: Consider declaring such methods as 'static'.");
            }
            configBeanDefs.put(beanName, (AbstractBeanDefinition) beanDef);
        }
    }
    if (configBeanDefs.isEmpty()) {
        // nothing to enhance -> return immediately
        return;
    }
	//创建cglib代理类并且放入BeanDefinition中
    ConfigurationClassEnhancer enhancer = new ConfigurationClassEnhancer();
    for (Map.Entry<String, AbstractBeanDefinition> entry : configBeanDefs.entrySet()) {
        AbstractBeanDefinition beanDef = entry.getValue();
        // If a @Configuration class gets proxied, always proxy the target class
        beanDef.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
        // Set enhanced subclass of the user-specified bean class
        Class<?> configClass = beanDef.getBeanClass();
        Class<?> enhancedClass = enhancer.enhance(configClass, this.beanClassLoader);
        if (configClass != enhancedClass) {
            if (logger.isTraceEnabled()) {
                logger.trace(String.format("Replacing bean definition '%s' existing class '%s' with " +
                                           "enhanced class '%s'", entry.getKey(), configClass.getName(), enhancedClass.getName()));
            }
            beanDef.setBeanClass(enhancedClass);
        }
    }
}

这个方法对@Configration配置类做了一个 代理,用的Cglib

2.2.2、ConfigFileApplicationListener

//org.springframework.boot.context.config.ConfigFileApplicationListener.PropertySourceOrderingPostProcessor#postProcessBeanFactory
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    reorderSources(this.context.getEnvironment());
}
//org.springframework.boot.context.config.ConfigFileApplicationListener.PropertySourceOrderingPostProcessor#reorderSources
private void reorderSources(ConfigurableEnvironment environment) {
    //DEFAULT_PROPERTIES = defaultProperties
    PropertySource<?> defaultProperties = environment.getPropertySources().remove(DEFAULT_PROPERTIES);
    if (defaultProperties != null) {
        environment.getPropertySources().addLast(defaultProperties);
    }
}

2.3、调用实现了优先级Order的BeanFactoryPostProcessor

//org.springframework.context.support.PropertySourcesPlaceholderConfigurer#postProcessBeanFactory
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (this.propertySources == null) {
        this.propertySources = new MutablePropertySources();
        if (this.environment != null) {
            this.propertySources.addLast(
                new PropertySource<Environment>(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, this.environment) {
                    @Override
                    @Nullable
                    public String getProperty(String key) {
                        return this.source.getProperty(key);
                    }
                }
            );
        }
        try {
            PropertySource<?> localPropertySource =
                new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergeProperties());
            if (this.localOverride) {
                this.propertySources.addFirst(localPropertySource);
            }
            else {
                this.propertySources.addLast(localPropertySource);
            }
        }
        catch (IOException ex) {
            throw new BeanInitializationException("Could not load properties", ex);
        }
    }

    processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources));
    this.appliedPropertySources = this.propertySources;
}
//org.springframework.context.support.PropertySourcesPlaceholderConfigurer#processProperties(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, org.springframework.core.env.ConfigurablePropertyResolver)
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, final ConfigurablePropertyResolver propertyResolver) throws BeansException {

    propertyResolver.setPlaceholderPrefix(this.placeholderPrefix);
    propertyResolver.setPlaceholderSuffix(this.placeholderSuffix);
    propertyResolver.setValueSeparator(this.valueSeparator);

    StringValueResolver valueResolver = strVal -> {
        String resolved = (this.ignoreUnresolvablePlaceholders ?
                           propertyResolver.resolvePlaceholders(strVal) :
                           propertyResolver.resolveRequiredPlaceholders(strVal));
        if (this.trimValues) {
            resolved = resolved.trim();
        }
        return (resolved.equals(this.nullValue) ? null : resolved);
    };

    doProcessProperties(beanFactoryToProcess, valueResolver);
}
//org.springframework.beans.factory.config.PlaceholderConfigurerSupport#doProcessProperties
protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess, StringValueResolver valueResolver) {

    BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);

    String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    for (String curName : beanNames) {
        // Check that we're not parsing our own bean definition,
        // to avoid failing on unresolvable placeholders in properties file locations.
        if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
            BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);
            try {
                visitor.visitBeanDefinition(bd);
            }
            catch (Exception ex) {
                throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage(), ex);
            }
        }
    }

    // New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
    beanFactoryToProcess.resolveAliases(valueResolver);

    // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
    beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
}

2.4、调用实现了普通Order的BeanFactoryPostProcessor

//org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator#postProcessBeanFactory
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    //对单例bean校验
    for (String beanName : beanFactory.getBeanDefinitionNames()) {
        if (!(beanFactory.containsSingleton(beanName) || isValueObjectBeanDefinition(beanFactory, beanName))) {
            validate(beanFactory, beanName);
        }
    }
}

2.5、调用没有实现order的BeanFactoryPostProcessor

2.5.1、EventListenerMethodProcessor

//org.springframework.context.event.EventListenerMethodProcessor#postProcessBeanFactory
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    this.beanFactory = beanFactory;
	//将所有的EventListenerMethodProcessor 排序
    Map<String, EventListenerFactory> beans = beanFactory.getBeansOfType(EventListenerFactory.class, false, false);
    List<EventListenerFactory> factories = new ArrayList<>(beans.values());
    AnnotationAwareOrderComparator.sort(factories);
    this.eventListenerFactories = factories;
}

2.5.2、PreserveErrorControllerTargetClassPostProcessor

//org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration.PreserveErrorControllerTargetClassPostProcessor#postProcessBeanFactory
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    //获取所有的errorController
    String[] errorControllerBeans = beanFactory.getBeanNamesForType(ErrorController.class, false, false);
    for (String errorControllerBean : errorControllerBeans) {
        try {
            //AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE = org.springframework.aop.framework.autoproxy.AutoProxyUtils.preserveTargetClass
            // 将 preserveTargetClass 属性 设置为true
            beanFactory.getBeanDefinition(errorControllerBean)
                .setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
        }
        catch (Throwable ex) {
            // Ignore
        }
    }
}
Spring Boot 项目的启动流程是一个高度自动化且模块化的过程,其核心逻辑围绕 `SpringApplication` 类的 `run()` 方法展开。在 application 文件(如 `application.properties` 或 `application.yml`)加载并解析后,Spring Boot 开始执行一系列标准化步骤以完成上下文的准备与初始化。 ### Spring Boot 启动时 application 文件执行后的流程 1. **环境对象的创建** 在 application 配置文件被读取后,Spring Boot 创建一个 `ConfigurableEnvironment` 实例,该实例负责管理所有配置属性,包括系统环境变量、JVM 参数以及配置文件中的属性[^3]。这一过程确保了后续组件能够通过 `@Value` 或 `Environment` 对象访问到配置信息。 2. **应用上下文的准备阶段 (`prepareContext`)** - **添加监听器和初始化器**:在此阶段,Spring Boot 添加了一系列内置的 `ApplicationListener` 和 `ApplicationContextInitializer`,例如 `CloudFoundryVcapEnvironmentPostProcessor` 和 `LoggingApplicationListener`,这些组件用于处理特定的启动逻辑,如日志配置和环境适配[^4]。 - **注册 Bean 定义**:Spring Boot 将主类(标注有 `@SpringBootApplication` 的类)作为配置类注册,并扫描该类所在包下的所有组件(如 `@Component`, `@Service`, `@Repository` 等),将其注册为 Spring 容器中的 Bean[^1]。 3. **刷新上下文 (`refreshContext`)** - **Bean 工厂的准备**:Spring 容器开始加载和注册所有的 Bean 定义,并准备 `BeanFactory`,这是 Spring IoC 容器的核心部分。 - **执行 `BeanFactoryPostProcessor`**:此步骤允许对 Bean 定义进行修改或扩展,例如 `PropertySourceOrderingPostProcessor` 会调整属性源的顺序。 - **注册 `BeanPostProcessor`**:这些处理器会在 Bean 初始化前后对其进行干预,实现诸如 AOP 功能等增强操作。 - **初始化消息资源、事件广播器等**:Spring 容器初始化国际化消息支持、事件发布机制等基础设施。 - **注册监听器**:将 `ApplicationListener` 注册到上下文中,以便在后续生命周期中响应事件。 - **完成上下文刷新**:触发 `ContextRefreshedEvent` 事件,表示上下文已准备好使用[^2]。 4. **运行 `CommandLineRunner` 和 `ApplicationRunner` 接口实现类** 在 Spring Boot 应用完全启动后,可以通过实现 `CommandLineRunner` 或 `ApplicationRunner` 接口来执行自定义的初始化逻辑。这两个接口的区别在于参数的处理方式不同,前者接收原始字符串数组,后者则提供了一个封装好的 `ApplicationArguments` 对象[^5]。 ```java @Component @Order(1) public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { // 执行启动后任务 } } ``` 5. **启动内嵌的 Web 容器(如果是 Web 应用)** 如果项目是基于 Web 的 Spring Boot 应用,则在此阶段会启动内嵌的 Tomcat、Jetty 或 Undertow 服务器,并绑定到指定端口等待请求[^1]。 6. **发送 `ApplicationReadyEvent` 事件** 当所有初始化工作完成后,Spring Boot 发送 `ApplicationReadyEvent` 事件,标志着应用已经完全启动并可以对外提供服务。开发者可以通过监听该事件执行额外的操作,例如打印启动成功的提示信息或执行健康检查[^1]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值