简体   繁体   中英

Spring Boot properties that depend on profile-specific properties

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM