Spring - BeanPostProcessors 扩展接口

简介: BeanPostProcessors 扩展接口

@[toc]

在这里插入图片描述


Pre

Spring Boot - 扩展接口一览

在这里插入图片描述


Bean的生成过程

在这里插入图片描述


org.springframework.beans.factory.config.BeanPostProcessor 介绍

public interface BeanPostProcessor {


    @Nullable
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

}

在这里插入图片描述

如果BeanPostProcessors接囗的实现类被注册到ioc容器,那么该容器的每个Bean在调用初始化方法之前,都会获得该接口实现类的一个回调。

要使用BeanPostProcessord回调,就必须先在容器中注册实现该接口的类,那么如何注册呢?

  • 若使用BeanFactory,则必须要显示的调用其addBeanPostProcessor方法进行注册,参数为BeanPostProcessor现类的实例;
  • 使用ApplicationContext,那么容器会在配置文件在中自动寻找实现了 BeanPostProcessor口的Bean,然后自动注册,我们只需要配置个BeanPostProcessor现类的Bean可以了。
  • 多个的BeanPostProcessorg实现类,只要实现Ordered接口,设置order属性就可以很轻确定不同实现类的处理顺序了;
  • 接口中的两个方法都要将传入的bean回,不能返回null,如果返回的是nu么我们通过getBean将得不到目标

ApplicationContext注册Bean PostProcessor源码解析

AbstractApplicationContext#refresh

org.springframework.context.support.AbstractApplicationContext#refresh

来看下关键代码

public void refresh() throws BeansException, IllegalStateException {
     
    ........

    // Register bean processors that intercept bean creation.
    registerBeanPostProcessors(beanFactory);
 
    ........
    // Instantiate all remaining (non-lazy-init) singletons.
    // 实例化剩余的所有非延迟加载单例对象
    // 在上面的registerBeanPostProcessors中已经把所有BeanPostProcessors所有对象都已经实例化过了

    //  这加载的时候会判断bean是不是 FactoryBean类型的
    //  如果是FactoryBean类型,则getBean(&beanName),这里是把FactoryBean本身的对象给实例化了,而没有调它的getObject方法;
    // 还要判断是不是SmartFactoryBean类型的,SmartFactoryBean继承了FactoryBean接口;但是它多了一个    boolean isEagerInit();方法;这个方法就是判断是否需要通过FactoryBean的getObject()生成实例;
    // 如果不是FactoryBean类型,直接getBean就行了;
    
    // 还要判断是不是SmartInitializingSingleton接口,这个接口有个afterSingletonsInstantiated方法;
    // 循环所以bean判断是不是这个类型的,只要是这个类型就调用afterSingletonsInstantiated方法;
    finishBeanFactoryInitialization(beanFactory);

    ........     
            
    }

AbstractApplicationContext#registerBeanPostProcessors

先看 registerBeanPostProcessors

    /**
     * Instantiate and register all BeanPostProcessor beans,
     * respecting explicit order if given.
     * <p>Must be called before any instantiation of application beans.
     */
    protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
    }

PostProcessorRegistrationDelegate.registerBeanPostProcessors

public static void registerBeanPostProcessors(
            ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
        /**
        * 代码走到了这里的时候其实 BeanDefinition数据已经被加载了,只是bean还没有被实例化
        所以这个是去容器里面找到所有类型为BeanPostProcessor的beanName
        */
        String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

    
        int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
        
        beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

        // 优先级最高的BeanPostProcessors,这类最先调用;需要实现PriorityOrdered接口
        List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
        //内部BeanPostProcessors
        List<BeanPostProcessor> internalPostProcessors = new ArrayList<BeanPostProcessor>();
        //继承了Ordered接口,优先级比上面低一点
        List<String> orderedPostProcessorNames = new ArrayList<String>();
        //这就是普通的了,优先级最低
        List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
        
        //下面的这些代码就是遍历所有postProcessorNames,按优先级排序;类型PriorityOrdered>Ordered>普通;在这个类型基础上,还要对他们的order属性就行排序;
        for (String ppName : postProcessorNames) {
            if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
                priorityOrderedPostProcessors.add(pp);
                if (pp instanceof MergedBeanDefinitionPostProcessor) {
                    internalPostProcessors.add(pp);
                }
            }
            else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
                orderedPostProcessorNames.add(ppName);
            }
            else {
                nonOrderedPostProcessorNames.add(ppName);
            }
        }

        // First, register the BeanPostProcessors that implement PriorityOrdered.
        sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
        registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

        // Next, register the BeanPostProcessors that implement Ordered.
        List<BeanPostProcessor> orderedPostProcessors = new ArrayList<BeanPostProcessor>();
        for (String ppName : orderedPostProcessorNames) {
            BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
            orderedPostProcessors.add(pp);
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
            }
        }
        sortPostProcessors(beanFactory, orderedPostProcessors);
        registerBeanPostProcessors(beanFactory, orderedPostProcessors);

        // Now, register all regular BeanPostProcessors.
        List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
        for (String ppName : nonOrderedPostProcessorNames) {
        //这里要注意一下了,看到没有,这个时候已经调用了getBean来生成实例对象了;
            BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
            nonOrderedPostProcessors.add(pp);
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
            }
        }
        registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

        // 排序
        sortPostProcessors(beanFactory, internalPostProcessors);
        //注册
        registerBeanPostProcessors(beanFactory, internalPostProcessors);

        // 加入ApplicationListenerDetector
        beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
    }

    /**
     * 注册 BeanPostProcessor beans.
     * 容器中beanPostProcessors是一个ArrayList来持有这些BeanPostProcessors
     */
    private static void registerBeanPostProcessors(
            ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) {

        for (BeanPostProcessor postProcessor : postProcessors) {
            beanFactory.addBeanPostProcessor(postProcessor);
        }
    }
