• Spring BeanFactory 初始化 和 Bean 生命周期


    (version:spring-context-4.3.15.RELEASE)

    AbstractApplicationContext#refresh()

    public void refresh() throws BeansException, IllegalStateException {
        // Prepare this context for refreshing.
        prepareRefresh();
    
        // Tell the subclass to refresh the internal bean factory.
        // 1. 创建一个 BeanFactory 的实例(DefaultListableBeanFactory)
        // 2. 加载bean的定义 BeanDefinition,并注册到 beanFactory 中
        // loadBeanDefinitions(beanFactory) --> registerBeanDefinitions()
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    
        // Prepare the bean factory for use in this context.
        // 给 beanFactory 做上些前期配置:
        // 1. 添加 BeanPostProcessor
        // 2. 注册一些可以解析的依赖和 bean,如:BeanFactory, ApplicationContext
        prepareBeanFactory(beanFactory);
    
        // Allows post-processing of the bean factory in context subclasses.
        // 允许子类对 beanFactory 对象做一些后置处理
        postProcessBeanFactory(beanFactory);
    
        // Invoke factory processors registered as beans in the context.
        // 调用 beanFactoryPostProcessor,允许用户对 beanFactory 做修改(扩展点),比如添加、修改或删除 beanFactory 中注册的 BeanDefinition 等
        // 按顺序调用:PriorityOrdered 类型的 --> Ordered 类型的 --> the rest
        invokeBeanFactoryPostProcessors(beanFactory);
    
        // Register bean processors that intercept bean creation.
        // 注册 beanPostProcessor 到 beanFactory 中
        registerBeanPostProcessors(beanFactory);
    
        // Initialize message source for this context.
        initMessageSource();
    
        // Initialize event multicaster for this context.
        // 初始化事件广播器
        initApplicationEventMulticaster();
    
        // Initialize other special beans in specific context subclasses.
        onRefresh();
    
        // Check for listener beans and register them.
        registerListeners();
    
        // Instantiate all remaining (non-lazy-init) singletons.
        // 完成 beanFactory 的初始化:解析 bean 的依赖,完成 bean 实例的初始化
        finishBeanFactoryInitialization(beanFactory);
    
        // Last step: publish corresponding event.
        finishRefresh();
    }

    beanFactory 实例化后,会向 beanFactory 注册 bean 的定义 BeanDefinition。
    最后 beanFactory 会进行初始化,执行 finishBeanFactoryInitialization(beanFactory) 时就开始了 bean 的生命周期

    bean 的生命周期:
    1. 实例化 Instantiation
    2. 属性赋值 Populate
    3. 初始化 Initialization
    4. 销毁 Destruction

    实例化 -> 属性赋值 -> 初始化 -> 销毁
    // 忽略了无关代码
    protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) throws BeanCreationException {
    
        // Instantiate the bean.
        BeanWrapper instanceWrapper = null;
        if (instanceWrapper == null) {
            // 实例化阶段!
            instanceWrapper = createBeanInstance(beanName, mbd, args);
        }
    
        // Initialize the bean instance.
        Object exposedObject = bean;
        try {
            // 属性赋值阶段!填充 bean 属性:这一步主要是对bean的依赖属性进行注入(@Autowired)
            populateBean(beanName, mbd, instanceWrapper);
            // 初始化阶段!
            exposedObject = initializeBean(beanName, exposedObject, mbd);
        }
    
    }

    BeanPostProcessor 和 BeanPostProcessor 执行顺序相同:

    PriorityOrdered 类型的 --> Ordered 类型的 --> the rest

    参考:
    https://www.jianshu.com/p/1dec08d290c1
    https://www.cnblogs.com/kevin-yuan/p/6402391.html

  • 相关阅读:
    fastjson的@JSONField注解
    Java 日期比较大小
    linux 查看文件显示行号
    Java double 加、减、乘、除
    Java 身份证判断性别获取年龄
    linux 查看端口被占用
    Unable to complete the scan for annotations for web application [/wrs] due to a StackOverflowError. Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies.
    nginx 返回数据不完整
    linux redis 启动 overcommit_memory
    IntelliJ IDEA 常用设置
  • 原文地址:https://www.cnblogs.com/kevin-yuan/p/11431223.html
Copyright © 2020-2023  润新知