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 ...
随机推荐
- 数字调节控件JSpinner的使用
---------------siwuxie095 工程名:TestUI 包名:com.siwuxie095.ui 类名:TestList.java ...
- SqlServer中把结果集放到到临时表的方法(转)
一. SELECT INTO 1. 使用select into会自动生成临时表,不需要事先创建 select * into #temp from sysobjects 01. 把存储过程结 ...
- Python 网络爬虫 006 (编程) 解决下载(或叫:爬取)到的网页乱码问题
解决下载(或叫:爬取)到的网页乱码问题 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 Python 的集成开发环境:PyCharm 20 ...
- Python 安装 第三方库的安装技巧
Python 安装 第三方库的安装技巧 我的电脑:Windows 10 64位. Python IDE 软件:PyCharm 2016.1.4 Python version : Python 3.5. ...
- 95E Lucky Country
传送门 题目大意 如果一个数中不包含除4和7之外的数字则是幸运数.有n个岛屿,通过双向道路连接.这些岛屿被分为几个地区.每个岛属于恰好一个区域,同一区域中的任何两个岛之间存在道路,不同区域的任何两个岛 ...
- Entity Framework Tutorial Basics(22):Disconnected Entities
Disconnected Entities: Before we see how to perform CRUD operation on disconnected entity graph, let ...
- map集合的应用
分析以下需求,并用代码实现: (1)利用键盘录入,输入一个字符串 (2)统计该字符串中各个字符的数量例如 用户输入字符串"If~you-want~to~change-your_fate_I_ ...
- winform GDI基础(二)画带圆角的矩形框
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.SmoothingMode ...
- SGML DTD
最近处理SGML文档时,碰到的DTD语法: <!ELEMENT name - - (#PCDATA)> 说明:- - 代表 name 元素有开始标识和结束标识 <!ELEMENT ...
- 简单介绍Jenkins&持续集成
1.定义 持续集成(Continuous integration),简称CI. 随着软件项目复杂度的增加,就会对集成和确保软件组件能够在一起工作提出了更多的要求-要早集成.常集成. CI不是一项软件开 ...