@Override
    public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
        Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
        this.beanPostProcessors.remove(beanPostProcessor);
        this.beanPostProcessors.add(beanPostProcessor);
        //将是否 hasInstantiationAwareBeanPostProcessors设置为true 
        if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
            this.hasInstantiationAwareBeanPostProcessors = true;
        }
        if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
            this.hasDestructionAwareBeanPostProcessors = true;
        }
    }

回调BeanPostProcessors的时机源码解析

org.springframework.context.support.PostProcessorRegistrationDelegate#registerBeanPostProcessors()
    BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        org.springframework.beans.factory.support.AbstractBeanFactory#getBean
          org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean            

doGetBean 重点看

// Create bean instance.
if (mbd.isSingleton()) {
    sharedInstance = getSingleton(beanName, () -> {
        try {
            return createBean(beanName, mbd, args);
        }
        catch (BeansException ex) {
            // Explicitly remove instance from singleton cache: It might have been put there
            // eagerly by the creation process, to allow for circular reference resolution.
            // Also remove any beans that received a temporary reference to the bean.
            destroySingleton(beanName);
            throw ex;
        }
    });
    beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}

继续看 createBean

 org.springframework.context.support.PostProcessorRegistrationDelegate#registerBeanPostProcessors()
    BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        org.springframework.beans.factory.support.AbstractBeanFactory#getBean
          org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean    
              org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean
                    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean

doCreateBean重点看

// Initialize the bean instance.
Object exposedObject = bean;
try {
    // 属性设置
    populateBean(beanName, mbd, instanceWrapper);
    // BeanPostProcessors两个方法都在这里面
    exposedObject = initializeBean(beanName, exposedObject, mbd);
}
org.springframework.context.support.PostProcessorRegistrationDelegate#registerBeanPostProcessors()
    BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        org.springframework.beans.factory.support.AbstractBeanFactory#getBean
          org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean    
              org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean
                    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
                        org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean

继续核心 initializeBean

protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
        if (System.getSecurityManager() != null) {
            AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                invokeAwareMethods(beanName, bean);
                return null;
            }, getAccessControlContext());
        }
        else {
           // 检查Aware相关接口并设置相关依赖
            invokeAwareMethods(beanName, bean);
        }

        Object wrappedBean = bean;
        if (mbd == null || !mbd.isSynthetic()) {
           // BeanPostProcessor前置处理
            wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
        }

        try {
           // 检查是否是InitializingBean以决定是否调用afterPropertiesSet方法 
           // 检查是否配置有自定义的init-method方法
            invokeInitMethods(beanName, wrappedBean, mbd);
        }
        catch (Throwable ex) {
            throw new BeanCreationException(
                    (mbd != null ? mbd.getResourceDescription() : null),
                    beanName, "Invocation of init method failed", ex);
        }
        if (mbd == null || !mbd.isSynthetic()) {
           // BeanPostProcessor后置处理
            wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
        }

        return wrappedBean;
    }

看下 applyBeanPostProcessorsAfterInitialization

   //注意 每一个实例对象触发这个的时候 都是执行所有的BeanPostProcessors实例对象

    @Override
    public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
            throws BeansException {

        Object result = existingBean;
        for (BeanPostProcessor processor : getBeanPostProcessors()) {
            Object current = processor.postProcessAfterInitialization(result, beanName);
            //这里是循环
            //    result =BeanPostProcessor调用执行方法;返回的对象还是result,只是有可能被某个BeanPostProcessor加强了 beanProcessor.postProcessAfterInitialization(result, beanName);
            if (current == null) {
                return result;
            }
            result = current;
        }
        return result;
    }

扩展示例

