如何为@PropertySource设置动态值?

问题描述 投票:3回答:2

我想使用@PropertySource批注设置动态属性源值。谁能告诉我如何实现这一目标?对于以下内容,我具有动态传递属性文件名。

@Configuration
@PropertySource("classpath:message.properties")
public abstract class AbstractCommonAMQPConfiguration {

        @Value("${cust.name}")
    private String custName;

        @Value("${cust.id}")
    private String custId;

}
spring spring-mvc
2个回答
2
投票

我不确定如何使用@PropertySource进行操作,但是您可以通过编程方式定义PropertySourcesPlaceholderConfigurer

   private int some_value = 1;

   @Bean
   public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource("test" + some_value + ".properties"));
        return propertySourcesPlaceholderConfigurer;
    }

0
投票

如果要在测试期间使用这些属性,则可以利用新功能(从Spring Framework 5.2.5和Spring Boot 2.2.6开始:]

@DynamicPropertySource
static void dynamicProperties(DynamicPropertyRegistry registry) {
    registry.add("cust.name", "some property");
}
© www.soinside.com 2019 - 2024. All rights reserved.