Spring筆記(6) - Spring的BeanFactoryPostProcessor探究 Spring筆記(2) - 生命週期/屬性賦值/自動裝配及部分源碼解析

一.背景

  在說BeanFactoryPostProcessor之前,先來說下BeanPostProcessor,在前文Spring筆記(2) - 生命週期/屬性賦值/自動裝配及部分源碼解析中講解了BeanPostProcessor是一個bean後置處理器(bean創建對象初始化前後進行攔截工作)。

  BeanPostProcessor的運行流程如下:

    1)Spring IOC容器實例化Bean;

    2)調用BeanPostProcessor的postProcessBeforeInitialization方法;

    3)調用bean實例的初始化方法;

    4)調用BeanPostProcessor的postProcessAfterInitialization方法; 

  實現BeanPostProcessor接口可以在Bean(實例化之後)初始化的前後做一些自定義的操作,但是拿到的參數只有BeanDefinition實例和BeanDefinition的名稱,也就是無法修改BeanDefinition元數據,這裏說的Bean的初始化是:

    1)bean實現了InitializingBean接口,對應的方法爲afterPropertiesSet 

    2)在bean定義的時候,通過init-method設置的方法

  Spring中Bean的實例化過程圖示:  


  那麼BeanFactoryPostProcessor顧名思義就是bean工廠的後置處理器,說通俗一些就是可以管理我們的bean工廠內所有的BeanDefinition(未實例化)數據,可以隨心所欲的修改屬性。            

  Spring容器初始化時,從資源中讀取到bean的相關定義後,保存在beanFactory的成員變量中(參考DefaultListableBeanFactory類的成員變量beanDefinitionMap),在實例化bean的操作就是依據這些bean的定義來做的,而在實例化之前,Spring允許我們通過自定義擴展來改變bean的定義,定義一旦變了,後面的實例也就變了,而beanFactory後置處理器,即BeanFactoryPostProcessor就是用來改變bean定義的;如果業務需要,可以配置多個BeanFactoryPostProcessor的實現類,通過”order”控制執行次序(要實現Ordered接口)。

  註冊一個BeanFactoryPostProcessor實例需要定義一個Java類來實現BeanFactoryPostProcessor接口,並重寫該接口的postProcessorBeanFactory方法。通過beanFactory可以獲取bean的定義信息,並可以修改bean的定義信息。(這點是和BeanPostProcessor最大區別)

  所以通過上面的介紹可以總結出有兩種方式可以對bean做控制(例如修改某個成員變量):

    1. 只改變實例化的對象(BeanPostProcessor接口);

    2. 改變bean的定義(BeanFactoryPostProcessor接口) ,可以想象成修改了class文件,這樣實例化出來的每個對象都變了;  

  PS:BeanFactoryPostProcessor回調會先於BeanPostProcessor  

  下面是BeanFactoryPostProcessor的源碼:

public interface BeanFactoryPostProcessor {

    /**
     * Modify the application context's internal bean factory after its standard
     * initialization. All bean definitions will have been loaded, but no beans
     * will have been instantiated yet. This allows for overriding or adding
     * properties even to eager-initializing beans.
     * @param beanFactory the bean factory used by the application context
     * @throws org.springframework.beans.BeansException in case of errors
     */
     //在ioc容器的bean Factory標準初始化之後可以對它們進行修改。所有的bean定義被加載了,但還沒有被實例化。
     //允許進行重載或添加屬性即使在eager-initializing beans
    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}

  BeanFactoryPostProcessor此接口只提供了一個方法,方法參數爲ConfigurableListableBeanFactory,下面是該類的源碼: 

public interface ConfigurableListableBeanFactory
        extends ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory {

    void ignoreDependencyType(Class<?> type);

    void ignoreDependencyInterface(Class<?> ifc);

    void registerResolvableDependency(Class<?> dependencyType, @Nullable Object autowiredValue);

    boolean isAutowireCandidate(String beanName, DependencyDescriptor descriptor)
            throws NoSuchBeanDefinitionException;

    BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;

    Iterator<String> getBeanNamesIterator();

    void clearMetadataCache();

    void freezeConfiguration();

    boolean isConfigurationFrozen();

    void preInstantiateSingletons() throws BeansException;

}

  其中有個方法名爲getBeanDefinition的方法,我們可以根據此方法,找到我們定義bean的BeanDefinition對象。然後我們可以對定義的屬性進行修改,以下是BeanDefinition中的方法:

