事件监听机制
配置在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...

7_3.springboot2.x启动配置原理_3.事件监听机制的更多相关文章

  1. Spring 事件监听机制及原理分析

    简介 在JAVA体系中,有支持实现事件监听机制,在Spring 中也专门提供了一套事件机制的接口,方便我们实现.比如我们可以实现当用户注册后,给他发送一封邮件告诉他注册成功的一些信息,比如用户订阅的主 ...

  2. Java swing(awt):事件监听机制的实现原理+简单示例

    (1)实现原理 事件监听机制的实现: 参考图:事件模型_ActionEvent 为了节省资源,系统无法对某个事件进行实时的监听.故实现的机制是当发生某个事件后,处理代码将被自动运行,类似钩子一般.(回 ...

  3. GUI编程笔记(java)05:GUI事件监听机制原理和举例说明

    1.事件监听机制:       A:事件源          事件发生的地方       B:事件             就是要发生的事情       C:事件处理       就是针对发生的事情做 ...

  4. SpringBoot事件监听机制源码分析(上) SpringBoot源码(九)

    SpringBoot中文注释项目Github地址: https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE 本篇接 SpringApplicat ...

  5. java Gui编程 事件监听机制

    1.     GUI编程引言 以前的学习当中,我们都使用的是命令交互方式: 例如:在DOS命令行中通过javac java命令启动程序. 软件的交互的方式:   1. 命令交互方式    图书管理系统 ...

  6. Halo 开源项目学习(六):事件监听机制

    基本介绍 Halo 项目中,当用户或博主执行某些操作时,服务器会发布相应的事件,例如博主登录管理员后台时发布 "日志记录" 事件,用户浏览文章时发布 "访问文章" ...

  7. 简单剖析Node中的事件监听机制(一)

    使用js的class类简单的实现一个事件监听机制,不同于浏览器中的时间绑定与监听,类似于node中的时间监听,并且会在接下来的文章中去根据自己的理解去写一下Event模块中的原理. Node.js使用 ...

  8. SpringBoot事件监听机制及观察者模式/发布订阅模式

    目录 本篇要点 什么是观察者模式? 发布订阅模式是什么? Spring事件监听机制概述 SpringBoot事件监听 定义注册事件 注解方式 @EventListener定义监听器 实现Applica ...

  9. 4.JAVA之GUI编程事件监听机制

    事件监听机制的特点: 1.事件源 2.事件 3.监听器 4.事件处理 事件源:就是awt包或者swing包中的那些图形用户界面组件.(如:按钮) 事件:每一个事件源都有自己特点有的对应事件和共性事件. ...

随机推荐

  1. springmvc之json交互406异常(Not Acceptable)和415异常(Unsupported Media Type)

    一. 406异常(Not Acceptable) 1. 没有添加jackson-databind包2. 请求的url的后缀是*.html.在springmvc中如果请求的后缀是*.html的话,是不可 ...

  2. Java ArrayList使用技巧 - 两个ArrayList去除重复的元素

    方法一.ArrayList中提供的removeAll方法(效率最低) List1.removeAll(mSubList); 方法二.双重循环(比方法一效率高) 双重循环分为内外两层循环,经过测试,将元 ...

  3. Jmeter-【beanshell处理器】-随机获取手机号

    一.通过操作变量 二.引用外部Java文件 三.引用外部class文件

  4. Web API 接口参考

    Web API 接口参考:https://developer.mozilla.org/zh-CN/docs/Web/API

  5. 求最长的任意两元素差不超过M的子段——双指针+单调队列hdu4123

    换根dp的部分比较容易,难点在于求求最长的任意两元素差不超过M的子段 首先会想到双指针维护(尺取法),如果p1,p2间的max-min>M,那么p1向右移动,直到p1,p2间的max-min&g ...

  6. 约数个数求和+线性筛约数——bzoj3994

    这题首先要会线性筛约数个数,并求出前缀和 bool vis[maxn]; int mm,mu[maxn],prime[maxn],num[maxn],sum[maxn],d[maxn],sum1[ma ...

  7. NX二次开发-UFUN查询体的类型为实体还是片体UF_MODL_ask_body_type

    NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...

  8. CSS 圣杯布局

    前端的两个经典布局想必大家都有多了解--圣杯布局和双飞翼布局,因为它既能体现你懂HTML结构又能体现出你对DIV+CSS布局的掌握. 事实上,圣杯布局其实和双飞翼布局是一回事.它们实现的都是三栏布局, ...

  9. Java Heap and Stack

    Heap(堆)(FIFO): heap是一个运行时数据区, 类的对象从中分配空间.这些对象通过new.newarray.anewarray和multianewarray等指令建立,它们不需要程序代码来 ...

  10. Delphi中的Sender:TObject对象解析转载

    https://blog.csdn.net/jl_tiny/article/details/24376661 Delphi中的Sender:TObject对象解析 procedure TForm1.B ...