Spring 项目默认是使用.properties
文件作为配置文件的,但是我们习惯上会更多的使用.yml
文件,因为其可以体现出配置项的树形结构,可读性较强。
配置文件乱象
一般在配置数据源、端口号、服务名称这些配置时并不会出现黄色警告,但是一些自定义的配置项则会出现一片黄色的骇人景象
。


虽然有警告,但是这并不是致命错误,因为你仍然可以正常的使用 @Value("${test.a}")
来取值,但是作为有代码洁癖的人,这不能忍!而且 @Value
这种方式很容易因大意导致名称不一致,进而引起无法取值。
主角登场
我们先去没有警告的配置项 spring.datasource.url
代码内部,看看是怎么写的,能不能找点蛛丝马迹。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @ConfigurationProperties( prefix = "spring.datasource" ) public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean { private ClassLoader classLoader; private boolean generateUniqueName = true; private String name; private Class<? extends DataSource> type; private String driverClassName; private String url; private String username; private String password; private String jndiName; }
|
英语稍懂一些就会立刻注意到 @ConfigurationProperties
这个注解,因为它与我们此次要解决的问题非常相近,翻译过来就是 配置属性
。
基本使用
没错,@ConfigurationProperties
注解就是解决配置文件警告的关键,那么他是如何使用的?
关系映射实体类
像使用 MyBatisPlus
那样,我们需要先创建一个 关系映射实体类
,目的是告诉 Spring,服务启动时你把哪些配置项的值给我塞到这个实体类中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.zyan.test.pojo.properties;
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.stereotype.Component;
@Data @Component @ConfigurationProperties(prefix = "test") public class TestProperties {
private String aProperty;
private String b;
private Boolean enable = Boolean.FALSE; }
|
需要注意这个实体类必须包含 Setter
和 Getter
方法,否则你将取不到值和无法取值。并且这个类必须使用 @Component
注解交由 Spring 管理,或者在服务启动类上使用 @ConfigurationPropertiesScan
注解进行扫描。
@ConfigurationProperties
注解的 prefix
用于指定配置项前缀,可以使用 .
进行多级指定。不能使用驼峰命名,只能使用 -
进行分词。
可以直接在此类中为属性赋上默认值。
取值
1 2 3 4 5 6 7 8 9 10 11 12
| @Service @RequiredArgsConstructor public class TestService {
private final TestProperties testProperties;
@Override public void test() { System.out.println("配置项 test.enable 取值:" + testProperties.getEnable()); } }
|
将 TestProperties
注入到适当的位置,通过 Getter
方法进行取值。此处使用 @RequiredArgsConstructor
进行构造器注入,想要详细了解使用方法,可阅读 如何优雅的进行 Bean 注入。
配置
1 2 3 4
| test: a-property: xxx b: xxx enable: false
|
按照习惯,建议在 .yml
文件中使用 -
进行分词,在实体类中使用驼峰命名,不要担心这样无法取值,Spring
会帮你进行转化,当然,你也可以选择不这么做。
进阶用法
多级配置
多级配置只需要引入静态内部类即可
映射类写法
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Data @Component @ConfigurationProperties(prefix = "test") public class TestProperties {
private Other other;
@Data public static class Other { private String a; private String b; } }
|
yml 文件写法
1 2 3 4
| test: other: a: xxx b: xxx
|
列表配置
映射类写法
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Data @Component @ConfigurationProperties(prefix = "test") public class TestProperties {
private List<Other> otherList;
@Data public static class Other { private String a; private String b; } }
|
yml 文件写法
1 2 3 4 5 6 7 8
| test: other-list: - a: xxx b: xxx - a: xxx b: xxx
|