Spring 执行顺序:Bean 的生命周期

回目录

代码:https://gitee.com/free/boot-order/tree/master/src/main/java/com/github/abel533/lifecycle

通过本例了解一个 Bean 中所有生命周期方法执行的顺序。

BeanLifecycle 实现了几乎全部方式的初始化和关闭方法,在当前例子中,执行顺序如下:

  1. @PostConstruct
  2. InitializingBean#afterPropertiesSet
  3. @Bean(initMethod)
  4. SmartLifecycle#isRunning = falsetrue时不会执行下面的 start)
  5. SmartLifecycle#start
  6. SmartLifecycle#isRunning = true (false 时不会执行下面的 stop)
  7. SmartLifecycle#stop
  8. @PreDestroy
  9. DisposableBean#destroy
  10. @Bean(destroyMethod)

第 0 个 @PostConstruct,在 InitDestroyAnnotationBeanPostProcessor 实现的 BeanPostProcessor#postProcessBeforeInitialization 方法中执行。

更早是在 AbstractAutowireCapableBeanFactory#initializeBean 中执行,代码如下(有删减):

protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
    Object wrappedBean = bean;
    if (mbd == null || !mbd.isSynthetic()) {
        //执行 0
        wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
    }
    try {
        //执行 1 和 2
        invokeInitMethods(beanName, wrappedBean, mbd);
    }
    catch (Throwable ex) {
    }
    return wrappedBean;
}

第 1 和 2 在 AbstractAutowireCapableBeanFactory 类中,该类继承的 AbstractBeanFactory#createBean 方法中,按照 doCreateBean > initializeBean > invokeInitMethods 顺序调用,在 invokeInitMethods 方法中执行了初始化方法。代码如下(有删减):

protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
        throws Throwable {
    boolean isInitializingBean = (bean instanceof InitializingBean);
    if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
        if (System.getSecurityManager() != null) {
            try {
                AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
                    //调用方法 1
                    ((InitializingBean) bean).afterPropertiesSet();
                    return null;
                }, getAccessControlContext());
            }
            catch (PrivilegedActionException pae) {}
        }
        else {
             //调用方法 1
            ((InitializingBean) bean).afterPropertiesSet();
        }
    }

    if (mbd != null && bean.getClass() != NullBean.class) {
        String initMethodName = mbd.getInitMethodName();
        if (StringUtils.hasLength(initMethodName) &&
                !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
                !mbd.isExternallyManagedInitMethod(initMethodName)) {
            //调用方法 2
            invokeCustomInitMethod(beanName, bean, mbd);
        }
    }
}

代码有删减,这里先调用 InitializingBean,然后是配置的 InitMethodName 对应的方法。

3 和 4 是先判断 isRunning,如果没有运行(false)在执行 start,这里是在 SpringApplication#refresh 后,层层调用,最终在 DefaultLifecycleProcessor 中执行,代码如下(有删减):

private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
    Lifecycle bean = lifecycleBeans.remove(beanName);
    if (bean != null && bean != this) {
        //判断是否 SmartLifecycle 并且自动运行
        if (!bean.isRunning() &&
                (!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
            try {
                //调用方法
                bean.start();
            }
            catch (Throwable ex) {}
        }
    }
}

5 和 6 也在 DefaultLifecycleProcessor 中执行 doStop 方法,逻辑和上面类似,这里不贴了,此时的方法堆栈如下:

doStop:231, DefaultLifecycleProcessor (org.springframework.context.support)
access$300:53, DefaultLifecycleProcessor (org.springframework.context.support)
stop:377, DefaultLifecycleProcessor$LifecycleGroup (org.springframework.context.support)
stopBeans:210, DefaultLifecycleProcessor (org.springframework.context.support)
onClose:128, DefaultLifecycleProcessor (org.springframework.context.support)
doClose:1003, AbstractApplicationContext (org.springframework.context.support)
close:961, AbstractApplicationContext (org.springframework.context.support)
main:11, BeanApplication (com.github.abel533.lifecycle)

这里需要关注 AbstractApplicationContext#doClose,代码如下(有删减):

protected void doClose() {
    if (this.active.get() && this.closed.compareAndSet(false, true)) {
        // Stop all Lifecycle beans, to avoid delays during individual destruction.
        if (this.lifecycleProcessor != null) {
            //这里对应 6 的 stop
            this.lifecycleProcessor.onClose();
        }
        // Destroy all cached singletons in the context's BeanFactory.
        // 这里对应 7 的 @PreDestroy
        destroyBeans();
    }
}

7 最早调用该方法的地方见上面代码,这里和 @PostConstruct 类似,在 InitDestroyAnnotationBeanPostProcessor 实现的 DestructionAwareBeanPostProcessor#postProcessBeforeDestruction 方法中执行。

8 和 9 在 DisposableBeanAdapter 实现的 DisposableBean#destroy 中,代码如下(有删减):

@Override
public void destroy() {
    if (this.invokeDisposableBean) {
        try {
            if (System.getSecurityManager() != null) {
                AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
                    //执行方法 8
                    ((DisposableBean) this.bean).destroy();
                    return null;
                }, this.acc);
            }
            else {
                //执行方法 8
                ((DisposableBean) this.bean).destroy();
            }
        }
        catch (Throwable ex) {
        }
    }

    if (this.destroyMethod != null) {
        //执行方法 9
        invokeCustomDestroyMethod(this.destroyMethod);
    }
    else if (this.destroyMethodName != null) {
        Method methodToCall = determineDestroyMethod(this.destroyMethodName);
        if (methodToCall != null) {
            //执行方法 9
            invokeCustomDestroyMethod(methodToCall);
        }
    }
}

通过以上代码和分析可以了解 Bean 的整个生命周期了。

发布了299 篇原创文章 · 获赞 1651 · 访问量 595万+

猜你喜欢

转载自blog.csdn.net/isea533/article/details/100146750
今日推荐