7_3.springboot2.x启动配置原理_3.事件监听机制

事件监听机制
配置在META-INF/spring.factories
ApplicationContextInitializer
SpringApplicationRunListener
ioc容器中的
ApplicationRunner
CommandLineRunner

测试

配置在META-INF/spring.factories
ApplicationContextInitializer

package com.spboot.springboot.listener;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

public class HelloApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        System.out.println("ApplicationContextInitializer...+initialize"+applicationContext);
    }
}

SpringApplicationRunListener

package com.spboot.springboot.listener;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {
    //必须有的构造器
    public HelloSpringApplicationRunListener(SpringApplication application, String[] args){
    }

    @Override
    public void starting() {
        System.out.println("SpringApplicationRunListener...starting...");
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        Object o = environment.getSystemProperties().get("os.name");
        System.out.println("SpringApplicationRunListener...environmentPrepared.."+o);
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...contextPrepared...");
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...contextLoaded...");
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...started...");
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener...running...");

    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        System.out.println("SpringApplicationRunListener...failed...");

    }
}

配置(META-INF/spring.factories)

org.springframework.context.ApplicationContextInitializer=\
com.spboot.springboot.listener.HelloApplicationContextInitializer

org.springframework.boot.SpringApplicationRunListener=\
com.spboot.springboot.listener.HelloSpringApplicationRunListener

在ioc容器中
ApplicationRunner

package com.spboot.springboot.listener;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class HelloApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner...run....");
    }
}

 

CommandLineRunner

package com.spboot.springboot.listener;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class HelloCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunner...run..."+ Arrays.asList(args));
    }
}

启动:

