• 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:jobjavajdk1.8injava.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:jobIntelliJ IDEA 2018.3.3libidea_rt.jar=53221:D:jobIntelliJ IDEA 2018.3.3in" -Dfile.encoding=UTF-8 -classpath D:jobjavajdk1.8jrelibcharsets.jar;D:jobjavajdk1.8jrelibdeploy.jar;D:jobjavajdk1.8jrelibextaccess-bridge-64.jar;D:jobjavajdk1.8jrelibextcldrdata.jar;D:jobjavajdk1.8jrelibextdnsns.jar;D:jobjavajdk1.8jrelibextjaccess.jar;D:jobjavajdk1.8jrelibextjfxrt.jar;D:jobjavajdk1.8jrelibextlocaledata.jar;D:jobjavajdk1.8jrelibext
    ashorn.jar;D:jobjavajdk1.8jrelibextsunec.jar;D:jobjavajdk1.8jrelibextsunjce_provider.jar;D:jobjavajdk1.8jrelibextsunmscapi.jar;D:jobjavajdk1.8jrelibextsunpkcs11.jar;D:jobjavajdk1.8jrelibextzipfs.jar;D:jobjavajdk1.8jrelibjavaws.jar;D:jobjavajdk1.8jrelibjce.jar;D:jobjavajdk1.8jrelibjfr.jar;D:jobjavajdk1.8jrelibjfxswt.jar;D:jobjavajdk1.8jrelibjsse.jar;D:jobjavajdk1.8jrelibmanagement-agent.jar;D:jobjavajdk1.8jrelibplugin.jar;D:jobjavajdk1.8jrelib
    esources.jar;D:jobjavajdk1.8jrelib
    t.jar;E:codespringbootspringboot-07	argetclasses;D:Apachemaven
    epositoryorgspringframeworkootspring-boot-starter-web2.1.9.RELEASEspring-boot-starter-web-2.1.9.RELEASE.jar;D:Apachemaven
    epositoryorgspringframeworkootspring-boot-starter2.1.9.RELEASEspring-boot-starter-2.1.9.RELEASE.jar;D:Apachemaven
    epositoryorgspringframeworkootspring-boot2.1.9.RELEASEspring-boot-2.1.9.RELEASE.jar;D:Apachemaven
    epositoryorgspringframeworkootspring-boot-autoconfigure2.1.9.RELEASEspring-boot-autoconfigure-2.1.9.RELEASE.jar;D:Apachemaven
    epositoryorgspringframeworkootspring-boot-starter-logging2.1.9.RELEASEspring-boot-starter-logging-2.1.9.RELEASE.jar;D:Apachemaven
    epositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;D:Apachemaven
    epositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;D:Apachemaven
    epositoryorgapachelogginglog4jlog4j-to-slf4j2.11.2log4j-to-slf4j-2.11.2.jar;D:Apachemaven
    epositoryorgapachelogginglog4jlog4j-api2.11.2log4j-api-2.11.2.jar;D:Apachemaven
    epositoryorgslf4jjul-to-slf4j1.7.28jul-to-slf4j-1.7.28.jar;D:Apachemaven
    epositoryjavaxannotationjavax.annotation-api1.3.2javax.annotation-api-1.3.2.jar;D:Apachemaven
    epositoryorgyamlsnakeyaml1.23snakeyaml-1.23.jar;D:Apachemaven
    epositoryorgspringframeworkootspring-boot-starter-json2.1.9.RELEASEspring-boot-starter-json-2.1.9.RELEASE.jar;D:Apachemaven
    epositorycomfasterxmljacksoncorejackson-databind2.9.9.3jackson-databind-2.9.9.3.jar;D:Apachemaven
    epositorycomfasterxmljacksoncorejackson-annotations2.9.0jackson-annotations-2.9.0.jar;D:Apachemaven
    epositorycomfasterxmljacksoncorejackson-core2.9.9jackson-core-2.9.9.jar;D:Apachemaven
    epositorycomfasterxmljacksondatatypejackson-datatype-jdk82.9.9jackson-datatype-jdk8-2.9.9.jar;D:Apachemaven
    epositorycomfasterxmljacksondatatypejackson-datatype-jsr3102.9.9jackson-datatype-jsr310-2.9.9.jar;D:Apachemaven
    epositorycomfasterxmljacksonmodulejackson-module-parameter-names2.9.9jackson-module-parameter-names-2.9.9.jar;D:Apachemaven
    epositoryorgspringframeworkootspring-boot-starter-tomcat2.1.9.RELEASEspring-boot-starter-tomcat-2.1.9.RELEASE.jar;D:Apachemaven
    epositoryorgapache	omcatembed	omcat-embed-core9.0.26	omcat-embed-core-9.0.26.jar;D:Apachemaven
    epositoryorgapache	omcatembed	omcat-embed-el9.0.26	omcat-embed-el-9.0.26.jar;D:Apachemaven
    epositoryorgapache	omcatembed	omcat-embed-websocket9.0.26	omcat-embed-websocket-9.0.26.jar;D:Apachemaven
    epositoryorghibernatevalidatorhibernate-validator6.0.17.Finalhibernate-validator-6.0.17.Final.jar;D:Apachemaven
    epositoryjavaxvalidationvalidation-api2.0.1.Finalvalidation-api-2.0.1.Final.jar;D:Apachemaven
    epositoryorgjbossloggingjboss-logging3.3.3.Finaljboss-logging-3.3.3.Final.jar;D:Apachemaven
    epositorycomfasterxmlclassmate1.4.0classmate-1.4.0.jar;D:Apachemaven
    epositoryorgspringframeworkspring-web5.1.10.RELEASEspring-web-5.1.10.RELEASE.jar;D:Apachemaven
    epositoryorgspringframeworkspring-beans5.1.10.RELEASEspring-beans-5.1.10.RELEASE.jar;D:Apachemaven
    epositoryorgspringframeworkspring-webmvc5.1.10.RELEASEspring-webmvc-5.1.10.RELEASE.jar;D:Apachemaven
    epositoryorgspringframeworkspring-aop5.1.10.RELEASEspring-aop-5.1.10.RELEASE.jar;D:Apachemaven
    epositoryorgspringframeworkspring-context5.1.10.RELEASEspring-context-5.1.10.RELEASE.jar;D:Apachemaven
    epositoryorgspringframeworkspring-expression5.1.10.RELEASEspring-expression-5.1.10.RELEASE.jar;D:Apachemaven
    epositoryorgslf4jslf4j-api1.7.28slf4j-api-1.7.28.jar;D:Apachemaven
    epositoryorgspringframeworkspring-core5.1.10.RELEASEspring-core-5.1.10.RELEASE.jar;D:Apachemaven
    epositoryorgspringframeworkspring-jcl5.1.10.RELEASEspring-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:codespringbootspringboot-07	argetclasses started by Administrator in E:codespringbootspringboot-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...
    
  • 相关阅读:
    Linux安装Oracle 10g
    Linux安装Oracle 10g
    如何利用BI实现人力资源可视化管理
    mongdb修改密码
    如何利用BI实现人力资源可视化管理
    jquery获取浏览器和屏幕的高度和宽度
    perl概述
    'CheckLicensesAndNotify' has encountered a problem.
    Caused by: java.sql.SQLException: The MySQL server is running with the --read-only option so it cann
    Caused by: java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Tim
  • 原文地址:https://www.cnblogs.com/jatpeo/p/11767490.html
Copyright © 2020-2023  润新知