spring容器生命周期回调

本文探讨了如何参与Spring容器的生命周期回调,介绍了Lifecycle、LifecycleProcessor和SmartLifecycle接口。Lifecycle接口允许对象在ApplicationContext启动和关闭时进行操作。LifecycleProcessor处理容器的refreshed和closed事件。SmartLifecycle接口通过getPhase()方法定义启动和停止的顺序,并在停止时提供异步关闭功能。此外,文章还简单说明了SmartLifecycle接口中各个方法的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                               spring容器生命周期回调

上一篇文章中我们谈了spring bean生命周期回调,这些都是建立在容器已经启动的前提下,那么如果想参与spring 容器的生命周期回调,我们该怎办?spring提供了一些接口

Lifecycle 

接口如下

public interface Lifecycle {

    void start();

    void stop();

    boolean isRunning();
}

任何Spring管理的对象都可以实现此接口。当ApplicationContext接口启动和关闭时,它会调用本容器内所有的Lifecycle实现。

LifecycleProcessor 

它继承Lifcycle接口。同时,增加了2个方法,用于处理容器的refreshed和closed事件,接口如下

public interface LifecycleProcessor extends Lifecycle {

    void onRefresh();

    void onClose();
}

SmartLifecycle

若两个对象有依赖关系(这种依赖不一定是一个bean引用另一个bean),希望某些bean先初始化,完成一些工作后,再初始化另一些bean。在此场景中,可以使用SmartLifecycle接口,该接口有个方法getPhase(),实现了父接口Phased,它返回一个int型数字,表明执行顺序:

public interface Phased {
    int getPhase();
}
public interface SmartLifecycle extends Lifecycle, Phased {

    boolean isAutoStartup();

    void stop(Runnable callback);
}

启动时,最小的phase最先启动,停止时相反。因此,若对象实现了SmartLifecycle接口,它的getPhase()方法返回Integer.MIN_VALUE,那么该对象最先启动,最后停止。若是getPhase()方法返回Integer.MAX_VALUE,那么该方法最后启动最先停止。关于phase的值,常规的并未实现SmartLifecycle接口的Lifecycle对象,其值默认为0。因此,负phase值表示要在常规Lifecycle对象之前启动(在常规Lifecycyle对象之后停止),使用正值则恰恰相反。

SmartLifecycle中stop()方法有一个回调参数。所有的实现在关闭处理完成后会调用回调的run()方法,相当于开启异步关闭功能。

LifecycleProcessor接口在Spring中的默认实现是DefaultLifecycleProcessor类,该类会为每个回调等待超时,默认超时是30秒。

可以重写该类默认的参数,该类在容器内默认bean名称是lifecycleProcessor。比如修改超时时间:

<bean id="lifecycleProcessor" class="org.springframework.context.support.DefaultLifecycleProcessor">
    <!-- timeout value in milliseconds -->
    <property name="timeoutPerShutdownPhase" value="10000"/>
</bean>

SmartLifecycle的简单使用

@Component
public class CustomComponent implements SmartLifecycle {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    private boolean hasData;

    @Override
    public boolean isRunning() {
        // TODO Auto-generated method stub
        return hasData;
    }

    @Override
    public void start() {
        System.out.println(">>>>>>>>>>>>>");
        String result = jdbcTemplate.queryForObject("select name from user ", String.class);
        System.out.println("start启动服务加载的数据:" + result);
        hasData = StringUtils.isNotEmpty(result) ? true : false;
    }

    @Override
    public void stop() {
        // TODO Auto-generated method stub

    }

    @Override
    public int getPhase() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isAutoStartup() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public void stop(Runnable arg0) {
        if(hasData) {
            System.out.println("删除表");
            jdbcTemplate.execute("drop table user");
        }
    }
}

martLifecycle接口的实现类,简单的说下每个方法的作用:

isAutoStartup()返回true时,Spring容器启动时会去执行start()方法。
isRunning()返回true的时候,容器销毁时会调用stop()方法。
getPhase()返回的是优先级,如果有多个这样的类可以指定不同的优先级,让其先后执行(销毁的时候顺序相反)。

参考:https://docs.spring.io/spring/docs/4.3.22.RELEASE/spring-framework-reference/htmlsingle/#beans-factory-nature

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值