Springboot @ConfigurationProperties 嵌套的 yaml 属性不加载

Ale*_*hen 5 java spring-boot

由于某种原因,非嵌套属性加载但嵌套不加载。

配置:

spring:
  profile: junit
  profiles:
    include: base
Run Code Online (Sandbox Code Playgroud)

配置类:

@ConfigurationProperties(prefix = "spring")
public class MyFirstProperties {

    private String profile;
    private Profiles profiles;
    // getters and setters


    public class Profiles
    {
        private String include;
    // getters and setters
    }
}
Run Code Online (Sandbox Code Playgroud)

主要类:

    @SpringBootApplication
    @EnableConfigurationProperties(MyFirstProperties.class)
    public class Main {
        public static void main(String... args) {
            SpringApplication.run(Main.class, args);
        }
}
Run Code Online (Sandbox Code Playgroud)

当我将配置类注入控制器并为非嵌套属性调用 getter 时,它会返回其值。但是嵌套属性的 getter 返回 null。

使用 ConfigurationProperties 及其自己的前缀注释内部类似乎不起作用。我错过了什么吗?

Lpp*_*Edd 5

你需要实例化你的profiles财产

private Profiles profiles = new Profiles();
Run Code Online (Sandbox Code Playgroud)

就是这样。

发生这种情况是因为你的内心class 不是 static.
您不能class直接实例化这种类型,而只能在封闭的上下文中实例化。

让你的class static,你会很高兴去

public static class Profiles { ... }
Run Code Online (Sandbox Code Playgroud)