Spring Boot 2.x:SpringBoot
几个重要的事件回调机制:
1、配置在META-INF/spring.factories
ApplicationContextInitializer
SpringApplicationRunlistener
2、需要放在ioc容器中
3、ApplicationRunner
4、CommandLineRunner
一、创建SpringApplication对象
快速创建一个工程,在SpringBootApplication类中,main方法作为一个入口类,在在run()方法处打上断点,debug运行;
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
//保存主配置类
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
//判断当前是否一个web应用
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//从类路径系找到META-INF/spring.factories配置的所有ApplicationContextInitializer,然后保存起来
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
//从类路径系找到META-INF/spring.factories配置的所有ApplicationListener
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//从多个配置类中找到有main方法的主配置类
this.mainApplicationClass = deduceMainApplicationClass();
}
二、运行run方法
接着上一步的继续进行:
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
//获取SpringApplicationRunListeners ,从类路径下METTA-INF/spring.factories
SpringApplicationRunListeners listeners = getRunListeners(args);
//回调所有的SpringApplicationRunListeners.starting方法
listeners.starting();
try {
//封装命令行参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//准备环境
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
//创建环境完成后回调SpringApplicationRunListeners.environmentPrepared();表示环境准备完成
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
//创建applicationContext,决定创建web的ioc还是普通的ioc
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
//准备上下文环境;将environment保存到ioc中;而且applyInitializers()
//applyInitializers():回调之前保存的所有的ApplicationContextInitializer的Initialize方法
//回调所有的SpringApplicationRunListeners.contextPrepared()
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//prepareContext运行完成以后回调所有的SpringApplicationRunListeners的contextLoaded()
//刷新容器;ioc容器初始化(如果是web应用还会创建嵌入式的tomcat);spring注解
//扫描,创建,加载所有组件的地方;(配置类,组件,自动配置)
refreshContext(context);
//从ioc容器中获取所有的ApplicationRunner和CommandLineRunner进行回调
//ApplicationRunner先回调,CommandLineRunner再回调
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
//整个SpringBoot应用启动完成外汇返佣以后返回启动的ioc容器
return context;
}三、事件监听机制
依次将上面的几个监听机制进行创建,
HelloApplicationContextInitializer:
public class HelloApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
System.out.println("ApplicationContextInitializer...initialize..." + applicationContext);
}
}
HelloSpringApplicationRunListener:
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...environment" +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");
}
}
HelloApplicationRunner:
@Component
public class HelloAppicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner...run..");
}
}
HelloCommandLineRuinner:
@Component
public class HelloCommandLineRunner implements CommandLineRunner{
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunner...run..." + Arrays.asList());
}
}
前面提到的ApplicationContextInitializer和SpringApplicationRunlistener是需要进行配置的,配置的位置是在META-INF/spring.factories中,因此创建一个META-INF的文件夹,并创建spring.factories文件,
org.springframework.context.ApplicationContextInitializer=\
com.itlaoqi.springbootspringbootapplication.listener.HelloApplicationContextInitializer
org.springframework.context.SpringApplicationRunListener=\
com.itlaoqi.springbootspringbootapplication.listener.HelloSpringApplicationRunListener
配置完成后启动项目即可进行测试。
原文链接:https://blog.csdn.net/cyl101816/article/details/103582373
Spring Boot 2.x:SpringBoot的更多相关文章
- spring boot 系列之八:SpringBoot处理定时任务
项目经常会用到定时任务,springboot自然是可以通过整合相关组件来实现的. 目前常用的定时任务的实现有两种: 通过spring 自带的定时器任务@Schedule来实现 通过Quartz来实现 ...
- spring boot 系列之七:SpringBoot整合Mybatis
springboot已经很流行,但是它仍需要搭配一款ORM框架来实现数据的CRUD,之前已经分享过JdbcTemplete和JPA的整合,本次分享下Mybatis的整合. 对于mybatis的使用,需 ...
- Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控
Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控 Spring Boot Actuator提供了对单个Spring Boot的监控,信息包含: ...
- Spring Boot入门(五):使用JDBC访问MySql数据库
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序开发的过程中,操作数据库是必不可少的部分,前面几篇博客中,也一直未涉及到数据库的操作,本篇博客 就 ...
- Spring Boot入门(四):开发Web Api接口常用注解总结
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序员的日常工作中,Web开发应该是占比很重的一部分,至少我工作以来,开发的系统基本都是Web端访问的 ...
- Spring Boot入门(二):使用Profile实现多环境配置管理&如何获取配置文件值
在上一篇博客Spring Boot入门(一):使用IDEA创建Spring Boot项目并使用yaml配置文件中,我们新建了一个最原始的Spring Boot项目,并使用了更为流行的yaml配置文件. ...
- Spring Boot入门(一):使用IDEA创建Spring Boot项目并使用yaml配置文件
由于公司最近在做技术转型(从.Net转Java),因此自己也开启了学习Java之路.学习Java怎么能不学习这几年这么火的Spring Boot框架,由于自己有总结的习惯,因此会把学习的过程以博客的形 ...
- (转)Spring Boot 2 (八):Spring Boot 集成 Memcached
http://www.ityouknow.com/springboot/2018/09/01/spring-boot-memcached.html Memcached 介绍 Memcached 是一个 ...
- (转)Spring Boot(二十):使用 spring-boot-admin 对 Spring Boot 服务进行监控
http://www.ityouknow.com/springboot/2018/02/11/spring-boot-admin.html 上一篇文章<Spring Boot(十九):使用 Sp ...
随机推荐
- [CSP-S模拟测试92]题解
A.数列 显然每个数的答案是互相独立的,直接扩欧求解.我们需要最小化$ax+by=gcd(a,b)$中的$|x|+|y|$,而显然当x或y靠近0时答案可能最优,列个不等式求一下即可. 能$O(1)$千 ...
- php的URL查询字符串解析函数
URL查询字符串格式:Data[650][BLN]=40002307312&Data[650][Status]=電聯無人接聽. 解析这种数据使用函数:parse_str(). parse_st ...
- php-mbstring php7.0-zip
sudo apt install php-mbstring sudo apt install zip unzip php7.0-zip
- java 集合框架 List相关接口
AbstractCollection 此类提供 Collection 接口的骨干实现,以最大限度地减少了实现此接口所需的工作. 还有两个抽象方法,具体的迭代器,具体的Collection 的大小 pu ...
- Vagrant 官网文档翻译汇总
入门 Vagrant 入门 - 项目设置 Vagrant 入门 - box Vagrant 入门 - 启动 vagrant 及 通过 ssh 登录虚拟机 Vagrant 入门 - 同步目录(synce ...
- ucenter 验证码看不到的解决办法
ucenter 验证码看不到的解决办法,很简单,很实用,本人亲试成功~http://www.jinyuanbao.cn 把images /fonts /en 的ttf 刪除可以了!
- JSP基础--javaBean
JavaBean 1 JavaBean概述 1.1 什么是JavaBean JavaBean是一种规范,也就是对类的要求.它要求Java类的成员变量提供getter/setter方法,这样的成员变量被 ...
- PHP/HTML混写的四种方式
[整理]PHP/HTML混写的四种方式 PHP作为一款后端语言,为了输出给浏览器让浏览器呈现出来,无可避免的要输出HTML代码,下文介绍下我用过的三种PHP/HTML混编方法 1.单/双引号包围法 ...
- 《JAVA设计模式》之备忘录模式(Memento)
在阎宏博士的<JAVA与模式>一书中开头是这样描述备忘录(Memento)模式的: 备忘录模式又叫做快照模式(Snapshot Pattern)或Token模式,是对象的行为模式. 备忘录 ...
- wireshark自动化之tshark命令行
tshark是wireshark安装目录下命令行工具 使用tshark可以通过自动化方式调用wireshark tshark -a duration:30 抓包30秒-w cap.cap 保存为cap ...