public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {

    /**
     * Scope identifier for the standard singleton scope: "singleton".
     * <p>Note that extended bean factories might support further scopes.
     * @see #setScope
     */
    String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;

    /**
     * Scope identifier for the standard prototype scope: "prototype".
     * <p>Note that extended bean factories might support further scopes.
     * @see #setScope
     */
    String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;


    /**
     * Role hint indicating that a {@code BeanDefinition} is a major part
     * of the application. Typically corresponds to a user-defined bean.
     */
    int ROLE_APPLICATION = 0;

    /**
     * Role hint indicating that a {@code BeanDefinition} is a supporting
     * part of some larger configuration, typically an outer
     * {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
     * {@code SUPPORT} beans are considered important enough to be aware
     * of when looking more closely at a particular
     * {@link org.springframework.beans.factory.parsing.ComponentDefinition},
     * but not when looking at the overall configuration of an application.
     */
    int ROLE_SUPPORT = 1;

    /**
     * Role hint indicating that a {@code BeanDefinition} is providing an
     * entirely background role and has no relevance to the end-user. This hint is
     * used when registering beans that are completely part of the internal workings
     * of a {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
     */
    int ROLE_INFRASTRUCTURE = 2;


    // Modifiable attributes

    /**
     * Set the name of the parent definition of this bean definition, if any.
     */
    void setParentName(@Nullable String parentName);

    /**
     * Return the name of the parent definition of this bean definition, if any.
     */
    @Nullable
    String getParentName();

    /**
     * Specify the bean class name of this bean definition.
     * <p>The class name can be modified during bean factory post-processing,
     * typically replacing the original class name with a parsed variant of it.
     * @see #setParentName
     * @see #setFactoryBeanName
     * @see #setFactoryMethodName
     */
    void setBeanClassName(@Nullable String beanClassName);

    /**
     * Return the current bean class name of this bean definition.
     * <p>Note that this does not have to be the actual class name used at runtime, in
     * case of a child definition overriding/inheriting the class name from its parent.
     * Also, this may just be the class that a factory method is called on, or it may
     * even be empty in case of a factory bean reference that a method is called on.
     * Hence, do <i>not</i> consider this to be the definitive bean type at runtime but
     * rather only use it for parsing purposes at the individual bean definition level.
     * @see #getParentName()
     * @see #getFactoryBeanName()
     * @see #getFactoryMethodName()
     */
    @Nullable
    String getBeanClassName();

    /**
     * Override the target scope of this bean, specifying a new scope name.
     * @see #SCOPE_SINGLETON
     * @see #SCOPE_PROTOTYPE
     */
    void setScope(@Nullable String scope);

    /**
     * Return the name of the current target scope for this bean,
     * or {@code null} if not known yet.
     */
    @Nullable
    String getScope();

    /**
     * Set whether this bean should be lazily initialized.
     * <p>If {@code false}, the bean will get instantiated on startup by bean
     * factories that perform eager initialization of singletons.
     */
    void setLazyInit(boolean lazyInit);

    /**
     * Return whether this bean should be lazily initialized, i.e. not
     * eagerly instantiated on startup. Only applicable to a singleton bean.
     */
    boolean isLazyInit();

    /**
     * Set the names of the beans that this bean depends on being initialized.
     * The bean factory will guarantee that these beans get initialized first.
     */
    void setDependsOn(@Nullable String... dependsOn);

    /**
     * Return the bean names that this bean depends on.
     */
    @Nullable
    String[] getDependsOn();

    /**
     * Set whether this bean is a candidate for getting autowired into some other bean.
     * <p>Note that this flag is designed to only affect type-based autowiring.
     * It does not affect explicit references by name, which will get resolved even
     * if the specified bean is not marked as an autowire candidate. As a consequence,
     * autowiring by name will nevertheless inject a bean if the name matches.
     */
    void setAutowireCandidate(boolean autowireCandidate);

    /**
     * Return whether this bean is a candidate for getting autowired into some other bean.
     */
    boolean isAutowireCandidate();

    /**
     * Set whether this bean is a primary autowire candidate.
     * <p>If this value is {@code true} for exactly one bean among multiple
     * matching candidates, it will serve as a tie-breaker.
     */
    void setPrimary(boolean primary);

    /**
     * Return whether this bean is a primary autowire candidate.
     */
    boolean isPrimary();

    /**
     * Specify the factory bean to use, if any.
     * This the name of the bean to call the specified factory method on.
     * @see #setFactoryMethodName
     */
    void setFactoryBeanName(@Nullable String factoryBeanName);

    /**
     * Return the factory bean name, if any.
     */
    @Nullable
    String getFactoryBeanName();

    /**
     * Specify a factory method, if any. This method will be invoked with
     * constructor arguments, or with no arguments if none are specified.
     * The method will be invoked on the specified factory bean, if any,
     * or otherwise as a static method on the local bean class.
     * @see #setFactoryBeanName
     * @see #setBeanClassName
     */
    void setFactoryMethodName(@Nullable String factoryMethodName);

    /**
     * Return a factory method, if any.
     */
    @Nullable
    String getFactoryMethodName();

    /**
     * Return the constructor argument values for this bean.
     * <p>The returned instance can be modified during bean factory post-processing.
     * @return the ConstructorArgumentValues object (never {@code null})
     */
    ConstructorArgumentValues getConstructorArgumentValues();

    /**
     * Return if there are constructor argument values defined for this bean.
     * @since 5.0.2
     */
    default boolean hasConstructorArgumentValues() {
        return !getConstructorArgumentValues().isEmpty();
    }

    /**
     * Return the property values to be applied to a new instance of the bean.
     * <p>The returned instance can be modified during bean factory post-processing.
     * @return the MutablePropertyValues object (never {@code null})
     */
    MutablePropertyValues getPropertyValues();

    /**
     * Return if there are property values values defined for this bean.
     * @since 5.0.2
     */
    default boolean hasPropertyValues() {
        return !getPropertyValues().isEmpty();
    }

    /**
     * Set the name of the initializer method.
     * @since 5.1
     */
    void setInitMethodName(@Nullable String initMethodName);

    /**
     * Return the name of the initializer method.
     * @since 5.1
     */
    @Nullable
    String getInitMethodName();

    /**
     * Set the name of the destroy method.
     * @since 5.1
     */
    void setDestroyMethodName(@Nullable String destroyMethodName);

    /**
     * Return the name of the destroy method.
     * @since 5.1
     */
    @Nullable
    String getDestroyMethodName();

    /**
     * Set the role hint for this {@code BeanDefinition}. The role hint
     * provides the frameworks as well as tools with an indication of
     * the role and importance of a particular {@code BeanDefinition}.
     * @since 5.1
     * @see #ROLE_APPLICATION
     * @see #ROLE_SUPPORT
     * @see #ROLE_INFRASTRUCTURE
     */
    void setRole(int role);

    /**
     * Get the role hint for this {@code BeanDefinition}. The role hint
     * provides the frameworks as well as tools with an indication of
     * the role and importance of a particular {@code BeanDefinition}.
     * @see #ROLE_APPLICATION
     * @see #ROLE_SUPPORT
     * @see #ROLE_INFRASTRUCTURE
     */
    int getRole();

    /**
     * Set a human-readable description of this bean definition.
     * @since 5.1
     */
    void setDescription(@Nullable String description);

    /**
     * Return a human-readable description of this bean definition.
     */
    @Nullable
    String getDescription();


    // Read-only attributes

    /**
     * Return a resolvable type for this bean definition,
     * based on the bean class or other specific metadata.
     * <p>This is typically fully resolved on a runtime-merged bean definition
     * but not necessarily on a configuration-time definition instance.
     * @return the resolvable type (potentially {@link ResolvableType#NONE})
     * @since 5.2
     * @see ConfigurableBeanFactory#getMergedBeanDefinition
     */
    ResolvableType getResolvableType();

    /**
     * Return whether this a <b>Singleton</b>, with a single, shared instance
     * returned on all calls.
     * @see #SCOPE_SINGLETON
     */
    boolean isSingleton();

    /**
     * Return whether this a <b>Prototype</b>, with an independent instance
     * returned for each call.
     * @since 3.0
     * @see #SCOPE_PROTOTYPE
     */
    boolean isPrototype();

    /**
     * Return whether this bean is "abstract", that is, not meant to be instantiated.
     */
    boolean isAbstract();

    /**
     * Return a description of the resource that this bean definition
     * came from (for the purpose of showing context in case of errors).
     */
    @Nullable
    String getResourceDescription();

    /**
     * Return the originating BeanDefinition, or {@code null} if none.
     * Allows for retrieving the decorated bean definition, if any.
     * <p>Note that this method returns the immediate originator. Iterate through the
     * originator chain to find the original BeanDefinition as defined by the user.
     */
    @Nullable
    BeanDefinition getOriginatingBeanDefinition();

}
View Code

  我們可以在上面代碼中發現裏面的方法名字類似bean標籤的屬性,setBeanClassName對應bean標籤中的class屬性,所以當我們拿到BeanDefinition對象時,我們可以手動修改bean標籤中所定義的屬性值。

  具體這個BeanDefinition是個什麼對象,當我們在xml中定義了bean標籤時,Spring會把這些bean標籤解析成一個javabean,這個BeanDefinition就是bean標籤對應的javabean。

  所以當我們調用BeanFactoryPostProcess方法時,這時候bean還沒有實例化,此時bean剛被解析成BeanDefinition對象。

  Spring容器初始化bean大致過程 :

    1)定義bean標籤

    2)將bean標籤解析成BeanDefinition

    3)調用構造方法實例化(IOC)

    4)屬性值得依賴注入(DI)

  所以可以看出BeanFactoryPostProcess方法的執行是發生在第二步之後,第三步之前。

  綜上所述BeanPostProcessor和BeanFactoryPostProcess都是爲Spring提供的後處理bean的接口,只是兩者執行的時機不一樣。BeanPostProcessor爲實例化之後,BeanFactoryPostProcess是實例化之前。功能上,BeanFactoryPostProcess對bean的處理功能更加強大。

二.案例

   1.配置類:進行包掃描將類加載到容器中

@ComponentScan("com.hrh.ext")
@Configuration
public class ExtConfig {
    @Bean
    public Person person() {
        return new Person("張三", "男");
    }
}

  2.實體類:

public class Person implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware {
private String name;
private String sex;

    public Person() {
        System.out.println("Person無參構造器");

    }

    public Person(String name, String sex) {
        System.out.println("Person有參構造器:[name=" + name + ",sex=" + sex + "]");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("[Person]調用了BeanFactoryAware的setBeanFactory方法了:" + beanFactory);
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("[Person]調用了BeanNameAware的setBeanName方法了:" + name);
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("[Person]調用了DisposableBean的destroy方法了");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("[Person]調用了Initailization的afterPropertiesSet方法了");
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", sex=" + sex
                + "]";
    }
}

   3.自定義BeanFactoryPostProcessor類:

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("[MyBeanFactoryPostProcessor]調用了postProcessBeanFactory");
        int count = beanFactory.getBeanDefinitionCount();
        System.out.println("[MyBeanFactoryPostProcessor]當前beanFactory共有" + count + "個bean");
        String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
        System.out.println("[MyBeanFactoryPostProcessor]當前beanFactory有下面組件" + Arrays.asList(beanDefinitionNames));
        //獲取容器中所有的beanDefinition
        for (String beanName : beanDefinitionNames) {
            if ("person".equals(beanName)) {
                //獲取PersonDefinition對象
                BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
                MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
                System.out.println(propertyValues.toString());
                //修改定義中的name屬性值
                propertyValues.addPropertyValue("name", "趙四");
                System.out.println("[MyBeanFactoryPostProcessor]postProcessBeanFactory方法中修改了name屬性初始值了");
                System.out.println(propertyValues.toString());
            }
        }
    }
}

  4.自定義BeanPostProcessor類:

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean,
                                                  String beanName) throws BeansException {
        System.out.println("[MyBeanPostProcessor]後置處理器處理bean=【" + beanName + "】開始");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean,
                                                 String beanName) throws BeansException {
        System.out.println("[MyBeanPostProcessor]後置處理器處理bean=【" + beanName + "】完畢!");
        return bean;
    }
}

   5.測試:

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ExtConfig.class);
        Person bean = context.getBean(Person.class);
        System.out.println(bean.toString());
        context.close();
    }
    
