SpringBoot 中 @ConfigrationProperties 的使用

default

多层嵌套的 yaml 应用配置

多层嵌套的 yaml 应用配置,必须使用 static class 嵌套,且加上 @ConfigurationProperties 注解,才能被 IDEA 关联到对应的 yml 文件,从 yml 文件跳转到对应配置类。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
@Data
@Configuration
@ConfigurationProperties(prefix = "file")
public class FileConfig {
    
    // 默认路径。在 yml 文件中为 file.root-path
    private String rootPath = "D:/config";
    
    @Data
	@Configuration
	@ConfigurationProperties(prefix = "temp")
    public static class Temp {
        // 默认值
    	private String path = "D:/config/temp";
    }
    
}

对应 yml 文件为:

1
2
3
4
file:
  root-path: D:/config
  temp:
  	path: D:/config/temp
Gear(夕照)的博客。记录开发、生活,以及一些不足为道的思考……