1

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.

Piotr Sulecki
  • 103
  • 1
  • 9

2 Answers2

1

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

A Praveen Kumar
  • 303
  • 2
  • 12
  • I played for a bit with your project and you're right, in your project this works. I'll need to check again if I'm not doing something stupid in mine. – Piotr Sulecki Sep 02 '20 at 15:44
  • 1
    ...and of course it turns out I am (doing something stupid, that is). I defined a property for dev and prod profiles but not for test, so test profile was failing. And that was the profile I was testing. Stupid me... – Piotr Sulecki Sep 02 '20 at 16:32
0

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...)

  • My question is not how to select a profile. My question is, how to configure a property in main application.properties file based on one defined in a profile-specific one. – Piotr Sulecki Sep 02 '20 at 15:28