Spring Boot 2.0 新特性ApplicationStartedEvent实战

一 点睛

在Spring Boot 2.0中对事件模型做了一些增强,主要就是增加了ApplicationStartedEvent事件,所以在2.0版本中所有的事件按执行的先后顺序如下:

  • ApplicationStartingEvent

  • ApplicationEnvironmentPreparedEvent

  • ApplicationPreparedEvent

  • ApplicationStartedEvent <= 新增的事件

  • ApplicationReadyEvent

  • ApplicationFailedEvent

二 实战

1 新增依赖


    
        org.springframework.boot
        spring-boot-starter-web
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
    

    
        org.projectlombok
        lombok
        provided
    

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 {


    @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 {

    @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 {
    @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 {

    @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 {
    @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 {

    @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注解

    org.projectlombok

    lombok

然后在类上写上@Slf4j注解

在方法中直接使用,就可以打印日志。

五 参考

https://blog.csdn.net/qq_26525215/article/details/79182628

http://blog.didispace.com/Spring-Boot-2-0-feature-2-ApplicationStartedEvent/

你可能感兴趣的:(Spring,Boot)