【Spring Boot】Spring Boot之五种容器启动后进行相关应用初始化操作方法
一、方式一,使用ApplicationListener<E extends ApplicationEvent>监听ContextRefreshedEvent事件
/**
* @author zhangboqing
* @date 2019-11-03
*/
@Component
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private MyService myService; @Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
System.out.println(">>>>>>>>>>>>>> ApplicationListener:" + myService.sayHello());
}
}
二、方式二,使用SmartInitializingSingleton
/**
* @author zhangboqing
* @date 2019-11-03
*/
@Component
public class MySmartInitializingSingleton implements SmartInitializingSingleton {
@Autowired
private MyService myService; @Override
public void afterSingletonsInstantiated() {
System.out.println(">>>>>>>>>>>>>> SmartInitializingSingleton:" + myService.sayHello());
}
}
三、方式三,使用SmartLifecycle
/**
* @author zhangboqing
* @date 2019-11-03
*/
@Component
public class MySmartLifecycle implements SmartLifecycle { @Autowired
private MyService myService; @Override
public void start() {
System.out.println(">>>>>>>>>>>>>> SmartLifecycle:" + myService.sayHello());
} @Override
public boolean isAutoStartup() {
return true;
} @Override
public void stop(Runnable callback) { } @Override
public void stop() { } @Override
public boolean isRunning() {
return false;
} @Override
public int getPhase() {
return ;
}
}
四、方式四,使用ApplicationRunner
/**
* @author zhangboqing
* @date 2019-11-07
*/
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Autowired
private MyService myService; @Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(">>>>>>>>>>>>>> ApplicationRunner:" + myService.sayHello());
}
}
五、方式五,使用CommandLineRunner
/**
* @author zhangboqing
* @date 2019-11-07
*/
@Component
public class MyCommandLineRunner implements CommandLineRunner { @Autowired
private MyService myService; @Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>> CommandLineRunner:" + myService.sayHello());
}
}
【Spring Boot】Spring Boot之五种容器启动后进行相关应用初始化操作方法的更多相关文章
- spring boot, 容器启动后执行某操作
常有在spring容器启动后执行某些操作的需求,现做了一个demo的实现,做一下记录,也希望可以给需要的同学提供参考. 1.spring启动后,以新线程执行后续需要的操作,所以执行类实现Runnabl ...
- Spring源码分析专题 —— IOC容器启动过程(上篇)
声明 1.建议先阅读<Spring源码分析专题 -- 阅读指引> 2.强烈建议阅读过程中要参照调用过程图,每篇都有其对应的调用过程图 3.写文不易,转载请标明出处 前言 关于 IOC 容器 ...
- web容器启动后自动执行程序的几种方式比较
1. 背景 1.1. 背景介绍 在web项目中我们有时会遇到这种需求,在web项目启动后需要开启线程去完成一些重要的工作,例如:往数据库中初始化一些数据,开启线程,初始化消息队 ...
- Centos7 docker容器启动后添加端口映射
docker容器启动后添加端口映射的两种方法: 一.通过修改防火墙策略添加端口映射 docker容器已创建好,但是想在容器内配置tomcat监控,需要新的端口去访问,但是映射时没有映射多余端口,此时, ...
- docker容器启动后添加端口映射
DOCKER 给运行中的容器添加映射端口 方法1 1.获得容器IP 将container_name 换成实际环境中的容器名 docker inspect `container_name` | grep ...
- web容器启动加载WebApplicationContext和初始化DispatcherServlet
原文地址:http://blog.csdn.net/zghwaicsdn/article/details/51186915 ContextLoaderListener监听器,加载ROOT WebApp ...
- @EnableAsync annotation metadata was not injected Spring容器启动后访问Servlet报错
@EnableAsync annotation metadata was not injected 2015年12月20日 20:06:54 7570 在初始化spring事务部分碰到该错误, 详细错 ...
- docker 容器启动后立马退出的解决方法
原因: 容器同时只能管理一个进程,如果这个进程结束了容器就退出了,但是不表示容器只能运行一个进程(其他进程可在后台运行),但是要使容器不退出必须要有一个进程在前台执行. 解决方案: 启动脚本最后一 ...
- zookeeper启动后没有相关进程
查看状态报错,报错,百度硕士nc问题,让看.out文件,但是这哥文件是空的,那就看log 016-12-15 14:08:19,355 [myid:] - INFO [main:QuorumPeer$ ...
随机推荐
- 接口自动化框架2-升级版(Pytest+request+Allure)
前言: 接口自动化是指模拟程序接口层面的自动化,由于接口不易变更,维护成本更小,所以深受各大公司的喜爱. 第一版入口:接口自动化框架(Pytest+request+Allure) 本次版本做了一些升级 ...
- redis哨兵模式实现主从故障切换
环境设定base2 172.25.78.12 masterbase3 172.25.78.13 slavebase4 172.25.78.14 slave1.配置一主二从环境在base2上[root@ ...
- List中的ArrayList和LinkedList源码分析
List是在面试中经常会问的一点,在我们面试中知道的仅仅是List是单列集合Collection下的一个实现类, List的实现接口又有几个,一个是ArrayList,还有一个是LinkedLis ...
- Java编程思想之十八 枚举类型
关键字enum可以将一组具名的值的有限集合创建为一种新的类型, 而这些具名的值可以作为常规的程序组件使用.这是一种非常有用的功能. 18.1 基本enum特性 创建enum时,编译器会为你生成一个相关 ...
- Node节点如何加入K8S集群
k8s集群中,有时候发现有些节点状态为 NotReady,如何修复为Ready状态呢? [root@k8s-master~]# kubectl get nodes NAME STATUS ROLES ...
- 学习spring源码-可参考的资料
剑指Spring源码(二) https://www.cnblogs.com/codebear/p/10374261.html 使用idea和gradle编译spring5源码https://blog. ...
- 递推 + 高精度 --- Tiling
Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7264 Accepted: 3528 Descriptio ...
- MODBUS 数据格式相关记录
串口通讯格式: 串口通讯可以分为同步通讯(Synchronous)和异步通讯(Asynchronous).同步通讯时有一根时钟信号,数据格式中没有起始位和停止位:异步通讯中没有时钟信号,数据格式中包含 ...
- Java学习:final关键字的使用与注意事项
final 关键字代表最终.不可改变的. 常见的四种用法 可以用来修饰一个类 可以用来修饰一个方法 可以用来修饰一个局部变量 可以用来修饰一个成员变量 1.当final关键字用来修饰一个类的时候,格式 ...
- - 反编译 AndroidKiller 逆向 实践案例 MD
目录 目录 反编译 AndroidKiller 逆向 实践案例 MD AndroidKiller 简介 插件升级 基本使用 实践案例 修改清单文件 打印 debug 级别的日志 方式一:直接代理 Lo ...