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...
7_3.springboot2.x启动配置原理_3.事件监听机制的更多相关文章
- Spring 事件监听机制及原理分析
简介 在JAVA体系中,有支持实现事件监听机制,在Spring 中也专门提供了一套事件机制的接口,方便我们实现.比如我们可以实现当用户注册后,给他发送一封邮件告诉他注册成功的一些信息,比如用户订阅的主 ...
- Java swing(awt):事件监听机制的实现原理+简单示例
(1)实现原理 事件监听机制的实现: 参考图:事件模型_ActionEvent 为了节省资源,系统无法对某个事件进行实时的监听.故实现的机制是当发生某个事件后,处理代码将被自动运行,类似钩子一般.(回 ...
- GUI编程笔记(java)05:GUI事件监听机制原理和举例说明
1.事件监听机制: A:事件源 事件发生的地方 B:事件 就是要发生的事情 C:事件处理 就是针对发生的事情做 ...
- SpringBoot事件监听机制源码分析(上) SpringBoot源码(九)
SpringBoot中文注释项目Github地址: https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE 本篇接 SpringApplicat ...
- java Gui编程 事件监听机制
1. GUI编程引言 以前的学习当中,我们都使用的是命令交互方式: 例如:在DOS命令行中通过javac java命令启动程序. 软件的交互的方式: 1. 命令交互方式 图书管理系统 ...
- Halo 开源项目学习(六):事件监听机制
基本介绍 Halo 项目中,当用户或博主执行某些操作时,服务器会发布相应的事件,例如博主登录管理员后台时发布 "日志记录" 事件,用户浏览文章时发布 "访问文章" ...
- 简单剖析Node中的事件监听机制(一)
使用js的class类简单的实现一个事件监听机制,不同于浏览器中的时间绑定与监听,类似于node中的时间监听,并且会在接下来的文章中去根据自己的理解去写一下Event模块中的原理. Node.js使用 ...
- SpringBoot事件监听机制及观察者模式/发布订阅模式
目录 本篇要点 什么是观察者模式? 发布订阅模式是什么? Spring事件监听机制概述 SpringBoot事件监听 定义注册事件 注解方式 @EventListener定义监听器 实现Applica ...
- 4.JAVA之GUI编程事件监听机制
事件监听机制的特点: 1.事件源 2.事件 3.监听器 4.事件处理 事件源:就是awt包或者swing包中的那些图形用户界面组件.(如:按钮) 事件:每一个事件源都有自己特点有的对应事件和共性事件. ...
随机推荐
- .net Core使用EFCore连接数据库
一.SQL Service 1.创建实体类 public class Student { public int Id { get; set; } [Required] [Display(Name =& ...
- Exception一自定义异常
异常体系的根类是:Throwable Throwable: Error: 重大的问题,我们处理不了.也不需要编写代码处理.比如说内存溢出. Exception: 一般性的错误,是需要我们对编写 ...
- 【硬盘】RAID卡
独立磁盘冗余阵列,或简称磁盘阵列(Redundant Array of Independent Disks) RAID是一种把多块独立的物理硬盘按不同方式组合起来形成一个逻辑硬盘,一般分为硬RAID卡 ...
- idea和eclipse快捷键对比(转)
分类 功能点 Eclipse快捷键 IDEA快捷键 搜索 搜索文本 Ctrl + F Ctrl + F Ctrl + R 查找替换 Alt + P/A 逐个/全部替换 Alt + F3 查找当前选中词 ...
- Android 配置正式签名和debug签名
为了测试微信分享,微信分享必须有签名信息才能成功调用微信,所以需要debug 下设置签名,方便调试build.gradle里,配置2个签名: signingConfigs { release { ke ...
- 谈谈E语言
基于中国文化底蕴的编程语言, 绝对不是E语言那个样子. 基于中文的编程,必将是计算机届的一次原子爆炸!
- PyTorch常用函数总结
将一个tensor分到多个GPU上:torch.cuda.comm.scatter
- 解决方案-CRM:Vtiger CRM
ylbtech-解决方案-CRM:Vtiger CRM vtiger CRM是一套基于Web以销售能力自动化(SFA)为主的客户关系管理系统(CRM).它基于是SugarCRM专业版(SPL1.1.2 ...
- c++11新特性学习2
noexcept 替代 throw.优点是更安全, 如果noexcept 修饰的函数抛出了异常,编辑器可以直接选择终止程序. C++ 11中析构函数默认为noexcept(true),从而阻止异常的扩 ...
- Spark 自定义函数(udf,udaf)
Spark 版本 2.3 文中测试数据(json) {"name":"lillcol", "age":24,"ip":& ...