======運行結果======
[MyBeanFactoryPostProcessor]調用了postProcessBeanFactory
[MyBeanFactoryPostProcessor]當前beanFactory共有9個bean
[MyBeanFactoryPostProcessor]當前beanFactory有下面組件[org.springframework.context.annotation.internalConfigurationAnnotationProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor, org.springframework.context.event.internalEventListenerProcessor, org.springframework.context.event.internalEventListenerFactory, extConfig, myBeanFactoryPostProcessor, myBeanPostProcessor, person]
PropertyValues: length=0
[MyBeanFactoryPostProcessor]postProcessBeanFactory方法中修改了name屬性初始值了
PropertyValues: length=1; bean property 'name'
[MyBeanPostProcessor]後置處理器處理bean=【extConfig】開始
[MyBeanPostProcessor]後置處理器處理bean=【extConfig】完畢!
Person有參構造器:[name=張三,sex=男]
[Person]調用了BeanNameAware的setBeanName方法了:person
[Person]調用了BeanFactoryAware的setBeanFactory方法了:org.springframework.beans.factory.support.DefaultListableBeanFactory@e45f292: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,extConfig,myBeanFactoryPostProcessor,myBeanPostProcessor,person]; root of factory hierarchy
[MyBeanPostProcessor]後置處理器處理bean=【person】開始
[Person]調用了Initailization的afterPropertiesSet方法了
[MyBeanPostProcessor]後置處理器處理bean=【person】完畢!
Person [name=趙四, sex=null]
[Person]調用了DisposableBean的destroy方法了

   從上面的運行結果可以看出:

    1)Person的name值由"張三"變爲"趙四";

    2)BeanFactoryPostProcessor方法執行順序先於BeanPostProcessor接口中方法,且在bean實例化之前執行;

    3)BeanFactoryPostProcessor改變bean的定義,實例化出來的對象變了:“Person有參構造器:[name=張三,sex=男] ”變成了“Person [name=趙四, sex=null]

    4)BeanPostProcessor在bean創建對象實例化後,初始化(bean執行afterPropertiesSet方法或init-method方法)前後進行攔截工作;

 三.原理

   接下來我們通過debug代碼來查看BeanFactoryPostProcessor的執行流程,從AbstractApplicationContext類的構造器方法看起,這裏面對應着容器初始化的基本操作;

  1.在測試main方法中下面的代碼打斷點:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ExtConfig.class);

  2.從下圖可以看出容器先註冊配置類ExtConfig的定義信息,然後進行refresh刷新容器;

 

  3.先來看看register(componentClasses)註冊流程:從class文件讀取信息解析成beanDefinition

AnnotationConfigApplicationContextpublic void register(Class<?>... componentClasses) {
        Assert.notEmpty(componentClasses, "At least one component class must be specified");
        this.reader.register(componentClasses);
    }


