spring中的Lifecycle

spring中的Lifecycle

Lifecycle介绍

Lifecycle是spring中的生命循环接口,实现该接口的类将会可以启动,关闭。在AbstractApplicationContext通过ConfigurableApplicationContext接口中就实现了Lifecycle,当上下文调用start()函数时,就会去BeanFactory中找实现了Lifecycle接口的Bean,并调用该bean的start()函数。

AbstractApplicationContext源码分析

初始化LifecycleProcessor

AbstractApplicationContext中有方法initLifecycleProcessor(),用来初始化生命周期执行器,用以执行容器中所有实现了Lifecycle接口的bean。

/**
	 * Initialize the LifecycleProcessor.
	 * Uses DefaultLifecycleProcessor if none defined in the context.
	 * @see org.springframework.context.support.DefaultLifecycleProcessor
	 */
protected void initLifecycleProcessor() {
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    //参试在容器中获取LifecycleProcessor
    if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) {
        this.lifecycleProcessor =
            beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class);
        if (logger.isTraceEnabled()) {
            logger.trace("Using LifecycleProcessor [" + this.lifecycleProcessor + "]");
        }
    }
    else {
        //容器没有则使用DefaultLifecycleProcessor
        DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor();
        //设置BeanFactory,因为要对BeanFactory里面的Lifecycle bean调用start,stop
        defaultProcessor.setBeanFactory(beanFactory);
        this.lifecycleProcessor = defaultProcessor;
        //注册到容器中
        beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);
        if (logger.isTraceEnabled()) {
            logger.trace("No '" + LIFECYCLE_PROCESSOR_BEAN_NAME + "' bean, using " +
                         "[" + this.lifecycleProcessor.getClass().getSimpleName() + "]");
        }
    }
}
start()和stop()

AbstractApplicationContext中的start()和stop()方法用来启动生命周期。通过LifecycleProcessor来去启动BeanFactory中的实现了Lifecycle接口的bean。

//---------------------------------------------------------------------
// Implementation of Lifecycle interface
//---------------------------------------------------------------------

@Override
public void start() {
    getLifecycleProcessor().start();
    publishEvent(new ContextStartedEvent(this));
}

@Override
public void stop() {
    getLifecycleProcessor().stop();
    publishEvent(new ContextStoppedEvent(this));
}

DefaultLifecycleProcessor源码分析

start()和startBeans()

DefaultLifecycleProcessor通过从BeanFactory中获取LifecycleBean,并调用它们的start()方法

@Override
public void start() {
    startBeans(false);
    this.running = true;
}

// Internal helpers

private void startBeans(boolean autoStartupOnly) {
    //这里是从BeanFactory中获取所有LifecycleBean
    Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
    Map<Integer, LifecycleGroup> phases = new HashMap<>();
    lifecycleBeans.forEach((beanName, bean) -> {
        if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
            int phase = getPhase(bean);
            LifecycleGroup group = phases.get(phase);
            if (group == null) {
                group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
                phases.put(phase, group);
            }
            group.add(beanName, bean);
        }
    });
    if (!phases.isEmpty()) {
        List<Integer> keys = new ArrayList<>(phases.keySet());
        Collections.sort(keys);
        for (Integer key : keys) {
            //最后会调用该bean的start()方法
            phases.get(key).start();
        }
    }
}

Lifecycle Bean使用示例

MyLifecycleBean
/**
 * @author liangwy 
 * @since 2022-5-17
 */
public class MyLifecycleBean implements Lifecycle {
    
    private Boolean flag = false;
    
    @Override
    public void start() {
        this.flag = true;
        System.out.println("启动MyLifecycleBean");
    }

    @Override
    public void stop() {
        this.flag = false;
        System.out.println("关闭MyLifecycleBean");
    }

    @Override
    public boolean isRunning() {
        return flag;
    }
}
测试
public static void main(String[] args) {

    AnnotationConfigApplicationContext applicationContext = runAnnotationConfigApplicationContext();
    applicationContext.start();
    MyLifecycleBean myLifecycleBean = applicationContext.getBean("myLifecycleBean", MyLifecycleBean.class);
    System.out.println(myLifecycleBean.isRunning());
    applicationContext.stop();
    System.out.println(myLifecycleBean.isRunning());
}

/**
     * 注解配置方式创建上下文
     * @return
     */
public static AnnotationConfigApplicationContext runAnnotationConfigApplicationContext(){
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationMain.class);
    applicationContext.getBeanFactory().registerCustomEditor(Date.class,TestPropertyEditorSupport.class);
    return applicationContext;
}

结果打印

启动MyLifecycleBean
true
关闭MyLifecycleBean
false

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值