Murali
Murali

Reputation: 13

Specifying value attribute in @PropertySource in SpringBoot

SETUP

I have one jar (say its A) which adds another jar (say its B) as its dependency through Maven. Jar A is from Spring Boot Application.

ISSUE

I have properties file in jar B under its path src/conf/. Am trying to set the path value in the value attribute of @PropertySource in one of java file in jar B. When trying to do it, it refers the src/conf of jar A. How can i achieve this.

@PropertySource( value = { "file:${spring.profiles.path}other-services-dev.properties" })

Here spring.profiles.path gets its value from the deployment script of jar A

Upvotes: 0

Views: 424

Answers (1)

Eduard Wirch
Eduard Wirch

Reputation: 9962

Use "classpath:" instead of file:.

Make the property file a resource of jar B by moving it to src/main/resources. Then you can reference the file like this: @PropertySource("classpath:/other-services-dev.properties").

The better approach, which has better encapsulation, would be to define a class in jar B, which is annotated @PropertySource("classpath:/other-services-dev.properties"), and exposes the properties through getters. Then jar A can simply let this class be injected.

In jar B:

 @PropertySource("classpath:/other-services-dev.properties")
 @Service
 class BProperties
   @Value("${setting1}")
   private String setting1;

   public String getSetting1() {
     return setting1;
   }
 }

In jar A

@Service
class SomeService {
  @Autowired
  private BProperties properties;
}

Upvotes: 1

Related Questions