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 ...
随机推荐
- 百度Apollo解析——3.common
1.略读 该目录下主要提供了各个模块公用的函数和class以及一些数学API还有公共的宏定义. 在Apollo 1.0中,common是整个框架的基础.configs是配置文件加载.adapters是 ...
- DB2--值为null则赋默认值
数据库sql操作经常会做一些null值的处理.如果一个字段的值为null,我们希望查询出的结果默认设为0或空,则使用函数 COALESCE(column,0) ,0的位置可以替换为其他值,可以是'' ...
- Java3D-对象基本变换
一个球体与三个圆柱体形成一个组合体,在该组合体中,球体的透明度属性是由全透明到不透明之间变换,而且包括:旋转.平移等变换. package com.vfsd.test0621; import java ...
- PCL—点云分割(超体聚类) 低层次点云处理
博客转载自:http://www.cnblogs.com/ironstark/p/5013968.html 1.超体聚类——一种来自图像的分割方法 超体(supervoxel)是一种集合,集合的元素是 ...
- Luogu 4151 [WC2011]最大XOR和路径
你谷又乱评分…… 首先发现答案只有可能是从$1$开始走到$n$,中间绕若干个环,然后使它取到的异或值最大. 这样子的话我们可以随便先取一条路径,强制选择走这条路径,然后把所有的环都丢进线性基里面去,因 ...
- DIY的RPM包怎么签名呢 - 笔记
参考 https://gist.github.com/fernandoaleman/1376720 如果打不开上一个连接,请参考https://www.cnblogs.com/LiuYanYGZ/p/ ...
- 找BUG
找一找BUG 一段代码,实现一个pop,push,和getmin都是O(1)的方法. 最初源代码 伙伴代码如下,代码的地址可以通过这个访问: Ubuntu Pastebin https://paste ...
- 1506-122 (S) Expecting pointer to struct or union.
__你们 大胆 猜 是什么错.. __ 很尴尬的错误..就是 结构体指针. ___只有结构体 指针 可以用 -> 这个符号哦.. 所以 你只要 将 -> 改成 . , 就 ...
- 《Linux内核设计与实现》读书笔记(四)- 进程的调度
主要内容: 什么是调度 调度实现原理 Linux上调度实现的方法 调度相关的系统调用 1. 什么是调度 现在的操作系统都是多任务的,为了能让更多的任务能同时在系统上更好的运行,需要一个管理程序来管理计 ...
- winform GDI基础(三)实现画笔
在程序窗口上使用鼠标画图 private Point pStart, pEnd; private bool isAllowDraw = false; private bool isOpenPen = ...