spring boot 启动时运行代码(2)ApplicationListener
项目概览:

StepExecutor:
@Component
@Slf4j
public class StepExecutor implements Runnable {
@Autowired
private HelloService helloService; @Override
public void run() {
log.info("1111111111111111,helloService={}",helloService);
try {
Thread.sleep(1000*10);
} catch (InterruptedException e) {
e.printStackTrace();
}
helloService.hello();
log.info("22222222222222222");
}
}
ApplicationStartup
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(stepExecutor);
thread.start();
}
}
Application
@SpringBootApplication
@ComponentScan(basePackages="com.ebc")
@EnableAutoConfiguration //必须加该注解,否则报:缺少ServletWebServerFactory bean
@Slf4j
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF);
app.addListeners(new ApplicationStartup());
app.run(args);
log.info("PortalApplication is success!");
}
}
HelloService
@Service
public class HelloService {
public void hello() {
Console.log("xxxxxxxxxxxxxxxxxxxxxx");
}
}
启动:
16:35:48,920:INFO - Starting ProtocolHandler ["http-nio-9021"]
16:35:48,921:INFO - 1111111111111111,helloService=com.ebc.service.HelloService@67ed3522
16:35:48,938:INFO - Using a shared selector for servlet write/read
16:35:48,961:INFO - Started Application in 3.92 seconds (JVM running for 4.959)
16:35:48,964:INFO - PortalApplication is success!
xxxxxxxxxxxxxxxxxxxxxx
16:35:58,945:INFO - 22222222222222222
总结:
等待spring注入了所有bean后才执行执行。意味着,启动时,可以使用spring托管的任意bean。
而@PostConstract,无法做到。
spring boot 启动时运行代码(2)ApplicationListener的更多相关文章
- spring boot 系统启动时运行代码(1)-@PostConstruct
Application.java import org.springframework.boot.SpringApplication; import org.springframework.boot. ...
- spring boot启动STS 运行报错 java.lang.NoClassDefFoundError: ch/qos/logback/classic/LoggerContext
spring boot启动STS 运行报错 java.lang.NoClassDefFoundError: ch/qos/logback/classic/LoggerContext 学习了: http ...
- Spring Boot启动时出现WARN:No MyBatis mapper was found in
今天发现spring-boot继承mybatis启动时老是出现WARN: org.mybatis.spring.mapper.ClassPathMapperScanner - No MyBatis m ...
- [Spring Boot] Spring Boot启动过程源码分析
关于Spring Boot,已经有很多介绍其如何使用的文章了,本文从源代码(基于Spring-boot 1.5.6)的角度来看看Spring Boot的启动过程到底是怎么样的,为何以往纷繁复杂的配置到 ...
- Spring Boot启动过程源码分析--转
https://blog.csdn.net/dm_vincent/article/details/76735888 关于Spring Boot,已经有很多介绍其如何使用的文章了,本文从源代码(基于Sp ...
- 使用Spring boot整合Hive,在启动Spring boot项目时,报错
使用Spring boot整合Hive,在启动Spring boot项目时,报出异常: java.lang.NoSuchMethodError: org.eclipse.jetty.servlet.S ...
- Spring Boot 启动(一) SpringApplication 分析
Spring Boot 启动(一) SpringApplication 分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html ...
- Spring Boot 启动原理分析
https://yq.aliyun.com/articles/6056 转 在spring boot里,很吸引人的一个特性是可以直接把应用打包成为一个jar/war,然后这个jar/war是可以直接启 ...
- Spring Boot启动过程及回调接口汇总
Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...
随机推荐
- 初阶html学习总结(一)(转)
一:颜色代码 如果你想使用某种颜色,取得它的颜色值即可.比如,您想改变某些文字的颜色,您可以使用下面的代码:<font color=#ffc060 size=2>改变#符号后的代码即可改变 ...
- php学习笔记-while循环
while(condition) { func(); //break the loop } while循环的执行顺序是先判断condition是不是true,如果是true,那么就执行while循环体 ...
- bzoj4619 4619: [Wf2016]Swap Space
传送门 分析 首先不难想到我们要先处理容量变大的再处理容量变小的 对于第一种情况我们自然要选择x小的先格式化,因为这个样暂时存储所需空间较小,可以使得情况更优 而第二种情况y先考虑,因为这样对总空间的 ...
- WarTransportation TopCoder - 8404
传送门 分析 我们高兴的发现数据范围特别小,所以我们可以随便搞.因为一共只砍掉一条路,所以我们先算出对于任意一个点如果将它的出边割掉一条则它到达终点的最坏情况的最短距离是多少,然后我们从终点向起点反着 ...
- java全栈day11----构造方法 综合案例
构造方法 在开发中经常需要在创建对象的同时明确对象的属性值,比如员工入职公司就要明确他的姓名.年龄等属性信息. 那么,创建对象就要明确属性值,那怎么解决呢?也就是在创建对象的时候就要做的事情,当使用n ...
- 关于C# WinForm_Tree View的一些基本用法(摘抄)
下面是treeview的用法TreeView组件是由多个类来定义的,TreeView组件是由命名空间"System.Windows .Forms"中的"TreeView& ...
- 在控制台使用MySQL数据库
本篇内容介绍的是如何在控制台下使用MySQL数据库.首先需要安装MySQL数据库应用程序,然后找到MySql的Command Line Client进入之后你会看到,此处需要正确输入密码,否则会直接退 ...
- 在eclipse中使用hadoop插件
我的配置环境看我的上篇博文. 配置过程: (1)把插件放到eclipse/plugins目录下.(我的版本上一篇也有) )重启eclipse,配置Hadoop installation directo ...
- maven+selenium+java+testng+jenkins自动化测试
最近在公司搭建了一套基于maven+selenium+java+testng+jenkins的自动化测试框架,免得以后重写记录下 工程目录 pom.xml <project xmlns=&quo ...
- 【51nod1847】奇怪的数学题(Min_25筛+杜教筛)
题面 传送门 题解 这题有毒--不知为啥的错误调了半天-- 令\(f(i)={sgcd(i)}\),那么容易看出\(f(i)\)就是\(i\)的次大质因子,用\(i\)除以它的最小质因子即可计算 于是 ...