系统监听器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.lang.NonNull;

/**
 * @author itning
 * @date 2020/4/29 16:59
 */
public class FirstListener implements ApplicationListener<ApplicationStartedEvent> {
    @Override
    public void onApplicationEvent(@NonNull ApplicationStartedEvent event) {
        System.out.println("FirstListener");
    }
}

直接子类

1
2
3
4
5
6
AbstractSubProtocolEvent, 
ApplicationContextEvent, 
BrokerAvailabilityEvent, 
PayloadApplicationEvent, 
RequestHandledEvent, 
TestContextEvent

源码:

1
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));

spring.factories文件中

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.ConfigFileApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.context.logging.LoggingApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener

同样三种配置方式:

  1. 关键代码:springApplication.addListeners(new FirstListener(), new SecondListener());

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import top.itning.springboottest.config.FirstApplicationContextInitializer;
    import top.itning.springboottest.listener.FirstListener;
    import top.itning.springboottest.listener.SecondListener;
      
    /**
    * @author itning
    */
    @SpringBootApplication
    public class SpringbootTestApplication {
       public static void main(String[] args) {
           //SpringApplication.run(SpringbootTestApplication.class, args);
           SpringApplication springApplication = new SpringApplication(SpringbootTestApplication.class);
           springApplication.addInitializers(new FirstApplicationContextInitializer());
           springApplication.addListeners(new FirstListener(), new SecondListener());
           springApplication.run(args);
    }
    }
    
  2. 只需要在应用配置文件中application.yml配置:context.initializer.classes=xxx.xxx.xxx.XX

    查看原理:org.springframework.boot.context.config.DelegatingApplicationListener

    1
    2
    3
    
    context:
      listener:
        classes: xxx.xxx.xxx.XX
    
  3. resources目录新建META-INF文件夹并且新建文件:spring.factories

    1
    
    org.springframework.context.ApplicationListener=xxx.xxx.xxx.XX