目录
1. 三种创建 SpringBoot 项目的方式
2. SpringBoot 主要特性
3. yaml 配置语法
4. 自动配置原理
5. 自动配置特征介绍
6. 静态资源
7. 自定义入参的 Converter 实现
简述:
Spring Boot 是和 Spring 框架紧密结合用于提升 Spring 开发者体验的工具,并不是用来替代 Spring 的。它主要是为了解决使用 Spring 框架需要进行大量的配置太麻烦的问题,降低了项目搭建的复杂度;同时它集成了大量常用的第三方库配置(例如 Jackson, JDBC, Mongo, Redis, Mail等)。
特性:
① 创建独立的 Spring 应用。
② 内嵌的 Server 服务器,避免了 WAR 文件的部署。
③ 使用 Starter 来简化依赖配置。
④ 实现 自动配置。
⑤ 提供生产级别的监控等功能。
⑥ 不再使用XML配置。
1. 三种创建 SpringBoot 项目的方式
配置要求:java8及以上、maven3.5+
<!-- 增加 SpringBoot Maven 父依赖: -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
</parent>
<!-- web 项目则增加 Spring Web 依赖: -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
添加以上依赖,并创建项目的启动类,就可以快速创建 SpringBoot 项目。
// 编写 SpringBoot 项目的主启动类:
@RestController
@EnableAutoConfiguration
public class MyApplication {
@RequestMapping("/")
String home() { return "Hello World!"; }
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
在官网 https://start.spring.io/ 按需选择后创建并下载
File ——> New ——> Project... ——> Spring Initializr
?填写信息:
?选择依赖:
?
2. SpringBoot 主要特性
使用 方式一 创建 SpringBoot 项目时,POM 中仅有一个 spring-boot-starter-parent 的父依赖;点进去之后会发现还有一个 spring-boot-dependencies 的父依赖;再点进去后会发现大量的基础依赖(如下图),从这可以看出 SpringBoot 使用 Starter 来简化依赖配置。
官方文档的描述:
?通过核心 Starter— spring-boot-starter,可以找到自动配置的依赖 spring-boot-autoconfigure,此包包含了 spring 框架的所有基础配置。
(1)默认的扫描路径:
com
+- yuyu
+- demo
+- DemoApplication.java
|
+- customer
| +- Customer.java
| +- CustomerController.java
| +- CustomerService.java
| +- CustomerRepository.java
业务类代码要放到 DemoApplication 的同级或??录下,才能被默认包扫描
(2)自定义扫描路径:
@SpringBootApplication 注解中包含包扫描的方法 scanBasePackages
?通过赋值 scanBasePackages 来进??定义包扫描路径
配置绑定方式有两种:
① @ConfigurationProperties + @Component
自定义一个配置类 Yuyu.java ,使用上面两个注解
@Component
@ToString
@ConfigurationProperties(prefix = "test")
@Data
public class Yuyu {
private String name;
private int age;
}
就可以在配置文件 application.properties 中使用 test.name=yuyu 进行配置。
② @ConfigurationProperties + @EnableConfigurationProperties
例如配置:server.port=8088
首先需要 @ConfigurationProperties 标注配置信息类:
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
再 @EnableConfigurationProperties 开启配置
@Configuration(proxyBeanMethods = false)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(ServletRequest.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(ServerProperties.class)
@Import({
ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,
ServletWebServerFactoryConfiguration.EmbeddedJetty.class,
ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
public class ServletWebServerFactoryAutoConfiguration {
【注】@EnableConfigurationProperties 要放到配置类 @Configuration 上。
最后就可以在配置文件 application.properties 中使用 server.port=8088 进行配置。
IDE 提醒 spring-boot-configurator-processor
自定义配置方式 ① @ConfigurationProperties + @Component 中,IDE没有提示
添加依赖 spring-boot-configurator-processor 后,IDE 会自动提醒
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 因为仅在开发时有用,所以需要在打包时剔除 -->
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
?
3. yaml 配置语法
① key: value (kv之间有空格)
② ??写敏感
③ 使?缩进表示层级关系
④ 注释?#
⑤ 字符串不?加引号
例如:
//StudentYaml.java
@Component
@ConfigurationProperties(prefix = "studentyaml")
@ToString
@Data
public class StudentYaml {
private String name; // 基本类型
private Boolean sex;
private Date birth;
private Integer age;
private Address address; //对象类型
private String[] interests; //数组
private List<String> fridends; // 集合
private Map<String, Double> score;
private Set<Double> weightHis;
private Map<String, List<Address>> allFridendsAddress; }
//Address.java
@ToString
@Data
public class Address {
private String street;
private String city;
}
#applic.yaml
studentyaml:
name: muse
sex: true
birth: 2021/9/5
age: 1
address:
street: 王府井?街
city: 北京
interests: [看书, 写字, 玩电?游戏] # 数组
fridends:
- 李雷 # List集合 -
- 韩梅梅
score: {math: 100,english: 80} # Map 集合
weight-his:
- 176
- 199
- 160
- 179
all-fridends-address: # Map<String, List<Address>> allFridendsAddress;
bestFridendsAddress:
- {street: 中关村?街,city: 北京市}
- street: 上海某?街
city: 上海市
worstFridendsAddress:
- {street: 中关村?街,city: 北京市}
- {street: 中关村?街,city: 北京市}
4. 自动配置原理
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
SpringBoot 项目的启动主要靠注解 @SpringBootApplication :
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes =
TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes =
AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
@SpringBootApplication 注解主要包含以下 三个注解:
@Configuration
@Indexed
public @interface SpringBootConfiguration {
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
下面主要分析 @EnableAutoConfiguration:
1: @AutoConfigurationPackage
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {
Registrar.class
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
register(registry, new PackageImports(metadata).getPackageNames().toArray(new String[0]));
}
@Override
public Set<Object> determineImports(AnnotationMetadata metadata) {
return Collections.singleton(new PackageImports(metadata));
}
}
debug 可以知道:metadata = com.yuyu.demo.DemoApplication 【主程序】
new PackageImports(metadata).getPackageNames()=com.yuyu.demo 【默认的包加载路径】
// AutoConfigurationImportSelector.java
public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware,
ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {
...
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
...
进入:getAutoConfigurationEntry() :获取配置
// AutoConfigurationImportSelector.java
protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
}
AnnotationAttributes attributes = getAttributes(annotationMetadata);
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
configurations = removeDuplicates(configurations);
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = getConfigurationClassFilter().filter(configurations);
fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}
进入:getCandidateConfigurations() :获取配置
// AutoConfigurationImportSelector.java
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
进入:loadFactoryNames() :获取配置
// SpringFactoriesLoader.java
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
ClassLoader classLoaderToUse = classLoader;
if (classLoaderToUse == null) {
classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
}
String factoryTypeName = factoryType.getName();
return loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
}
进入:loadSpringFactories() :从指定路径加载配置
// SpringFactoriesLoader.java
private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
// 首先从缓存里找
Map<String, List<String>> result = cache.get(classLoader);
if (result != null) {
return result;
}
result = new HashMap<>();
try {
Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);
while (urls.hasMoreElements()) {
/** 查找所有我们依赖的jar包:找到对应
META-INF/spring.factories?件,然后获取?件中的内容 */
//第一次循环:jar:file:/.../org/springframework/boot/spring-boot/2.5.3/spring-boot-2.5.3.jar!/META-INF/spring.factories
//第二次循环:jar:file:/.../spring-boot-autoconfigure-2.5.3.jar!/META-INF/spring.factories
//第三次循环:jar:file:/.../spring-beans-5.3.9.jar!/META-INF/spring.factories
URL url = urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
String factoryTypeName = ((String) entry.getKey()).trim();
String[] factoryImplementationNames =
StringUtils.commaDelimitedListToStringArray((String) entry.getValue());
for (String factoryImplementationName : factoryImplementationNames) {
result.computeIfAbsent(factoryTypeName, key -> new ArrayList<>())
.add(factoryImplementationName.trim());
}
}
}
// Replace all lists with unmodifiable lists containing unique elements
result.replaceAll((factoryType, implementations) -> implementations.stream().distinct()
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)));
cache.put(classLoader, result);
}
// Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);
// 点进去 FACTORIES_RESOURCE_LOCATION
public final class SpringFactoriesLoader {
/**
* The location to look for factories.
* <p>Can be present in multiple JAR files.
*/
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
META-INF/spring.factories 默认配置的路径
5. 自动配置特征介绍
1> ?动配置按需加载原理
2> 容错兼容
IOC 容器中有某类型的 bean(例:MultipartResolver) ,但 IOC 中不存在此类型名称的 bean(例:没有名称为 multipartResolver 的bean),此时会容错兼容改个名字(改成默认名字)
@Bean
@ConditionalOnBean(MultipartResolver.class) // 作?于该?法上,要求我们的 IOC 中存在 MultipartResolver 类型的 bean
@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) // IOC 容器中不存在名称为multipartResolver 的 bean
public MultipartResolver multipartResolver(MultipartResolver resolver) {
// 调?者IOC,resolver 就是从 IOC 中获取到的类型为 MultipartResolver 的bean(是 MultipartResolver 类型的但是名称不是 “multipartResolver” )
return resolver; // 返回保存到 IOC中 的 bean 的名字就是 “multipartResolver”
}
//@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
//MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver"
3> ?户配置优先
@Bean
@ConditionalOnMissingBean // 如果IOC中没有defaultViewResolver bean,那么才去创建(你没有创建,我帮你去创建;如果你创建了,那我就以你为主)
public InternalResourceViewResolver defaultViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix(this.mvcProperties.getView().getPrefix());
resolver.setSuffix(this.mvcProperties.getView().getSuffix());
return resolver;
}
4> 外部配置项修改组件?为
@Bean
@ConditionalOnMissingBean
public InternalResourceViewResolver defaultViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
//设置成用户的配置
resolver.setPrefix(this.mvcProperties.getView().getPrefix());
resolver.setSuffix(this.mvcProperties.getView().getSuffix());
return resolver;
}
// application.properties 用户配置
spring.mvc.view.prefix="aa"
spring.mvc.view.suffix="bb"
5> 查看?动配置情况
debug=true
6. 静态资源
相关源码在 WebMvcAutoConfiguration.addResourceHandles(...) 方法中:
?this.mvcProperties.getStaticPathPattern()
?this.resourceProperties.getStaticLocations()
?
7. 自定义入参的 Converter 实现
|