package com.artisan.bootspringextend.testextends;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;

/**
 * @author 小工匠
 * @version 1.0
 * @description: TODO
 * @date 2022/11/27 21:22
 * @mark: show me the code , change the world
 */

@Slf4j
@Configuration
public class ExtendBeanPostProcessor  implements BeanPostProcessor {

 @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        log.info("beanName: {}-----> postProcessBeforeInitialization",beanName);
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        log.info( "beanName: {} ------> postProcessAfterInitialization",beanName);
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
    
}
    

看日志

2022-11-27 22:07:40.269  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: bootSpringExtendApplication-----> postProcessBeforeInitialization
2022-11-27 22:07:40.271  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: bootSpringExtendApplication ------> postProcessAfterInitialization
2022-11-27 22:07:40.271  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.AutoConfigurationPackages-----> postProcessBeforeInitialization
2022-11-27 22:07:40.272  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.AutoConfigurationPackages ------> postProcessAfterInitialization
2022-11-27 22:07:40.273  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration-----> postProcessBeforeInitialization
2022-11-27 22:07:40.273  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration ------> postProcessAfterInitialization
2022-11-27 22:07:40.281  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration-----> postProcessBeforeInitialization
2022-11-27 22:07:40.281  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration ------> postProcessAfterInitialization
2022-11-27 22:07:40.284  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: objectNamingStrategy-----> postProcessBeforeInitialization
2022-11-27 22:07:40.284  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: objectNamingStrategy ------> postProcessAfterInitialization
2022-11-27 22:07:40.294  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: mbeanServer-----> postProcessBeforeInitialization
2022-11-27 22:07:40.294  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: mbeanServer ------> postProcessAfterInitialization
2022-11-27 22:07:40.297  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: mbeanExporter-----> postProcessBeforeInitialization
2022-11-27 22:07:40.297  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: mbeanExporter ------> postProcessAfterInitialization
2022-11-27 22:07:40.299  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration-----> postProcessBeforeInitialization
2022-11-27 22:07:40.299  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration ------> postProcessAfterInitialization
2022-11-27 22:07:40.303  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: springApplicationAdminRegistrar-----> postProcessBeforeInitialization
2022-11-27 22:07:40.304  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: springApplicationAdminRegistrar ------> postProcessAfterInitialization
2022-11-27 22:07:40.307  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.aop.AopAutoConfiguration$ClassProxyingConfiguration-----> postProcessBeforeInitialization
2022-11-27 22:07:40.307  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.aop.AopAutoConfiguration$ClassProxyingConfiguration ------> postProcessAfterInitialization
2022-11-27 22:07:40.308  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.aop.AopAutoConfiguration-----> postProcessBeforeInitialization
2022-11-27 22:07:40.308  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.aop.AopAutoConfiguration ------> postProcessAfterInitialization
2022-11-27 22:07:40.308  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration-----> postProcessBeforeInitialization
2022-11-27 22:07:40.308  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration ------> postProcessAfterInitialization
2022-11-27 22:07:40.310  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: applicationAvailability-----> postProcessBeforeInitialization
2022-11-27 22:07:40.310  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: applicationAvailability ------> postProcessAfterInitialization
2022-11-27 22:07:40.311  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration-----> postProcessBeforeInitialization
2022-11-27 22:07:40.311  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration ------> postProcessAfterInitialization
2022-11-27 22:07:40.311  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.context.properties.BoundConfigurationProperties-----> postProcessBeforeInitialization
2022-11-27 22:07:40.311  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.context.properties.BoundConfigurationProperties ------> postProcessAfterInitialization
2022-11-27 22:07:40.312  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.context.properties.EnableConfigurationPropertiesRegistrar.methodValidationExcludeFilter-----> postProcessBeforeInitialization
2022-11-27 22:07:40.312  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.context.properties.EnableConfigurationPropertiesRegistrar.methodValidationExcludeFilter ------> postProcessAfterInitialization
2022-11-27 22:07:40.313  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration-----> postProcessBeforeInitialization
2022-11-27 22:07:40.313  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration ------> postProcessAfterInitialization
2022-11-27 22:07:40.329  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: spring.lifecycle-org.springframework.boot.autoconfigure.context.LifecycleProperties-----> postProcessBeforeInitialization
2022-11-27 22:07:40.329  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: spring.lifecycle-org.springframework.boot.autoconfigure.context.LifecycleProperties ------> postProcessAfterInitialization
2022-11-27 22:07:40.330  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: lifecycleProcessor-----> postProcessBeforeInitialization
2022-11-27 22:07:40.330  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: lifecycleProcessor ------> postProcessAfterInitialization
2022-11-27 22:07:40.333  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties-----> postProcessBeforeInitialization
2022-11-27 22:07:40.333  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties ------> postProcessAfterInitialization
2022-11-27 22:07:40.334  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration-----> postProcessBeforeInitialization
2022-11-27 22:07:40.334  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration ------> postProcessAfterInitialization
2022-11-27 22:07:40.334  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration-----> postProcessBeforeInitialization
2022-11-27 22:07:40.334  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration ------> postProcessAfterInitialization
2022-11-27 22:07:40.336  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties-----> postProcessBeforeInitialization
2022-11-27 22:07:40.336  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties ------> postProcessAfterInitialization
2022-11-27 22:07:40.340  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: taskExecutorBuilder-----> postProcessBeforeInitialization
2022-11-27 22:07:40.340  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: taskExecutorBuilder ------> postProcessAfterInitialization
2022-11-27 22:07:40.341  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration-----> postProcessBeforeInitialization
2022-11-27 22:07:40.341  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration ------> postProcessAfterInitialization
2022-11-27 22:07:40.343  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: spring.task.scheduling-org.springframework.boot.autoconfigure.task.TaskSchedulingProperties-----> postProcessBeforeInitialization
2022-11-27 22:07:40.344  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: spring.task.scheduling-org.springframework.boot.autoconfigure.task.TaskSchedulingProperties ------> postProcessAfterInitialization
2022-11-27 22:07:40.345  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: taskSchedulerBuilder-----> postProcessBeforeInitialization
2022-11-27 22:07:40.345  INFO 12876 --- [           main] c.a.b.t.ExtendBeanPostProcessor          : beanName: taskSchedulerBuilder ------> postProcessAfterInitialization
2022-11-27 22:07:40.362  INFO 12876 --- [           main] c.a.b.BootSpringExtendApplication        : Started BootSpringExtendApplication in 0.822 seconds (JVM running for 2.124)

Process finished with exit code 0

小结

总结一下:

  1. BeanPostProcessors 册发生在容器启动的时候;自动注册BeanPostProcessors类型;
  1. 只要调用getBean始化对象都会触发BeanPostProcessors口的两个方法,并且是所有BeanPostProcessors例都会触发;
  2. 如果是FactoryBean型,容器不会帮我们自动初始化它产生的实例

,除非是SmartFactoryBean实例,并且它的isEagerlnit()返回的是true; IOC器才会也帮我们调用它的getobject放法来生成实例;

在这里插入图片描述

相关文章
|
12天前
|
XML Java 数据格式
探索Spring之利剑:ApplicationContext接口
本文深入介绍了Spring框架中的核心接口ApplicationContext,解释了其作为应用容器的功能,包括事件发布、国际化支持等,并通过基于XML和注解的配置示例展示了如何使用ApplicationContext管理Bean实例。
40 6
|
2月前
|
存储 安全 Java
|
2月前
|
自然语言处理 JavaScript Java
Spring 实现 3 种异步流式接口,干掉接口超时烦恼
本文介绍了处理耗时接口的几种异步流式技术,包括 `ResponseBodyEmitter`、`SseEmitter` 和 `StreamingResponseBody`。这些工具可在执行耗时操作时不断向客户端响应处理结果,提升用户体验和系统性能。`ResponseBodyEmitter` 适用于动态生成内容场景,如文件上传进度;`SseEmitter` 用于实时消息推送,如状态更新;`StreamingResponseBody` 则适合大数据量传输,避免内存溢出。文中提供了具体示例和 GitHub 地址,帮助读者更好地理解和应用这些技术。
412 0
|
3月前
|
存储 数据采集 Java
Spring Boot 3 实现GZIP压缩优化:显著减少接口流量消耗!
在Web开发过程中,随着应用规模的扩大和用户量的增长,接口流量的消耗成为了一个不容忽视的问题。为了提升应用的性能和用户体验,减少带宽占用,数据压缩成为了一个重要的优化手段。在Spring Boot 3中,通过集成GZIP压缩技术,我们可以显著减少接口流量的消耗,从而优化应用的性能。本文将详细介绍如何在Spring Boot 3中实现GZIP压缩优化。
450 6
|
2月前
|
存储 NoSQL Java
Spring Boot项目中使用Redis实现接口幂等性的方案
通过上述方法,可以有效地在Spring Boot项目中利用Redis实现接口幂等性,既保证了接口操作的安全性,又提高了系统的可靠性。
58 0
|
4月前
|
JSON 安全 Java
|
4月前
|
存储 SQL Java
|
5月前
|
SQL Java 数据库
实时计算 Flink版产品使用问题之Spring Boot集成Flink可以通过什么方式实现通过接口启动和关闭Flink程序
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
4月前
|
JavaScript Java Spring
Spring Boot 接口返回文件流
Spring Boot 接口返回文件流
173 0