AnnotatedBeanDefinitionReader:註冊beanDefinition
    public void register(Class<?>... componentClasses) {
        for (Class<?> componentClass : componentClasses) {
            registerBean(componentClass);
        }
    }
    
    public void registerBean(Class<?> beanClass) {
        doRegisterBean(beanClass, null, null, null, null);
    }
        
    //Register a bean from the given bean class, deriving its metadata from  class-declared annotations.
    //從class文件中讀取bean的定義信息,並註冊到容器中
    private <T> void doRegisterBean(Class<T> beanClass, @Nullable String name,
        @Nullable Class<? extends Annotation>[] qualifiers, @Nullable Supplier<T> supplier,
        @Nullable BeanDefinitionCustomizer[] customizers) {
        //得到bean的所有定義信息:元數據metadata、作用域scope、初始化方法名字initMethodName等等
        AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(beanClass);
        if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
            return;
        }
        //爲bean實例創建一個特殊的回調信號
        abd.setInstanceSupplier(supplier);
        //獲取作用域的數據:單例
        ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
        //設置bean爲單例
        abd.setScope(scopeMetadata.getScopeName());
        //獲取beanName
        String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
        //處理一些註釋信息:lazyInit、primary、dependsOn、role、description
        AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
        if (qualifiers != null) {//qualifiers = nul 跳過
            for (Class<? extends Annotation> qualifier : qualifiers) {
                if (Primary.class == qualifier) {
                    abd.setPrimary(true);
                }
                else if (Lazy.class == qualifier) {
                    abd.setLazyInit(true);
                }
                else {
                    abd.addQualifier(new AutowireCandidateQualifier(qualifier));
                }
            }
        }
        if (customizers != null) {//customizers = null跳過
            for (BeanDefinitionCustomizer customizer : customizers) {
                customizer.customize(abd);
            }
        }
        //創建一個BeanDefinitionHolder
        BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
        definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
        //容器中註冊beanDefinition
        BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
    }

  4.refresh刷新容器:invokeBeanFactoryPostProcessors方法用來找出所有beanFactory後置處理器,並且調用這些處理器來改變bean的定義

    public void refresh() throws BeansException, IllegalStateException {
        //來個鎖,不然 refresh() 還沒結束,你又來個啓動或銷燬容器的操作,那不就亂套了嘛
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            //容器刷新前的處理方法:獲取啓動的系統時間、設置active活躍標識、開始打印日誌、設置環境變量、設置容器監聽器、設置容器事件
            prepareRefresh();

            // Tell the subclass to refresh the internal bean factory.
            //刷新bean工廠並獲取到bean工廠
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // Prepare the bean factory for use in this context.
            // bean工廠的初始化操作:設置類加載器、設置bean表達式解析器、設置bean後置處理器等等;
            prepareBeanFactory(beanFactory);

            try {
                // Allows post-processing of the bean factory in context subclasses.
                // 【這裏需要知道 BeanFactoryPostProcessor 這個知識點,Bean 如果實現了此接口,
                // 那麼在容器初始化以後,Spring 會負責調用裏面的 postProcessBeanFactory 方法。】
                 // 這裏是提供給子類的擴展點,到這裏的時候,所有的 Bean 都加載、註冊完成了,但是都還沒有初始化
                 // 具體的子類可以在這步的時候根據自身業務添加或修改一些特殊的 beanFactory屬性
                postProcessBeanFactory(beanFactory);

                // Invoke factory processors registered as beans in the context.
                //找出所有beanFactory後置處理器,並且調用這些處理器來改變bean的定義
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.
//註冊bean後置處理器
registerBeanPostProcessors(beanFactory); // Initialize message source for this context.
//初始化容器的信息源
initMessageSource(); // Initialize event multicaster for this context.
//初始化事件監聽多路廣播器
initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses.
//是個空殼方法,在AnnotationApplicationContex上下文中沒有實現,可能在spring後面的版本會去擴展。
//與Web上下文有關
onRefresh(); // Check for listener beans and register them. //註冊監聽器 registerListeners(); // Instantiate all remaining (non-lazy-init) singletons. //對象的創建:初始化剩下所有的(非懶加載的)單實例對象【從這裏可以看出beanFactory後置處理器在初始化其他組件之前執行】 finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event.
//刷新完成工作,包括初始化LifecycleProcessor,發佈刷新完成事件等
finishRefresh(); } catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn("Exception encountered during context initialization - " + "cancelling refresh attempt: " + ex); } // Destroy already created singletons to avoid dangling resources.
//銷燬已經初始化的 singleton 的 Beans,以免有些 bean 會一直佔用資源
destroyBeans(); // Reset 'active' flag.
//取消刷新的標誌
cancelRefresh(ex); // Propagate exception to caller. throw ex; } finally { // Reset common introspection caches in Spring's core, since we // might not ever need metadata for singleton beans anymore... resetCommonCaches(); } } }

  5.打開invokeBeanFactoryPostProcessors方法,如下所示,實際操作是委託PostProcessorRegis

