Appearance
三、Spring Boot配置文件
我们知道,在项目中,很多时候需要用到一些配置的信息,这些信息可能在测试环境和生产环境下会有不同的配置,后面根据实际业务情况有可能还会做修改,针对这种情况,我们不能将这些配置在代码中写死,最好就是写到配置文件中。比如可以把这些信息写到 application.yml
,application.properties
文件中。
1、properties
常见的一种配置文件格式,Spring中也是用这种格式,语法结构很简单,结构为: key=value 。具体如下:
properties
user.name=xue
user.age=25
user.active=true
user.created-date=2022/05/31 16:54:30
user.map.k1=v1
user.map.k2=v2
上述配置文件中对应的实体类如下:
java
public class User {
private String name;
private Integer age;
private Boolean active;
private Map<String,Object> map;
private Date createdDate;
private List<String> hobbies;
}
2、YML
YAML 是 "YAML Ain't Markup Language"(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。
非常适合用来做以数据为中心的配置文件
- key: value;kv之间有空格
- 大小写敏感
- 使用缩进表示层级关系
- 缩进不允许使用tab,只允许空格
- 缩进的空格数不重要,只要相同层级的元素左对齐即可
- '#'表示注释
- 字符串无需加引号,如果要加,''与""表示字符串内容 会被 转义/不转义
1、数据类型
- 字面量:单个的、不可再分的值。date、boolean、string、number、null
yaml
k: v
- 对象:键值对的集合。map、hash、set、object
yaml
行内写法: k: {k1:v1,k2:v2,k3:v3}
#或
k:
k1: v1
k2: v2
k3: v3
- 数组:一组按次序排列的值。array、list、queue
yaml
行内写法: k: [v1,v2,v3]
#或者
k:
- v1
- v2
- v3
2、示例
说明:
@Data注解是lombok插件提供的
xml<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency>
java
@Data
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;
}
@Data
public class Pet {
private String name;
private Double weight;
}
yaml
# yaml表示以上对象
person:
userName: zhangsan
boss: false
birth: 2019/12/12 20:12:33
age: 18
pet:
name: tomcat
weight: 23.4
interests: [篮球,游泳]
animal:
- jerry
- mario
score:
english:
first: 30
second: 40
third: 50
math: [131,140,148]
chinese: {first: 128,second: 136}
salarys: [3999,4999.98,5999.99]
allPets:
sick:
- {name: tom}
- {name: jerry,weight: 47}
health: [{name: mario,weight: 47}]
3、配置提示
自定义的类和配置文件绑定一般没有提示。
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
4、如何从配置文件取值
1、@ConfigurationProperties
这个注解用于从配置文件中取值,支持复杂的数据类型,但是不支持 SPEL
表达式。 该注解中有一个属性 prefix
,用于指定获配置的前缀,毕竟配置文件中的属性很多,也有很多重名的,必须用一个前缀来区分下。
该注解可以标注在类上也可以标注在方法上,这也注定了它有两种获取值的方式。
1、标注在实体类上
java
@Component
@ConfigurationProperties(prefix = "user")
@Data
@ToString
public class User {
private String name;
private Integer age;
private Boolean active;
private Map<String,Object> map;
private Date createdDate;
private List<String> hobbies;
}
2、标注在配置类中的方法上
标注在配置类上的方法上,同样是从配置文件中取值赋值到返回值的属性中。使用如下:
java
/**
* @Bean : 将返回的结果注入到IOC容器中
* @ConfigurationProperties :从配置文件中取值
* @return
*/
@ConfigurationProperties(prefix = "user")
@Bean
public User getUser() {
return new User();
}
总结
@ConfigurationProperties
注解能够很轻松的从配置文件中取值,优点如下:
支持批量的注入属性,只需要指定一个前缀
prefix
支持复杂的数据类型,比如
List
、Map
对属性名匹配的要求较低,比如
user-name
,user_name
,userName
,USER_NAME
都可以取值支持JAVA的JSR303数据校验
注意: 这个注解仅仅是支持从Spring Boot的默认配置文件中取值,比如 application.properties
、application.yml
。
2、@Value
@Value
这个注解估计很熟悉了,Spring中从属性取值的注解,支持 SPEL
表达式,不支持复杂的数据类型,比如 List 。使用如下:
java
@Value("${user.name}")
private String userName;
5、如何从自定义配置文件中取值
Spring Boot在启动的时候会自动加载 application.xxx 和 bootsrap.xxx ,但是为了区分,有时候需要自 定义一个配置文件,那么如何从自定义的配置文件中取值呢?此时就需要配合 @PropertySource
这个注解 使用了。
java
@SpringBootApplication
@PropertySource(value = {"classpath:my.properties"})
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}
value
属性是一个数组,可以指定多个配置文件同时引入。 @PropertySource
默认加载 xxx.properties
类型的配置文件,不能加载 YML
格式的配置文件
6、如何加载自定义YML格式的配置文件
@PropertySource
注解有一个属性 factory
,默认值是 PropertySourceFactory.class
,这个就是用来加载properties
格式的配置文件,我们可以自定义一个用来加载 YML
格式的配置文件,如下:
java
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import java.io.IOException;
import java.util.Properties;
/**
* @Author: xueqimiao
* @Date: 2022/5/4 18:44
*/
public class YmlConfigFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws
IOException {
String sourceName = name != null ? name : resource.getResource().getFilename();
if (!resource.getResource().exists()) {
return new PropertiesPropertySource(sourceName, new Properties());
} else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
Properties propertiesFromYaml = loadYml(resource);
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
} else {
return super.createPropertySource(name, resource);
}
}
private Properties loadYml(EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
}
}
此时只需要将factory
属性指定为YmlConfigFactory.class
即可,如下:
java
@SpringBootApplication
@PropertySource(value = {"classpath:my.yml"},factory = YmlConfigFactory.class)
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}