开发者学堂课程【SpringBoot快速掌握 - 核心技术:第一步:创建SpringApplication】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/612/detail/9280
第一步:创建 SpringApplication
内容简介
一、Spring Boot 运行流程
二、Spring Boot 启动原理
三、Spring Boot 依赖导入
四、启动配置原理
一、Spring Boot运行流程
(1)创建SpringBootApplication对象
(2)运行main方法
//导入项目所需要的包
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
Import
org.springframework.boot.autoconfigure.SpringBootApplication;
//加入注解
@SpringBootApplication
@MapperScan("com.example.mapper")
//扫描
public class NewdemoApplication {
public static void main(String[] args) {
//传入主方法和args
SpringApplication.run(NewdemoApplication.class, args);
}
}
1. Spring Boot启动流程
2. 创建SpringApplication对象
3. initialize(sources);
4. private void initialize(Object[] sources){
5. //保存主配置类
6. if (sources != null && sources.length >e){
7. this.sources.addAll(Arrays.aslist(sources));
8. //判断当前是否一个web应用
9. this.webEnvironment = deduceWebEnvironment();
10. //从类路径下找到META-INF/spring.factories配置的所有ApplicationContextInitializer;然后保存起来
11.setInitializers((Collection) getSpringFactoriesInstances(
12. ApplicationContextInitializer.class));
13. //从类路径下找到ETA-INF/spring.factories配置的所有ApplicationListener
14. setlisteners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
15. //从多个配置类中找到有main方法的主配置类
16.
this.mainApplicationclass = deduceMainApplicationclass();
运行结果:
this.initializers = {ArrayList@1768) size =6
>0=(DelegatingApplicationContextinitializer@1770)
>1= {ContextldApplicationContextinitializer@1771)
>2={ConfigurationWarningsApplicationContextInitializer@1772)>3 ={ServerPortInfoApplicationContextlnitializer@1773)
>4=(SharedMetadataReaderFactoryContextlnitializer@1774)
>5=(AutoConfigurationReportLogginglnitializer@1775)
二、Spring Boot启动原理
SpringApplication.run(主程序类)
new SpringApplication(主程序类)
判断是否web应用
加载并保存所有
ApplicationContextlnitializer(META-INF/spring.factories)
加载并保存所有 ApplicationListener
获取到主程序类
- run(
回调所有的
SpringApplicationRunListener(MEA-INF/spring.factories)的starting
获取ApplicationArguments
准备环境&回调所有监听器( SpringApplicationRunListener)的environmentPrepared
打印banner信息
创建ioc容器对象(
- AnnotationConfigEmbeddedWebApplicationContext(web环境容器)
- AnnotationConfigApplicationContext(普通环境容器)
- run()
准备环境
-执行ApplicationContextlnitializer. initialize()
-监听器SpringApplicationRunListener回调contextPrepared
-加载主配置类定义信息
-监听器SpringApplicationRunListener回调contextLoaded
刷新启动IOC容器;
-扫描加载所有容器中的组件
-包括从META-INF/spring,factories中获取的所有EhableAutoConfiguration组件
回调容器中所有的ApplicationRunner、CommandLineRunner的run方法
监听器SpringApplicationRunListener回调finished
三、Spring Boot依赖导入
1. Maven导包
添加pom文件:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</ dependency>
<dependency>
<groupId>org.springframework.boot</ groupId>
<artifactId>spring-boot-starter-test</ artifactId><scope>test</scope>
</ dependency>
四、启动配置原理
重要事件回调机制:
ApplicationContextInitializer
SpringApplicationRunListener
ApplicationRunner
CommandLineRunner