trationDelegate去完成的:調用getBeanFactoryPostProcessors()方法獲取手工註冊到ApplicationCon

text的容器後置處理器集合

    protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());

        // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
        // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
        if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
            beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
            beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
        }
    } 

  1)在調用PostProcessorRegistrationDelegate類的invokeBeanFactoryPostProcessors方法時,注意第二個入參是getBeanFactoryPostProcessors()方法,該方法返回的是applicationContext的成員變量beanFactoryPostProcessors,該成員變量的值來自AbstractApplicationContext.addBeanFactoryPostProcessor方法被調用的時候:

    private final List<BeanFactoryPostProcessor> beanFactoryPostProcessors = new ArrayList<>();
    @Override
    public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor) {
        Assert.notNull(postProcessor, "BeanFactoryPostProcessor must not be null");
        this.beanFactoryPostProcessors.add(postProcessor);
    }

    public List<BeanFactoryPostProcessor> getBeanFactoryPostProcessors() {
        return this.beanFactoryPostProcessors;
    }

    2)AbstractApplicationContext.addBeanFactoryPostProcessor方法是留給業務擴展時調用的,例如在springboot初始化時,ConfigurationWarningsApplicationContextInitializer類的initialize方法中就有調用:

@Override
public void initialize(ConfigurableApplicationContext context) {
    context.addBeanFactoryPostProcessor(
            new ConfigurationWarningsPostProcessor(getChecks()));
}

6.看過了如何添加BeanFactoryPostProcessor,再回到PostProcessorRegistrationDelegate.invok

eBeanFactoryPostProcessors方法:實例化並調用所有已註冊的BeanFactoryPostProcessor bean;

流程是:

  1)beanFactory是BeanDefinitionRegistry類型時,此條件下完成如下流程:

    1.遍歷傳入後置處理器集合查找類型爲BeanDefinitionRegistryPostProcessor的後置處理器,調用後置處理器的postProcessBeanDefinitionRegistry方法;

    2.在容器中查找所有的實現了PriorityOrdered接口的BeanDefinition

RegistryPostProcessor集合,對後置處理器集合排序,遍歷,執行後置處理的postProcessBeanDefinitionRegistry方法;

    3.在容器中查找所有實現了Ordered接口的BeanDefinitionRegistryPostProcessor集合,對後置處理器集合排序,遍歷,執行後置處理的postProcessBeanDefinitionRegistry方法;

    4.在容器中查找其它(未實現排序接口)的BeanDefinitionRegistryPostProcessor並添加到集合nonOrderedPostProcessors中,對後置處理器集合排序,遍歷,執行後置處理的postProcessBeanDefinitionRegistry方法;

    5.當前所有的BeanDefinitionRegistryPostProcessor處理器的方法postProcessBeanD

efinitionRegistry 執行完畢後,執行其父類postProcessBeanFactory方法;

    6.執行所有非BeanDefinitionRegistryPostProcessor類型的後置處理器的postProcessB

