Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set dynamic value to @PropertySource?

I want to set dynamic property source value using @PropertySource annotation. Can any one tell me how to achieve this? For the below I have pass properties file name dynamically.

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

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

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

}
like image 530
Pand005 Avatar asked Nov 01 '22 08:11

Pand005


1 Answers

I'm not sure how to do it with @PropertySource but you can define a PropertySourcesPlaceholderConfigurer programmatically:

   private int some_value = 1;

   @Bean
   public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource("test" + some_value + ".properties"));
        return propertySourcesPlaceholderConfigurer;
    }
like image 191
dustin.schultz Avatar answered Nov 15 '22 07:11

dustin.schultz