Search code examples
springspring-cloud-config

Spring Cloud Config custom environment repository


I am wondering is there an example how to create a custom EnvironmentRepository for Spring Cloud Config, cause there are git, svn, vault repositories, but I don't wanna use them, I need my custom one. For instance if I just want to store all properties in a Map.


Solution

  • Provide an implementation of the EnvironmentRepository as a bean in your application context. Spring cloud config server then will pick it up automatically. Here's a minimalistic example:

    public class CustomEnvironmentRepository implements 
    EnvironmentRepository
    {
        @Override
        public Environment findOne(String application, String profile, String label)
        {
            Environment environment = new Environment(application, profile);
    
            final Map<String, String> properties = loadYouProperties();
            environment.add(new PropertySource("mapPropertySource", properties));
            return environment;
        }
    }
    

    Note if you have multiple EnvironmentRepository (Git, Vault, Native...) you'd also want to implement the Ordered interface to specify an order.

    A good approach is to look up existing EnvironmentRepository implementation like the VaultEnvironmentRepository from the Spring cloud config server package.