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.

Share:
10,056
sash84
Author by

sash84

Updated on June 14, 2022

Comments

  • sash84
    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" and qa-url="qa-abc.com"

    @Value( #{systemProperties. ??? )
    String url;
    
    • Zico
      Zico almost 7 years
      Do you mean you have all these 3 kind of url variable and you want to set String url based on your spring profile?
    • soorapadman
      soorapadman almost 7 years
      You must have different .properties obviously . you can get according to the environment
  • sash84
    sash84 almost 7 years
    Yes 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
    xyz almost 7 years
    use @Value("${spring.profiles.active}-url") String url;