spring容器启动事件和关闭事件

本文介绍了一种在Spring应用中实现启动和关闭事件监听的方法。通过实现ApplicationListener接口并覆盖onApplicationEvent方法来响应ContextRefreshedEvent和ContextClosedEvent,从而在Spring容器启动成功后启动Netty服务,在容器关闭时停止服务。

启动事件

实现ApplicationListener ContextRefreshedEvent

@Service
public class StartAddDataListener  implements ApplicationListener<ContextRefreshedEvent> {
    private Logger logger= LoggerFactory.getLogger(StartAddDataListener.class);
    @Autowired
    TCPServer  tcpServer;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if(event.getApplicationContext().getParent() == null){
            logger.info("spring Start Success");
            NettyStartService nettyStartService=new NettyStartService();
            nettyStartService.setTcpServer(tcpServer);
            new Thread(nettyStartService).start();
        }
    }
}

关闭事件

implements ApplicationListener

@Service
public class StopAddDataListener implements ApplicationListener<ContextClosedEvent> {
        private Logger logger= LoggerFactory.getLogger(StartAddDataListener.class);
        @Autowired
        TCPServer  tcpServer;

    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        if(contextClosedEvent.getApplicationContext().getParent() == null) {
            NettyStopService nettyStopService = new NettyStopService();
            nettyStopService.setTcpServer(tcpServer);
            new Thread(nettyStopService).start();
        }
    }
}
Spring Bean 的生命周期由 Spring 容器全权管理,包含五个主要阶段:实例化、属性注入、初始化、使用销毁,各阶段有其关键步骤扩展点[^1]。 - **实例化(Instantiation)**:这是 Bean 生命周期的起始阶段,Spring 容器会创建 Bean 的实例。 - **属性注入**:在 Bean 实例创建之后,Spring 容器会将配置文件或注解中定义的属性值注入到 Bean 实例中。 - **初始化**:此阶段会调用 Bean 的初始化方法,完成一些初始化操作。例如,实现 `InitializingBean` 接口的 `afterPropertiesSet` 方法,或者在配置文件中指定 `init - method` 方法。 - **使用**:经过前面的步骤,Bean 已经准备好被应用程序使用,在应用程序运行期间,会根据需要调用 Bean 的方法来完成相应的业务逻辑。 - **销毁**:当 Spring 容器关闭时,会对 Bean 进行销毁操作。可以通过实现 `DisposableBean` 接口的 `destroy` 方法,或者在配置文件中指定 `destroy - method` 方法来完成自定义的销毁逻辑。 关于 Spring启动事件Spring 框架提供了一系列的事件机制,在容器启动过程中会发布一些关键事件: - **ContextRefreshedEvent**:当 `ApplicationContext` 被初始化或刷新时发布该事件。这意味着所有的 Bean 已经被加载、配置初始化完成,此时可以进行一些额外的初始化操作。 - **ContextStartedEvent**:当使用 `ConfigurableApplicationContext` 的 `start()` 方法启动 `ApplicationContext` 时发布该事件。 - **ContextStoppedEvent**:当使用 `ConfigurableApplicationContext` 的 `stop()` 方法停止 `ApplicationContext` 时发布该事件。 - **ContextClosedEvent**:当使用 `ConfigurableApplicationContext` 的 `close()` 方法关闭 `ApplicationContext` 时发布该事件,此时 Bean 会进入销毁阶段。 ### 示例代码 ```java import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; @Component public class ContextRefreshedEventListener implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { // 当 ContextRefreshedEvent 事件触发时执行的逻辑 System.out.println("Context refreshed event received"); } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值