5

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;

}
2
  • Just use placeholders and either define them environment or JVM properties (-D) that is the only way you can dynamically specify it. But why? Why not simply place it in a well known location and load it from there... Commented Dec 16, 2013 at 13:07
  • Thanks for the response Deinum. The reason for loading different properties file is, I have 5 services(1, 2,..5) for each service properties values are different. Like, for the 1st service I want load service1.properties, and so on. Here only values are different but bean properties are same. So, to achieve this am trying load dynamic properties file with annotation. Could you please suggest if we have any other approach? Commented Dec 16, 2013 at 13:20

2 Answers 2

2

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to use those properties during tests you can leverage new functionality (as of Spring Framework 5.2.5 and Spring Boot 2.2.6):

@DynamicPropertySource
static void dynamicProperties(DynamicPropertyRegistry registry) {
    registry.add("cust.name", "some property");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.