eanFactory方法;

  2)beanFactory非BeanDefinitionRegistry類型時,此條件下完成如下流程:

    1.遍歷傳入後置處理器集合,執行後置處理器的postProcessBeanFactory方法;

    2.在容器中(beanFactory.getBeanNamesForType)查找所有的實現了PriorityOrdered接口的BeanFactoryPostProcessor集合,對後置處理器集合排序,遍歷,執行後置處理;

    3.在容器中查找所有實現了Ordered接口的BeanFactoryPostProcessor集合,對後置處理器集合排序,遍歷,執行後置處理;

    4.在容器中查找其它(未實現排序接口)的BeanFactoryPostProcessor並添加到集合nonOrderedPostProcessors中,對後置處理器集合排序,遍歷,執行後置處理;

    public static void invokeBeanFactoryPostProcessors(
            ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

        // Invoke BeanDefinitionRegistryPostProcessors first, if any.
        Set<String> processedBeans = new HashSet<>();
        //如果beanFactory實現了BeanDefinitionRegistry
        if (beanFactory instanceof BeanDefinitionRegistry) {
            BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
            List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
            List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

            for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
                //如果beanFactoryPostProcessor實現了BeanDefinitionRegistryPostProcessor,分別放入兩個集合:registryProcessors 和 regularPostProcessors
                if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                    BeanDefinitionRegistryPostProcessor registryProcessor =
                            (BeanDefinitionRegistryPostProcessor) postProcessor;
                    registryProcessor.postProcessBeanDefinitionRegistry(registry);
                    registryProcessors.add(registryProcessor);
                }
                else {
                    regularPostProcessors.add(postProcessor);
                }
            }

            // Do not initialize FactoryBeans here: We need to leave all regular beans
            // uninitialized to let the bean factory post-processors apply to them!
            // Separate between BeanDefinitionRegistryPostProcessors that implement
            // PriorityOrdered, Ordered, and the rest.
            List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

            // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
            //找出所有實現了BeanDefinitionRegistryPostProcessor接口和PriorityOrdered接口的bean,放入registryProcessors集合,
            //放入根據Set接口來排序,然後這些bean會被invokeBeanDefinitionRegistryPostProcessors方法執行;
            String[] postProcessorNames =
                    beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            for (String ppName : postProcessorNames) {
                if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                    currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    processedBeans.add(ppName);
                }
            }
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            registryProcessors.addAll(currentRegistryProcessors);
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
            currentRegistryProcessors.clear();

            // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
            //找出所有實現了BeanDefinitionRegistryPostProcessor接口和Ordered接口的bean,放入registryProcessors集合,
            //放入根據Set接口來排序,然後這些bean會被invokeBeanDefinitionRegistryPostProcessors方法執行;
            postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            for (String ppName : postProcessorNames) {
                if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
                    currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    processedBeans.add(ppName);
                }
            }
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            registryProcessors.addAll(currentRegistryProcessors);
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
            currentRegistryProcessors.clear();

            // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
            //對於那些實現了BeanDefinitionRegistryPostProcessor接口,但是沒有實現PriorityOrdered和Ordered的bean也被找出來,
            //然後這些bean會被invokeBeanDefinitionRegistryPostProcessors方法執行;
            boolean reiterate = true;
            while (reiterate) {
                reiterate = false;
                postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
                for (String ppName : postProcessorNames) {
                    if (!processedBeans.contains(ppName)) {
                        currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                        processedBeans.add(ppName);
                        reiterate = true;
                    }
                }
                sortPostProcessors(currentRegistryProcessors, beanFactory);
                registryProcessors.addAll(currentRegistryProcessors);
                invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
                currentRegistryProcessors.clear();
            }

            // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
            //registryProcessors和regularPostProcessors集合被invokeBeanFactoryPostProcessors執行
            invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
            invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
        }

        else {
            // Invoke factory processors registered with the context instance.
            //入參中的BeanFactoryPostProcessor,沒有實現BeanDefinitionRegistryPostProcessor的那些bean,被invokeBeanFactoryPostProcessors執行
            invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
        }

        // Do not initialize FactoryBeans here: We need to leave all regular beans
        // uninitialized to let the bean factory post-processors apply to them!
        String[] postProcessorNames =
                beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

        // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
        // Ordered, and the rest.
        //找出實現了BeanFactoryPostProcessor接口的bean,注意這裏已將上面實現了BeanDefinitionRegistryPostProcessor接口的bean給剔除了,
        //將這些bean分爲三類:實現了PriorityOrdered接口的放入priorityOrderedPostProcessors,
        //實現了Ordered接口的放入orderedPostProcessorNames,其他的放入nonOrderedPostProcessorNames
        //自定義的實現BeanFactoryPostProcessor接口的bean就會在nonOrderedPostProcessorNames被找出來
        List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
        List<String> orderedPostProcessorNames = new ArrayList<>();
        List<String> nonOrderedPostProcessorNames = new ArrayList<>();
        for (String ppName : postProcessorNames) {
            if (processedBeans.contains(ppName)) {
                // skip - already processed in first phase above
            }
            else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
            }
            else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
                orderedPostProcessorNames.add(ppName);
            }
            else {
                nonOrderedPostProcessorNames.add(ppName);
            }
        }

        // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
        //priorityOrderedPostProcessors先排序再被invokeBeanFactoryPostProcessors執行
        sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
        invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

        // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
        //orderedPostProcessorNames先被遍歷加入到orderedPostProcessors,再被排序,最後才被invokeBeanFactoryPostProcessors執行
        List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
        for (String postProcessorName : orderedPostProcessorNames) {
            orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
        }
        sortPostProcessors(orderedPostProcessors, beanFactory);
        invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

        // Finally, invoke all other BeanFactoryPostProcessors.
        //nonOrderedPostProcessorNames也是先被遍歷到nonOrderedPostProcessors,再被invokeBeanFactoryPostProcessors執行
        List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
        for (String postProcessorName : nonOrderedPostProcessorNames) {
            nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
        }
        //這時纔是執行自定義BeanFactoryPostProcessor的postProcessBeanFactory
        invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

        // Clear cached merged bean definitions since the post-processors might have
        // modified the original metadata, e.g. replacing placeholders in values...
        beanFactory.clearMetadataCache();
    }
    • getBeanNamesForType():根據傳遞的類型獲取容器中的beanName
      // type:類的類型名稱
      // includeNonSingletons:返回數據包含了非單例beanName
      // allowEagerInit: 可以提前加載初始化
      public String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {
          if (!isConfigurationFrozen() || type == null || !allowEagerInit) {
             // 不可用緩存、類型無效、不允許提前加載初始化
             // 需要獲取當前type的原始類型,繼續獲取數據
              return doGetBeanNamesForType(ResolvableType.forRawClass(type), includeNonSingletons, allowEagerInit);
          }
          Map<Class<?>, String[]> cache =
                  (includeNonSingletons ? this.allBeanNamesByType : this.singletonBeanNamesByType);
          String[] resolvedBeanNames = cache.get(type);
          // 如果緩存已經存儲了該數據,則無需再計算,直接返回即可
          if (resolvedBeanNames != null) {
              return resolvedBeanNames;
          }
          resolvedBeanNames = doGetBeanNamesForType(ResolvableType.forRawClass(type), includeNonSingletons, true);
          // 這一步就是真正的獲取數據,遍歷beanDefinitionNames的每一個數據,符合要求的就會加入到返回的列表中
          
          if (ClassUtils.isCacheSafe(type, getBeanClassLoader())) {
              cache.put(type, resolvedBeanNames);
              // 便於下一次獲取,加入緩存中
          }
          return resolvedBeanNames;
      }
    • getBean後面還有一個參數BeanFactoryPostProcessor.class,注意看這個函數,會發現返回的是一個抽象類,結論就是nonOrderedPostProcessors添加的不是bean實例,而是beandefinition,在實例化前。
  7.從上面代碼中可以看出所有實現了BeanFactoryPostProcessor接口的bean,都被作爲入參,然後調用了invokeBeanDefinitionRegistryPostProcessors或者invokeBeanFactoryPostProcessors方法去處理:對每個BeanFactoryPostProcessor接口的實現類,都調用了其接口方法,不同的是,對於實現了BeanDefinitionRegistryPostProcessor接口的bean,調用其postProcessBeanDefinitionRegistry方法的時候,入參是BeanDefinitionRegistry,而非BeanFactory,因此,實現了BeanDefinitionRegistryPostProcessor接口的bean,其postProcessBeanDefinitionRegistry在被調用時,可以通過入參BeanDefinitionRegistry來做更多和bean的定義有關的操作,例如註冊bean;
    /**
     * Invoke the given BeanDefinitionRegistryPostProcessor beans.
     */
    private static void invokeBeanDefinitionRegistryPostProcessors(
            Collection<? extends BeanDefinitionRegistryPostProcessor> postProcessors, BeanDefinitionRegistry registry) {

        for (BeanDefinitionRegistryPostProcessor postProcessor : postProcessors) {
            postProcessor.postProcessBeanDefinitionRegistry(registry);
        }
    }
        
    /**
     * Invoke the given BeanFactoryPostProcessor beans.
     */    
    private static void invokeBeanFactoryPostProcessors(
        Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {

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

  8.BeanFactoryPostProcessor 執行的整體流程:

    1)ApplicationContext的refresh方法

    2)ApplicationContext的invokeBeanFactoryPostProcessors方法

    3)PostProcessorRegistrationDelegate的invokeBeanFactoryPostProcessors

  9.BeanFactoryPostProcessor執行的優先級:

    1)首先是實現了PriorityOrdered接口的,排序執行

    2)下來是實現了Ordered接口的,排序執行

    3)最後是其它(未實現排序接口),順序執行

  10.BeanFactoryPostProcessor獲取機制:

    1)首先獲取手動註冊ApplicationContext的集合

    2)再次是通過beanFactory.getBeanNamesForType查找所有已註冊的BeanFactory

