Spring Boot properties that depend on profile-specific properties

955 Views Asked by At

I have a Spring Boot application which should connect to different servers in dev and prod, with many services running on those servers. To this point, I have created the configuration like this:

application.properties:

server.url.srv1=${server.url.base}/srv1
server.url.srv2=${server.url.base}/srv2
server.url.srv3=${server.url.base}/srv3

application-dev.properties:

server.url.base=http://192.168.86.17

application-prod.properties:

server.url.base=https://10.11.12.3

Yet when I initialize a bean argument with @Value("${server.url.srv1}"), I get a string of "${server.url.base}/srv1" and not "http://192.168.86.17/srv1" or "https://10.11.12.3/srv1" as expected.

Is this doable at all? It should be if the "${}" references are only resolved once all the config files are loaded, but this doesn't seem to be the case.

I have searched for an answer on both the Spring site, on Google (which pointed me to an otherwise useful Baeldung site), and here, but found nothing relevant to my particular question.

2

There are 2 best solutions below

2
On BEST ANSWER

Placeholders in the application.properties should work. Please refer sample project I have added with your use case and it work as expected: https://github.com/itsprav/spring-profile-properties-using-placeholder

1
On

When you run your application you must have to set the specific spring-profile to be set in order to get the specific properties defined previously.

There is many ways to set these profiles.

Setting Profiles in different ways (JVM, Programmatically, Environment Variable...)