spring boot, 容器启动后执行某操作
2018-02-09 16:25
573 查看
常有在spring容器启动后执行某些操作的需求,现做了一个demo的实现,做一下记录,也希望可以给需要的同学提供参考。
1.spring启动后,以新线程执行后续需要的操作,所以执行类实现Runnable接口
@Component
public class StepExecutor implements Runnable{
@Override
public void run() {
startStreamTask();
}
public void startStreamTask() {
//do your business
}
}
2.监听类实现ApplicationListener<ContextRefreshedEvent>
/**
* spring boot 容器加载完成后执行
* @author yhz
*
*/
public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext ac = event.getApplicationContext();
StepExecutor StepExecutor = ac.getBean(StepExecutor .class);
Thread thread = new Thread(sparkKafkaStreamExecutor);
thread.start();
}
}注:亲测,spring boot项目下,上面方式正确触发执行一次;如果是spring web项目下,可能会造成二次执行,因为此时系统会存在两个容器,一个是spring本身的root application context,另一个是servlet容器(作为spring容器的子容器,projectName-servlet context),此时,加以下限制条件规避:
if(event.getApplicationContext().getParent()==null){
//只有root application context 没有父容器
//start the executor
}
3.容器启动时注册监听类
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(DemoApplication .class);
springApplication.addListeners(new ApplicationStartup());
springApplication.run(args);
}
}
到此,逻辑流程实现完毕,谢谢阅读。
1.spring启动后,以新线程执行后续需要的操作,所以执行类实现Runnable接口
@Component
public class StepExecutor implements Runnable{
@Override
public void run() {
startStreamTask();
}
public void startStreamTask() {
//do your business
}
}
2.监听类实现ApplicationListener<ContextRefreshedEvent>
/**
* spring boot 容器加载完成后执行
* @author yhz
*
*/
public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext ac = event.getApplicationContext();
StepExecutor StepExecutor = ac.getBean(StepExecutor .class);
Thread thread = new Thread(sparkKafkaStreamExecutor);
thread.start();
}
}注:亲测,spring boot项目下,上面方式正确触发执行一次;如果是spring web项目下,可能会造成二次执行,因为此时系统会存在两个容器,一个是spring本身的root application context,另一个是servlet容器(作为spring容器的子容器,projectName-servlet context),此时,加以下限制条件规避:
if(event.getApplicationContext().getParent()==null){
//只有root application context 没有父容器
//start the executor
}
3.容器启动时注册监听类
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(DemoApplication .class);
springApplication.addListeners(new ApplicationStartup());
springApplication.run(args);
}
}
到此,逻辑流程实现完毕,谢谢阅读。
相关文章推荐
- Spring Boot 启动时执行加载资源/执行操作
- 详解spring boot容器加载完后执行特定操作
- Spring-Spring容器启动各项操作窥探(AbstractApplicationContext#refesh()执行逻辑分析)
- Spring Boot容器加载时执行特定操作(推荐)
- spring boot容器加载完后执行特定操作
- 如何在spring容器开始后,和销毁前,执行一些操作
- springboot 学习之路 9 (项目启动后就执行特定方法)
- spring boot容器启动流程
- spring 在web容器启动时执行初始化方法
- Spring boot启动之后执行方法 CommandLineRunner
- Spring-boot 启动完成时执行指定任务
- springBoot 设置服务启动时执行代码
- Spring,启动程序后顺序执行指定操作
- spring在web容器启动时执行初始化方法
- spring boot容器启动详解
- spring容器启动会默认执行的注解
- spring 在web 容器启动时,执行FIFO队列初始化方法
- Spring Boot 启动完成执行 CommandLineRunner
- Spring,启动程序后顺序执行指定操作
- 启动springboot jar脚本和jenkins集成gitlab执行execute shell