Search code examples
javaspring-bootspring-cloudspring-cloud-netflixspring-cloud-config

Spring Cloud Config- Customizing the Bootstrap context to read config URI from another property Source


So I have the following use case-

I need to customize the bootstrap context to read the uri where the config Server lies from a particular file and then inject that uri into the property spring.cloud.config.uri to get properties from the configServer.

I have been looking at documentation on Spring cloud commons and have experimented around customizing Bootstrap property sources. This, unfortunately does not work, because this acts as another source of properties for the application context, not a source of properties for the bootstrap context( bootstrap.yml or properties).

The only solution I have so far, which is probably not the most elegant one is to set the uri property as a system property before we run the spring Application.

So, something like the following.

            public static void main(String args[]) {

            * Read properties from file and set as system properties*
            System.setProperty("uri","http://localhost:8097");
            SpringApplication.run(Application.class,args);

          }

And then reference the above property in bootstrap.properties as-

          spring.cloud.config.uri=${uri}

This works and is okay,but i would like to do this still in a more Spring Friendly way.

Thank you in advance.


Solution

  • Unfortunately this cant be done.

    You can change the location of bootstrap.yml/properties using config

                     spring.cloud.bootstrap.location
    

    But , you cannot add a bean to read properties which will be part of the Bootstrap context, as the Bootstrap Context will be the parent of the most senior ancestor created by the user himself.