如何在Spring Boot 2.2.4中与@ConfigurationProperties一起使用@ConstructorBinding和@PropertySource?

问题描述 投票:1回答:1

我是Spring Boot的新手。当前,我正在尝试创建POJO类(SystemProperties.class)以读取属性文件(parameter.properties)中的值,该文件与application.properties分开,但仍在同一目录/ src / main下/ resources。当我在类中使用@ConstructorBinding使其不可变时,就会发生此问题。

  • @@ ConstructorBinding需要与@EnableConfigurationProperties或@ConfigurationPropertiesScan一起使用。
  • @@@@@@@@@@@@@@@@@@@@@@@@。

A)SystemProperties.class

@Configuration
@PropertySource("classpath:parameter.properties")

@ConstructorBinding
@ConfigurationProperties(prefix = "abc")
public class SystemProperties {

    private final String test;

    public SystemProperties (
            String test) {
        this.test = test;
    }

    public String getTest() {
        return test;
    }

B] parameter.properties

abc.test=text1

我已经尝试删除@PropertySource批注,但是除非它来自application.properties,否则无法检索该值。非常感谢您的帮助!

我是Spring Boot的新手。当前,我正在尝试创建一个POJO类(SystemProperties.class)以读取属性文件中的值(parameter.properties与application.properties分开,但...

java spring spring-boot immutability
1个回答
0
投票

解决此问题的方法是将类分为两个具有两个不同关注点的类。使用这种解决方案,您可以保留创建的SystemProperties类,并另外添加一个仅用于加载属性文件参数的类,以使它们对您的应用程序可用。

© www.soinside.com 2019 - 2024. All rights reserved.