在Spring Boot中使用ConfigurationProperties

时间:2019-07-13 12:25:41

标签: spring-boot properties configuration

我正在尝试使用ConfigurationProperties类来加载我的属性值。一切正常,但使用默认的application.properties文件。

我需要使用另一个文件,例如app-1.properties。如何在下面的代码中进行更改。我的意思是,在我的应用程序中,我将使用默认属性文件和自定义属性文件。

我的问题还在于更改此文件的路径,而不仅仅是更改文件名。

@ConfigurationProperties(prefix = "spring.profs")
@Component
public class ProfsProperties {

    private String network ;

    private String database;

    public String getDatabase() {
        return database;
    }

    public String getNetwork() {
        return network;
    }

    public void setDatabase( String database ) {
        this.database = database;
    }

    public void setNetwork( String network ) {
        this.network = network;
    }
}

我使用此对象的类

@EnableConfigurationProperties
@Configuration
public class questions {

    @Autowired
    private ProfsProperties profsProperties;
}

我的app-1.properties文件

spring.profs.database = Arnaud
spring.profs.network = Bagna

我还想知道是否必须将@Configuration@EnableConfigurationProperties一起使用吗?

1 个答案:

答案 0 :(得分:0)

添加@PropertySource批注或使用@ConfigurationProperties中的位置。

示例:

@PropertySource(value = "classpath:abc.properties")

@ConfigurationProperties(locations = "classpath:abc.properties", prefix = "info")