配置文件
- application.properties/application.yml
YAML
- 以数据为中心,比json、xml等更hi合作配置文件
语法
层次、空格
server:
port: 8081
值的写法
- 字面量:普通的值(数字,字符串,布尔)
# k:v 字面直接来写;
# 字符串默认不用加上单引号或双引号;
# 双引号:回车
name: "\n"
# 单引号:普通\n
name: '\n'
- 对象、Map
# 对象(两种写法)
friends:
lastName: zhangsan
age: 20
friends: {lastName: zhangsan,age: 18}
# 数组(两种写法)
pets:
- cat
- dog
- pig
pets: [cat,dog,pig]
配置文件注入(获取Yml的值)
- 配置提示器
# pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
- Person实体
/**
* 1. 将配置文件中配置的每一个属性的值,映射到这个组件中
* 2. ConfigurationProperties(prefix = "person")
* 2.1 告诉boot将本类中的所有属性和配置文件中相关的配置进行绑定
* 2.2 prefix = "person": 配置文件中那个下面的的所有属性一一映射
* 3. 只有这个组件是容器中的组件,才能提供@ConfigurationProperties功能
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private HashMap<String,Object> map;
private List<Object> list;
private Dog dog;
//@Value
public class Person {
/**
* @value: "字面量"/"${key}"从环境变量中取值/"#{spEL}"
*/
@Value("${person.last-name}")
private String lastName;
@Value("#{11*2}")
private Integer age;
@Value("true")
private Boolean boss;
- application.yml
person:
lastName: zhangsan
age: 18
boss: false
birth: 2017/12/12
map: {k1: v1,k2: v2}
list:
- lisi
- zhaoliu
dog:
name: 小狗
age: 12
- 也可以用application.properties
person.last-name=张三
person.age=18
person.birth=2017/12/15
person.boss=false
person.map.k1=v1
person.map.k2=v2
person.list=a,b,c
person.dog.name=dog
person.dog.age=15
- 区别
优先配置文件 | @ConfigurationProperties | @Value |
---|---|---|
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定 | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
- 松散绑定:
- person.firstName:标准写法
- person.first-name:大写用-
- person.first_name:小写用_
- PERSON_FIRST_NAME
- 配置文件注入值校验:JSR303数据校验
@Component
//添加数据校验,只对@ConfigurationProperties有效
@Validated
@ConfigurationProperties(prefix = "person")
public class Person {
@Email
private String E_mail;
指定配置文件
- @PropertySource:加载指定的配置文件
@ConfigurationProperties(prefix = "person")
@Component
@PropertySource(value = {"classpath:entity/person.properties"})
public class Person {
- @ImportResource
- 导入Spring的配置文件,让配置文件里面的内容生效
- 自己写的配置文件(xml)需要在@SpringBootApplication上标注
@ImportResource(locations = {"classpath:spring/*"})
@SpringBootApplication
public class SpringBoot02ConfigApplication {
## 传统配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class = "com.springboot.springboot.service.HelloService" id="helloService"/>
</beans>
配置类
config.MyConfig.java
/**
* @Configuration:当前类是一个配置替代之前的配置文件
*/
@Configuration
public class MyAppConfig {
@Bean //将方法的返回值添加到容器中;容器这个组件默认的id就是方法名
public HelloService helloService(){
System.out.println("配置类");
return new HelloService();
}
}
注入细节
- @Autowired
@Test //如果方法有两个相同类型参数,需要参数名跟bean的name相同
void contextLoads(@Autowired Person stu,@Autowired Person stuTea) {
System.out.println(stu.toString());
System.out.println(stuTea.toString());
}
@Test //如果方法只有一个参数且参数类型只有一个类,自动注入
void contextLoads2(@Autowired Teacher random) {
System.out.println(random.toString());
}
- @Bean
@Bean //Teacher类型 name =teacher
public Teacher teacher(){
return new Teacher("qy");
}
@Bean//Student 类型 name = zs
public Student zs(){
return new Student("zs");
}
@Bean("ls")//Student 类型 name = ls
public Student student(){
return new Student("ls");
}
@Bean("stuTea")//因为参数名是zs,自动注入Student类型bean sz,Teacher自动注入
public Person person2(Student zs, Teacher teacher){
return new Person(zs.getName()+teacher.getName());
}
@Bean("stu")//指定bean name 的注入方法
public Person personA(@Qualifier("ls") Student student){
return new Person(student.getName());
}
配置文件占位符
- 可以使用随机数,random.int(10,100)…
- 属性配置占位符
- 引用前面配置过的属性
- 在属性后加:表示默认值
person.last-name=张三${random.uuid}
person.age=${random.int}
person.map.k1=${person.last-name}
person.dog.name=${person.hello:hello}dog
Profile
-
多Profile文件
- 编写多个主配置文件时,文件名可以是:application-{profile}.properties/yml
- 如application-dev.properties
-
yml支持多文档快方式
server:
port: 8080
spring:
profiles: dev
---
server:
port: 8081
spring:
profiles: dev
---
server:
port: 8082
spring:
profiles: prod
- 激活指定Profile
# 命令行
# 可以在IDEA配置
# 也可以在运行jar包时加上该命令
--spring.profile.active = dev
# 配置文件,在默认配置文件加
spring.profiles.active = dev
# jvm参数
# 可以在IDEA配置
-Dspring.profile.active = dev
配置文件加载位置
-
spring boot 启动会扫描以下未知的application.properties或者application.yml文件作为Spring boot的默认配置文件,以下顺序优先级从高到低,全部加载,互补配置,高级配置会覆盖低级配置,file指项目文件夹
- file:./config/
- file:./
- classpath:/config/
- classpath:/
-
通过spring.config.location
- 项目打包后,用命令行参数的形式,启动项目的时候来制定新的位置
- 会和原来的一起使用
外部配置加载顺序
SpringBoot也可以从以下位置加载配置;优先级从高到低;高优先级的配置覆盖低优先级的配置
- 命令行 :加载时直接加,如
--server.port=8081 server.servlet.context-path=/boot
- jar包外部:跟jar包放在同一个目录下
自动配置原理
原理
- springboot启动的时候加载主配置类,开启了自动配置功能@EnableAutoConfiguration
- @EnableAutoConfiguration作用:将类路径下META-INF/spring.factories 里面所配置的所有EnableAutoConfiguration的值加入到了容器中
- 利用@Import(AutoConfigurationImportSelector.class)给容器导入一些组件
- 查看selectImport方法的内容
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(autoConfigurationMetadata,
annotationMetadata);
# 获取候选的配置
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes)
# 获取指定路径下的配置
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
getBeanClassLoader());
# 臊面所有jar包类路径下META-INF/spring.factories
classLoader.getResources(FACTORIES_RESOURCE_LOCATION)
FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories"
# 将扫描到的这些文件内容包装properties对象
# 从properties中获取到EnableAutoConfiguration.class类(类名)对应的值
# 并把它们添加到容器中
#在以下路径
spring-boot-autoconfigure:2.2.6.RELEASE\spring-boot-autoconfigure-2.2.6.RELEASE.jar!\META-INF\spring.factories
每一个这样的AutoConfiguration类都是容器的一个组件
用他们来做自动配置
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
...
- 每一个自动配置类进行自动配置功能
- 以HttpEncodingAutoConfiguration为例子解释
(Http编码自动配置)
// 表示这是一个配置类,
@Configuration(proxyBeanMethods = false)
//启用指定类的ConfigurationProperties功能
//将配置文件中对应的值和HttpProperties绑定起来
//并把HttpProperties加入到ioc容器中
@EnableConfigurationProperties(HttpProperties.class)
//Spring底层@Conditional注解
//根据不同的条件,如果满足指定的条件,整个配置类里面的配置就会生效
// 判断当前应用是否是web应用,如果是,当前配置类生效
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
//判断当前项目有没有这个类
//CharacterEncodingFilter SpringMVC中进行乱码解决的过滤器
@ConditionalOnClass(CharacterEncodingFilter.class)
//判断配置文件中是否存在某个配置 spring.http.encoding.enabled
// matchIfMissing = true 即使配置文件中不配置spring.http.encoding.enabled,也默认生效
@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)
public class HttpEncodingAutoConfiguration {
//他已经和SpringBoot的配置文件映射了
private final HttpProperties.Encoding properties;
//只有一个有参构造器的情况下,参数的值就会从容器中拿
public HttpEncodingAutoConfiguration(HttpProperties properties) {
this.properties = properties.getEncoding();
}
@Bean //给容器添加一个组件,这个组件的某些值需要从properties中获取
@ConditionalOnMissingBean //容器中没有当前组件才配置
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name());
filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
return filter;
}
- 根据当前不同的条件判断,决定这个配置类是否生效
- 一旦这个类生效,这个配置类就会给容器中添加各种组件
- 这些组件的属性是从对应的properties类中获取的
- 这些类的每一个属性又是和配置文件绑定的
- 所有在配置文件中能配置的属性都是在xxxProperties类中封装着,配置文件能配置什么就可以参照某个功能对应的属性类
//HttpProperties类
//从配置文件中获取指定的值和bean的属性进行绑定
@ConfigurationProperties(prefix = "spring.http")
public class HttpProperties {
精髓
- SpringBoot启动会加载大量的自动配置类
- 查看需要的功能有没有SpringBoot默认写好的自动配置类
- 再来看这个自动配置类中到底配置了哪些组件;(只要要用的组件有,就不需要再来配置)
- 给容器中自动配置类添加组件的时候,会从Properties类中获取某些属性,可以在配置文件中指定这些属性的值
- xxxAutoConfiguration:自动配置类,给容器添加组件
- xxxProperties:封装配置文件中相关属性
细节
- @Conditional派生注解(指定conditionall类的match方法返回true生效)
作用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置里面的所有内容才生效
自动配置类必须在一定的条件下才能生效
- 获得当前自动配置的类
- 通过启用 debug=true 让控制台打印自动配置报告