D:\job\java\jdk1.8\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=53220 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\job\IntelliJ IDEA 2018.3.3\lib\idea_rt.jar=53221:D:\job\IntelliJ IDEA 2018.3.3\bin" -Dfile.encoding=UTF-8 -classpath D:\job\java\jdk1.8\jre\lib\charsets.jar;D:\job\java\jdk1.8\jre\lib\deploy.jar;D:\job\java\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\job\java\jdk1.8\jre\lib\ext\cldrdata.jar;D:\job\java\jdk1.8\jre\lib\ext\dnsns.jar;D:\job\java\jdk1.8\jre\lib\ext\jaccess.jar;D:\job\java\jdk1.8\jre\lib\ext\jfxrt.jar;D:\job\java\jdk1.8\jre\lib\ext\localedata.jar;D:\job\java\jdk1.8\jre\lib\ext\nashorn.jar;D:\job\java\jdk1.8\jre\lib\ext\sunec.jar;D:\job\java\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\job\java\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\job\java\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\job\java\jdk1.8\jre\lib\ext\zipfs.jar;D:\job\java\jdk1.8\jre\lib\javaws.jar;D:\job\java\jdk1.8\jre\lib\jce.jar;D:\job\java\jdk1.8\jre\lib\jfr.jar;D:\job\java\jdk1.8\jre\lib\jfxswt.jar;D:\job\java\jdk1.8\jre\lib\jsse.jar;D:\job\java\jdk1.8\jre\lib\management-agent.jar;D:\job\java\jdk1.8\jre\lib\plugin.jar;D:\job\java\jdk1.8\jre\lib\resources.jar;D:\job\java\jdk1.8\jre\lib\rt.jar;E:\code\springboot\springboot-07\target\classes;D:\Apache\maven\repository\org\springframework\boot\spring-boot-starter-web\2.1.9.RELEASE\spring-boot-starter-web-2.1.9.RELEASE.jar;D:\Apache\maven\repository\org\springframework\boot\spring-boot-starter\2.1.9.RELEASE\spring-boot-starter-2.1.9.RELEASE.jar;D:\Apache\maven\repository\org\springframework\boot\spring-boot\2.1.9.RELEASE\spring-boot-2.1.9.RELEASE.jar;D:\Apache\maven\repository\org\springframework\boot\spring-boot-autoconfigure\2.1.9.RELEASE\spring-boot-autoconfigure-2.1.9.RELEASE.jar;D:\Apache\maven\repository\org\springframework\boot\spring-boot-starter-logging\2.1.9.RELEASE\spring-boot-starter-logging-2.1.9.RELEASE.jar;D:\Apache\maven\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Apache\maven\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Apache\maven\repository\org\apache\logging\log4j\log4j-to-slf4j\2.11.2\log4j-to-slf4j-2.11.2.jar;D:\Apache\maven\repository\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;D:\Apache\maven\repository\org\slf4j\jul-to-slf4j\1.7.28\jul-to-slf4j-1.7.28.jar;D:\Apache\maven\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\Apache\maven\repository\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\Apache\maven\repository\org\springframework\boot\spring-boot-starter-json\2.1.9.RELEASE\spring-boot-starter-json-2.1.9.RELEASE.jar;D:\Apache\maven\repository\com\fasterxml\jackson\core\jackson-databind\2.9.9.3\jackson-databind-2.9.9.3.jar;D:\Apache\maven\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\Apache\maven\repository\com\fasterxml\jackson\core\jackson-core\2.9.9\jackson-core-2.9.9.jar;D:\Apache\maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.9\jackson-datatype-jdk8-2.9.9.jar;D:\Apache\maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.9\jackson-datatype-jsr310-2.9.9.jar;D:\Apache\maven\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.9\jackson-module-parameter-names-2.9.9.jar;D:\Apache\maven\repository\org\springframework\boot\spring-boot-starter-tomcat\2.1.9.RELEASE\spring-boot-starter-tomcat-2.1.9.RELEASE.jar;D:\Apache\maven\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.26\tomcat-embed-core-9.0.26.jar;D:\Apache\maven\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.26\tomcat-embed-el-9.0.26.jar;D:\Apache\maven\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.26\tomcat-embed-websocket-9.0.26.jar;D:\Apache\maven\repository\org\hibernate\validator\hibernate-validator\6.0.17.Final\hibernate-validator-6.0.17.Final.jar;D:\Apache\maven\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\Apache\maven\repository\org\jboss\logging\jboss-logging\3.3.3.Final\jboss-logging-3.3.3.Final.jar;D:\Apache\maven\repository\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;D:\Apache\maven\repository\org\springframework\spring-web\5.1.10.RELEASE\spring-web-5.1.10.RELEASE.jar;D:\Apache\maven\repository\org\springframework\spring-beans\5.1.10.RELEASE\spring-beans-5.1.10.RELEASE.jar;D:\Apache\maven\repository\org\springframework\spring-webmvc\5.1.10.RELEASE\spring-webmvc-5.1.10.RELEASE.jar;D:\Apache\maven\repository\org\springframework\spring-aop\5.1.10.RELEASE\spring-aop-5.1.10.RELEASE.jar;D:\Apache\maven\repository\org\springframework\spring-context\5.1.10.RELEASE\spring-context-5.1.10.RELEASE.jar;D:\Apache\maven\repository\org\springframework\spring-expression\5.1.10.RELEASE\spring-expression-5.1.10.RELEASE.jar;D:\Apache\maven\repository\org\slf4j\slf4j-api\1.7.28\slf4j-api-1.7.28.jar;D:\Apache\maven\repository\org\springframework\spring-core\5.1.10.RELEASE\spring-core-5.1.10.RELEASE.jar;D:\Apache\maven\repository\org\springframework\spring-jcl\5.1.10.RELEASE\spring-jcl-5.1.10.RELEASE.jar com.spboot.springboot.Springboot07Application
SpringApplicationRunListener...starting...
SpringApplicationRunListener...environmentPrepared..Windows 7

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.9.RELEASE)

ApplicationContextInitializer...+initializeorg.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3c87521, started on Thu Jan 01 08:00:00 CST 1970
SpringApplicationRunListener...contextPrepared...
2019-10-07 19:22:17.554  INFO 2992 --- [           main] c.s.springboot.Springboot07Application   : Starting Springboot07Application on jitp with PID 2992 (E:\code\springboot\springboot-07\target\classes started by Administrator in E:\code\springboot\springboot-07)
2019-10-07 19:22:17.557  INFO 2992 --- [           main] c.s.springboot.Springboot07Application   : No active profile set, falling back to default profiles: default
SpringApplicationRunListener...contextLoaded...
2019-10-07 19:22:18.869  INFO 2992 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-10-07 19:22:18.893  INFO 2992 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-10-07 19:22:18.893  INFO 2992 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.26]
2019-10-07 19:22:18.978  INFO 2992 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-10-07 19:22:18.978  INFO 2992 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1373 ms
2019-10-07 19:22:19.168  INFO 2992 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-07 19:22:19.328  INFO 2992 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-10-07 19:22:19.331  INFO 2992 --- [           main] c.s.springboot.Springboot07Application   : Started Springboot07Application in 2.192 seconds (JVM running for 3.262)
SpringApplicationRunListener...started...
ApplicationRunner...run....
CommandLineRunner...run...[]
SpringApplicationRunListener...running...

 

 

posted @ 2019-10-07 18:59  杰醍奋  阅读(264)  评论(0编辑  收藏  举报