简体   繁体   English

Springboot - @ConfigurationProperties 不填充类字段

[英]Springboot - @ConfigurationProperties not populating the class fields

I know similar questions have been asked many times with different variations.我知道类似的问题已经被问过很多次,但有不同的变化。 I have tried to learn from them and from the available tutorials and try to get it working but I think I am missing something that I am not able to figure out.我试图从他们和可用的教程中学习并尝试让它工作,但我想我错过了一些我无法弄清楚的东西。

I want to load a configuration from the external yaml file admin-config.yml , and not the default application.yml in my case.我想从外部 yaml 文件admin-config.yml加载配置,而不是我的默认application.yml

This is what I have done so far.这是我迄今为止所做的。

  1. Annotated the Application file with @EnableConfigurationProperties使用@EnableConfigurationProperties注释应用程序文件
@SpringBootApplication
@EnableConfigurationProperties
public class MyServer {

    public static void main(String[] args) {
        SpringApplication.run(MyServer.class, args);
    }
}
  1. The AdminConfig component class: AdminConfig组件类:
@Component
@PropertySource("classpath:admin-config.yml")
@ConfigurationProperties(prefix = "admin-config")
public class AdminConfig {

    private List<PrimeModerator> primeModerators;

    @PostConstruct
    public void init() {
        System.out.println(primeModerators);    // this is null
    }

    public void setPrimeModerators(List<PrimeModerator> primeModerators) {
        this.primeModerators = primeModerators;
    }

    public List<PrimeModerator> getPrimeModerators() {
        return primeModerators;
    }
}

  1. The PrimeModerator class PrimeModerator
@Data
public class PrimeModerator implements Serializable {
    private long id;
    private String name;
    private List<String> roles;
    private String details;
    private Date created;
}
  1. The admin-config.yml . admin-config.yml
admin-config:
  primeModerators:
    - !!com.mydomain.model.PrimeModerator
      id: 1
      name: Pawan
      roles: [super-admin]

    - !!com.mydomain.model.PrimeModerator
      id: 2
      name: Prashant
      roles: [admin, moderator]

I have created a unit test case which always fails (which should not).我创建了一个总是失败的单元测试用例(不应该)。

@SpringBootTest
@RunWith(SpringRunner.class)
public class AdminConfigTest {

    @Autowired
    private AdminConfig adminConfig;

    @Test
    public void testAdminConfig() {
        assertNotNull(adminConfig.getPrimeModerators());    // this fails
    }
}

I am not sure what I am missing here.我不确定我在这里缺少什么。 Using springboot version 2.2.1.RELEASE with jdk8.使用springboot版本2.2.1.RELEASE与jdk8。

Thanks for your answers in advance!提前感谢您的回答!

from this documentation : Baeldung - Spring来自本文档: Baeldung - Spring

It says : " The Hibernate Validation framework uses standard Java bean getters and setters, so it's important that we declare getters and setters for each of the properties. "它说:“ Hibernate Validation 框架使用标准的 Java bean getter 和 setter,所以我们为每个属性声明 getter 和 setter 很重要。

Could you please test by adding getters/settest in PrimeModerator ?您能否通过在 PrimeModerator 中添加 getter/settest 来进行测试? Did you try with a single simple String value before you nested array ?在嵌套数组之前,您是否尝试过使用单个简单的 String 值? Good luck...祝你好运...

A github sample on Baeldung in the bottom of page 页面底部 Baeldung 上的 github 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM