如何将动态值设置为@PropertySource?

Pan*_*005 5 spring spring-mvc

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

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

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

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

}
Run Code Online (Sandbox Code Playgroud)

dus*_*ltz 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;
    }
Run Code Online (Sandbox Code Playgroud)