started
一 点睛
在Spring Boot 2.0中对事件模型做了一些增强,主要就是增加了APPlicationStartedEvent事件,所以在2.0版本中所有的事件按执行的先后顺序如下:
-
ApplicationStartingEvent
-
ApplicationenvironmentPreparedEvent
-
ApplicationPreparedEvent
-
ApplicationStartedEvent <= 新增的事件
-
ApplicationReadyEvent
-
ApplicationFailedEvent
二 实战
1 新增依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
2 ApplicationEnvironmentPreparedEventListener监听器
package com.didispace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
@Slf4j
public class ApplicationEnvironmentPreparedEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
log.info("......ApplicationEnvironmentPreparedEvent......");
}
}
3 ApplicationfailedEventListener监听器
package com.didispace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.context.ApplicationListener;
@Slf4j
public class ApplicationFailedEventListener implements ApplicationListener<ApplicationFailedEvent> {
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
log.info("......ApplicationFailedEvent......");
}
}
4 ApplicationPreparedEventListener监听器
package com.didispace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationListener;
@Slf4j
public class ApplicationPreparedEventListener implements ApplicationListener<ApplicationPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
log.info("......ApplicationPreparedEvent......");
}
}
5 ApplicationReadyEventListener监听器
package com.didispace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
@Slf4j
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
log.info("......ApplicationReadyEvent......");
}
}
6 ApplicationStartedEventListener监听器
package com.didispace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
@Slf4j
public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
log.info("......ApplicationStartedEvent......");
}
}
7 ApplicationStartingEventListener监听器
package com.didispace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener;
@Slf4j
public class ApplicationStartingEventListener implements ApplicationListener<ApplicationStartingEvent> {
@Override
public void onApplicationEvent(ApplicationStartingEvent event) {
log.info("......ApplicationStartingEvent......");
}
}
8 启动类
1 代码
package com.didispace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.commandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@Slf4j
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public DataLoader dataLoader() {
return new DataLoader();
}
@Slf4j
static class DataLoader implements CommandLineRunner {
@Override
public void run(String... strings) throws Exception {
log.info("Loading data...");
}
}
}
2 说明
官方文档对ApplicationStartedEvent和ApplicationReadyEvent的解释:
An ApplicationStartedEvent is sent after the context has been refreshed but before any application and command-line runners have been called.An ApplicationReadyEvent is sent after any application and command-line runners have been called. It indicates that the application is ready to service requests
从文档中我们可以知道他们两中间还有一个过程就是command-line runners被调用的内容。所以,为了更准确的感受这两个事件的区别,我们在应用主类中加入CommandLineRunner的实现。
三 运行结果
2018-10-24 19:32:01.772 INFO 14708 --- [ main] c.d.ApplicationPreparedEventListener : ......ApplicationPreparedEvent......
......
2018-10-24 19:32:05.032 INFO 14708 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-10-24 19:32:05.068 INFO 14708 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-10-24 19:32:05.072 INFO 14708 --- [ main] com.didispace.Application : Started Application in 3.8 seconds (JVM running for 4.372)
2018-10-24 19:32:05.074 INFO 14708 --- [ main] c.d.ApplicationStartedEventListener : ......ApplicationStartedEvent......
2018-10-24 19:32:05.075 INFO 14708 --- [ main] com.didispace.Application$DataLoader : Loading data...
2018-10-24 19:32:05.076 INFO 14708 --- [ main] c.d.ApplicationReadyEventListener : ......ApplicationReadyEvent......
四 知识拾遗
1 关于@Slf4j注解
<!--引入日志 @Slf4j注解-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
然后在类上写上@Slf4j注解
在方法中直接使用,就可以打印日志。
五 参考
https://blog.csdn.net/qq_26525215/article/details/79182628
http://blog.didispace.com/Spring-Boot-2-0-feature-2-ApplicationStartedEvent/
相关阅读
Spring Cloud 是一套完整的微服务解决方案,基于 Spring Boot 框架,准确的说,它不是一个框架,而是一个大的容器,它将市面上较好的微服务
#SpringMVC--理解web.xml中的 url-pattern标签及其使
在web.xml中,我们通过下面的标签来匹配客户端发出的请求: <servlet-mapping> <servlet-name>yzh_ssh</servlet-name> <ur
spring-mvc.xml 和 application-context.xml的配置与
在java框架这个话题,前几篇文章是基于搭建ssm项目框架,以及web.xml的配置讲解,本篇主要就ssm框架的其他配置文件进行深入讲解,他们分
最近再研究springboot的原理��颇有收获,现在让我分享一下springboot如何使用吧~ 想要解锁更多新姿势?请访问我的博客 啥是Springboo
Spring与Struts的整合_使用ActionSupport代替Action
有一种方法可以用于Spring与Struts的整合:让Action在程序中手动获得ApplicationContext实例。在这种整合策略下,Struts的Action不接