SprinBoot的功能

本文介绍了SpringBoot中如何在项目启动后执行特定任务以及在项目正常关闭前执行清理操作。通过实现ApplicationRunner接口或使用@PostConstruct注解,可以在应用启动后运行自定义代码。同时,通过实现DisposableBean接口或使用@PreDestroy注解,可以确保在应用关闭前执行必要的清理工作。

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

1、项目启动后加载的功能
ApplicationRunner程序启动后会调用实现了这个接口的类
@Component
public class ApplicationTask implements ApplicationRunner{

@Override
public void run(ApplicationArguments args) throws Exception {
	System.out.println("--------application run loader this method----------");
}

}
也可以用其他的方法,比如@postconstruct来实现相同的效果

2、项目正常关闭后执行的方法
@Component
public class CloseTask implements DisposableBean{

@Override
public void destroy() throws Exception {
	System.out.println("---------------this method run before destory-------------");
}

}

其他的方式,如@PreDestroy也可以实现相同的效果

3、自动配置、起步依赖、Actuator、命令行界面是Spring Boot最重要的4大核心特性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值