一、SpringBoot自动配置原理
1.maven配置:
(1)父依赖:
SpringBoot项目需要添加父依赖进行项目管理,用来控制后续所需框架,场景的版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
(2)配置场景:
传统的Spring、SpringMVC开发中在构建项目时,需要导入大量jar包,例如:Spring的jar包,mysql驱动的jar包;而在SpirngBoot中提供了场景配置,假如我现在需要开发Web项目,我们只需在maven中配置web场景,相关的jar包会自动导入;
<!--starter场景启动器,所有场景启动器的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<!--web场景的启动器,配置后自动web开发中所需要的jar包依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.自动配置:
SpringBoot在启动时,自动往容器加载了很多之前SpringMVC需要手动配置的组件,例如编码拦截器,视图解析器以及扫描注入bean等等,这些组件是如何加载的?
(1)@SpringBootApplication注解:
@SpringBootApplication注解式标注在SpirngBoot启动类上
@SpringBootApplication
public class BootMain {
public static void main(String[] args) {
ConfigurableApplicationContext application = SpringApplication.run(BootMain.class,args);
}
}
@SpringBootApplication注解由以下注解构成:
(1)@SpringBootConfiguration:其实就相当于@Configuration代表当前是一个配置类
(2) @EnableAutoConfiguration:
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}
在AutoConfigurationImportSelector类中,有一个方法:
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
getAutoConfigurationEntry(annotationMetadata)方法中调用了:
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
该方法是利用工厂加载 Map<String, List> loadSpringFactories(@Nullable ClassLoader classLoader);扫描spring-boot-autoconfigure-2.3.4.RELEASE.jar包里的META-INF/spring.factories配置文件,得到所需要加载的组件的名称,从而加载到容器中:
这么多的组件加载到容器中也不是全部生效,在springboot中有一个注解:@ConditionalOnXXXX,通过这个注解,可以为某个组件设置为按照条件进行生效。并且每一个组件默认都会和xxxProperties配置文件进行了绑定。这样我们可以通过修改properties配置文件去自定义改组件。
(3) @ComponentScan:指定扫描哪些,Spring注解;
3 注解总结:
1、@Configuration:
标注配置类,在该类里面的可以使用@Bean为某一些特定的方法进行标注,eg:
@Configuration
public class Config {
@Bean
public User user01(){
return new User("zhangsan",13);
}
@Bean
public Pet tomcat(){
return new Pet();
}
}
@bean标注的方法则会往容器中注入一个bean实体,类型为方法返回值类型,名称为方法名;
2、@Conditional
条件装配,例如上述所说的只有在某个条件下,才会注入bean到容器中。
3、@ConfigurationProperties
通过配置文件自动装配改注解所标注的类,但是该类必须在容器中,常配合@Component使用
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
private Pet pet;
private String[] interests;
private List<String> animal;
private Map<String, Object> score;
private Set<Double> salarys;
private Map<String, List<Pet>> allPets;
}
yaml配置文件
person:
age: 18
user-name: zhangsan
pet:
name: xiaogou
weight: 20.00
birth: 2020/10/12
boss: false
all-pets:
sick:
- {name: xiaomao,weight: 19.00}
- {name: xiaoniao,weight: 2.00}
-health:
- {name: xiaoji,weight: 0.00}
animal:
- haha
- xixi
- miemie
interests: [足球,篮球,排球]
salarys: 2000000.00
score:
english: 100
math: 0