0

I want to be able to load properties from a yaml file and then set a property dynamically everytime the application starts up. The active profile determines the source.system properties below, different per profile as is the back-end.url

yaml looks like this:

source:
  system:
    google:
      uri: https://www.google.com
      user: testUser
      value: {dynamically_set_property}
source:
  system:
    facebook:
      uri: https://www.facebook.com
      user: testUser
      value: {dynamically_set_property}
back-end:
   url: /version1/
     

There is some UTIL class that i have written which takes in the properties above and then uses them to set the dynamic property.

@ConfigurationProperties(prefix=source.system)
public class MyUtil {

private Map<String, SourceSystem> sourceSystems;

//doSomething with the source system
public void setDynamicProperty(String value) {
   SourceSystem source = sourceSystems.get(value);
   //check back-end.url and set source.value
}

}

So on startup when the application runs, I want to set "dynamically_set_property" but to do that I need to have access to the back-end.url property. I have looked at DynamicPropertyResolver but that doesn't give me access to the "back-end.url" property in the static method implementation.

Is implementing EnvironmentAware the only other option? Ideally what I would like is some configuration bean loading before any spring core, 3rd party or other annotated beans within my application and it would run some pre-processing so set the dynamically_set_property.

mvarta
  • 13
  • 4
  • 1
    Just use a config file per profile : `application.yaml`, `application-dev.yaml`, `application-prod.yaml`, ... Another tip : every profile file inherits the values from `application.yaml`, so update only the properties you need to – Aserre May 09 '22 at 10:12
  • I am doing that already, I think you've missed the question, I want to dymanically resolve a property at runtime using some code – mvarta May 09 '22 at 10:14
  • You could set up the dynamic values in an environment variable, then use the @Value annotation : https://stackoverflow.com/questions/44803211/read-environment-variable-in-springboot – Aserre May 11 '22 at 13:59

0 Answers0