核心理念
- 能够帮助开发者快速的整合第三方框架
原理:maven依赖整合和自定义starter - 完全去除xml配置,采用注解形式
原理:SpringBoot根据spring体系原生的注解实现包装 - 不需要外部容器,内嵌入服务器
原理:Java语言支持创建Tomcat服务器
自定义starter
Starter是springboot中的一个非常重要的概念,starter相当于模块,它能够将模块所需的依赖整合起来并对模块内的Bean环境进行自动配置,使用者只需要依赖相关功能的starter,无需过多的配置和依赖。
官方命名格式为:spring-boot-starter-{name},以spring-boot-starter开头
自定义的starter的命名格式:{name}-spring-boot-starter
下面自定义 token-redis-spring-boot-starter
。
Maven依赖
<!-- springboot-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- 能够给开发者引入该jar包后配置文件的时候有一定提示 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
配置configurattion类
package com.mayikt.config;
import com.mayikt.propeties.TokenProperty;
import com.mayikt.service.TokenService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description:
* @Author: ChenYi
* @Date: 2020/07/16 08:05
**/
@Configuration
@EnableConfigurationProperties(TokenProperty.class)
public class TokenRedisConfiguration {
@Bean
public TokenService tokenService() {
return new TokenService();
}
}
相关的配置属性类
package com.mayikt.propeties;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @Description:
* @Author: ChenYi
* @Date: 2020/07/16 08:07
**/
@ConfigurationProperties(prefix = "mayikt")
public class TokenProperty {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.mayikt.service;
import com.mayikt.propeties.TokenProperty;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @Description:
* @Author: ChenYi
* @Date: 2020/07/16 08:09
**/
public class TokenService {
@Autowired
private TokenProperty tokenProperty;
public String token() {
return tokenProperty.getUsername() + ">>" + tokenProperty.getPassword();
}
}
META-INF/spring.factories 内容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mayikt.utils.TokenAutoConfiguration
SpringBoot启动流程
核心分为两个步骤
- 创建SpringApplication对象
- 调用SpringApplication run实现启动同时返回当前的容器上下文
具体的路程
-
创建SpringApplication对象Springboot容器初始化操作
-
获取当前的启动类型,原理:判断当前classpatch是否有加载我们的servlet类,返回servletweb启动方式
webApplicationType分为三种类型
1.响应式启动
2.None不会嵌入web容器启动
3.servlet基于web容器启动 -
setInitializers读取springboot包下的META-INF/spring.factories获取对应的ApplicationContextInitializer装配到集合中
-
setListeners读取springboot包下的META-INF/spring.factories获取对应的ApplicationListener装配到集合中
-
mainApplicationClass获取当前运行的主函数
-
调用SpringApplication run方法实现启动
-
StopWatch stopWatch = new StopWatch(),记录我们SpringBoot项目启动时间
-
getRunListeners(args),读取我们的META-INF/spring.factories中的SpringApplicationRunListeners类型存入到集合中
-
listeners.starting();循环调用监听starting方法
-
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
isteners.environmentPrepared(environment);
读取我们配置文件到SpringBoot容器中 -
Banner printedBanner = printBanner(environment); 打印我们SpringBoot Banner
-
建SpringBoot上下文AnnotationConfigServletWebServerApplicationContext对象
-
refreshContext(context); 刷新我们上下文
-
开始创建我们的tomcat容器
-
开始加载我们的SpringMVC
-
afterRefresh 定义一个空的模版给其他子类实现重写
-
listeners.started(context); 使用广播和回调机制通知监听器已SpringBoot容器启动成功
-
listeners.running(context); 使用广播和回调机制通知监听器已SpringBoot容器启动成功running
-
最后的返回当前上下文
classpath 与file读取有那些区别
Classpath 读取编译后的配置文件内容
File读取编译前的配置文件
默认读取配置文件名称为:
application
SpringBoot支持 yml yaml xml properties
参考:蚂蚁课堂