Setting variable value based on spring profile
10,056
Solution 1
if you have all properties inside on property file you can use :
@Value("${spring.profiles.active}-url") String url;
Solution 2
I would suggest to avoid profiles as much as possible. Modern applications should strive to follow rule 3 of 12 Factor app:
The twelve-factor app stores config in environment variables
With Spring Boot you would have environment variable URL
environment variable and use it in Spring Boot as ${URL}
. Each environment would have this environment variable configured to correct value.
Author by
sash84
Updated on June 14, 2022Comments
-
sash84 almost 2 years
How to assign value to variable based on region?
Lets says
system properties
,dev-url="dev-abc.com"
,prod-url="prod-abc.com"
andqa-url="qa-abc.com"
@Value( #{systemProperties. ??? ) String url;
-
Zico almost 7 yearsDo you mean you have all these 3 kind of url variable and you want to set String url based on your spring profile?
-
soorapadman almost 7 yearsYou must have different
.properties
obviously . you can get according to the environment
-
-
sash84 almost 7 yearsYes I'm using only one property file and I'm not interested in using 3 property files I tried your below suggestion @Value( #{systemProperties[${spring.profiles.active}'-url'] ) String url; but it is not working.Is any changes required in spel ?
-
xyz almost 7 yearsuse @Value("${spring.profiles.active}-url") String url;