PostProcessor的bean定義並實例化。

四.總結

  1. ApplicationContext擴展類可以調用AbstractApplicationContext.addBeanFactoryPostProcessor方法,將自定義的BeanFactoryPostProcessor實現類保存到ApplicationContext中;
  2. Spring容器初始化時,上一步中被加入到ApplicationContext的bean會被優先調用其postProcessBeanFactory方法;
  3. 自定義的BeanFactoryPostProcessor接口實現類,也會被找出來,然後調用其postProcessBeanFactory方法;
  4. postProcessBeanFactory方法被調用時,beanFactory會被作爲參數傳入,自定義類中可以使用該參數來處理bean的定義,達到業務需求;
  5. 此時的Spring容器還沒有開始實例化bean,因此自定義的BeanFactoryPostProcessor實現類不要做與bean實例有關的操作,而是做一些與bean定義有關的操作,例如修改某些字段的值,這樣後面實例化的bean的就會有相應的改變

   6.Spring主要將BeanFactoryPostProcessor劃分了兩類:

    • 正常的BeanFactoryPostProcessor
    • BeanDefinitionRegistry類型的BeanDefinitionRegistryPostProcessor

  7.在執行流程中可以看到Spring先執行了BeanDefinitionRegistryPostProcessor類型的postProcessBeanDefinitionRegistry方法,再執行BeanDefinitionRegistryPostProcessor和正常BeanFactoryPostProcessor的postProcessBeanFactory方法。

  8.Spring對BeanDefinitionRegistryPostProcessor的解釋是:允許在正常的BeanFactoryPostProcessor執行檢測開始之前註冊更多的自定義bean。也就是說BeanDefinitionRegistryPostProcessor的方法postProcessBeanDefinitionRegistry可以在後置處理器執行前自定義註冊更多的BeanDefinition

  例如:Spring實現的ConfigurationClassPostProcessor用於註冊註解@Configuration標識的類裏面定義的BeanDefinition。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章