【问题标题】:Springboot @ConfigurationProperties nested yaml properties don't loadSpring Boot @ConfigurationProperties 嵌套的 yaml 属性不加载
【发布时间】:2019-08-09 03:26:15
【问题描述】:

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

配置:

spring:
  profile: junit
  profiles:
    include: base

配置类:

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

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


    public class Profiles
    {
        private String include;
    // getters and setters
    }
}

主类:

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

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

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

【问题讨论】:

    标签: java spring-boot


    【解决方案1】:

    您需要实例化您的 profiles 属性

    private Profiles profiles = new Profiles();
    

    就是这样。

    这是因为你的内部class 不是 static
    你不能直接实例化这种类型的class,而只能在封闭的上下文中。

    让你的class static 你会很高兴

    public static class Profiles { ... }
    

    【讨论】:

    • 这行得通,谢谢。尽管在我看来,所有教程和停靠栏都说您在添加字段时不必调用属性的构造函数。
    猜你喜欢
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    相关资源
    最近更新 更多