Search code examples
javaspringspring-environment

Spring 5 getting lists of objects from Environment


using latest Spring 5 on Java 9....

With the following YAML:

flow:
  - name: cats
    url: http://dogs.com
  - name: dogs
    url: http://cats.com

With Environment the nested property values can be pulled as usual (env.getProperty("flow[0].name") to a string). But how do I pull the flow list into an List<Flow>?

Assuming I need a ConfigurationProperties that maps to a Flow class. Don't want to prefix flow in the yaml.

Then via Environment what would the call to getProperty look like (e.g. env.getProperty("flow", List.class) but with the generic List<Flow> reference). As an aside, the reason I want the list of flows is to register beans after the environment is setup (i.e. EnvironmentPostProcessor) with the individual flow configuration.


Solution

  • This should work. Give it a try.

    @Configuration
    @ConfigurationProperties
    @Getter
    @Setter
    public class Configclass {
    
      List<Flow> flow;
    }
    
    @Getter
    @Setter
    public class Flow {
    
      public String name;
      public String url;
    }