IOC容器启动阶段

IOC容器要为我们提供服务需要经过两个阶段:
①容器启动阶段
②bean实例化阶段

容器启动阶段主要是在真正构造bean前,为构造工作提供图纸。就比如<bean id="..." class="...">...</bean>,这是我们人手动写的,ioc要把它转为bean的图纸:也就是转化为一个BeanDefinition,然后把BeanDefinition注册到BeanDefinitionRegistry

Spring在容器启动阶段提供了一些容器扩展点,我们可以在扩展点上做自己的一些操作,比如对BeanDefinition做一些操作。BeanFactoryPostProcessor提供了这种功能。

@FunctionalInterface
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. (所有的BeanDifinition已经被加载,但是还没有bean被实例化)
     * 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
     */
    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}

可以有多个BeanFactoryPostProcessor 先后进行处理,但是要有顺序,即实现Ordered接口。

主要几个BeanFactoryPostProcessor
这里写图片描述

猜你喜欢

转载自blog.csdn.net/a_842297171/article/details/79967005