0

I have 2 URLs among which 1 is specific to Dev and the other to Prod. I am also using Spring profiling where i have seperate file for dev and prod application-dev.properties and application-prod.properties and my appication.properties file look like this for Dev env

spring.profiles.active=dev

Now in my java code i want to have one property which will bind to the appropriate value depending on the spring profile i am using. How can i do it.

Current Java Class:-

//DEV
    private static final String WIND_RESOURCE_EXTRACTOR_URL = "https://localhost:9090/dev";

    //FOR PROD
    //private static final String WIND_RESOURCE_EXTRACTOR_URL = "https://localhost:9090/prod";

SO i want to mention this properties in my application-dev.properties or application-prod.properties file and my java class should pick the correct value based on the current spring profile.

Sunny
  • 858
  • 3
  • 17
  • 39

1 Answers1

0

You just need to inject the value from your properties file. Assuming all the setup has already been done to read from the properties to the enviroment, i assume it has been.

    public class MyClazz {
         private final String myUrl;
         @Autowired
         public MyClazz(@Value("${my.url.property.name}") String myUrl){
           this.myUrl = myUrl;
   }

and within your two properties files place a my.url.property.name=WhateverValueIwant.

Darren Forsythe
  • 10,712
  • 4
  • 43
  • 54