美文网首页
springbean创建过程及执行顺序

springbean创建过程及执行顺序

作者: 呆呆猿 | 来源:发表于2020-07-25 01:39 被阅读0次

    1.如果框架里有实现BeanDefinitionRegistryPostProcessor的,最先执行2.BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry方法
    比如 mybitis整合spring用到了

    3.如果框架里有实现BeanFactoryPostProcessor的,最先执行BeanFactoryPostProcessor.postProcessBeanFactory方法
    构造方法

    4.set 设置属性

    5.如果实现了Aware 执行Aware实现方法BeanNameAware.setBeanName -->BeanFactoryAware。setBeanFactory

    6.如果框架里有实现BeanPostProcessor (所有bean创建都会执行)就执行BeanPostProcessor.postProcessBeforeInitialization

    7.如果实现了InitializingBean接口就执行afterPropertiesSet

    8.如果设置了initMethod 就执行此方法

    9.如果框架里有实现BeanPostProcessor(所有bean创建都会执行)就执行BeanPostProcessor.postProcessAfterInitialization(和4对应)

    springbean创建过程.png

    spring启动过程分析
    AbstractApplicationContext.refresh

    public void refresh() throws BeansException, IllegalStateException {
    
    synchronized (this.startupShutdownMonitor) {
                // Prepare this context for refreshing.
                prepareRefresh();
    
                // Tell the subclass to refresh the internal bean factory.
                ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
                           
                // Prepare the bean factory for use in this context.
                prepareBeanFactory(beanFactory);
    
                try {
                                  
                    // Allows post-processing of the bean factory in context subclasses.
                    postProcessBeanFactory(beanFactory);
    
                                     //此处会执行BeanFactoryPostProcessor
                    // Invoke factory processors registered as beans in the context.
                    invokeBeanFactoryPostProcessors(beanFactory);
    
                    // Register bean processors that intercept bean creation.
                    registerBeanPostProcessors(beanFactory);
    
                                    //此处会执行BeanPostProcessor
                    // 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.
                    finishBeanFactoryInitialization(beanFactory);
    
                    // Last step: publish corresponding event.
                    finishRefresh();
                }
    }
    

    相关文章

      网友评论

          本文标题:springbean创建过程及执行顺序

          本文链接:https://www.haomeiwen.com/subject/fopylktx.html