1
votes

Hey guys I have been trying to determine which properties in Spring boot were configured by users vs. spring auto-configuration. I am able to list all properties, but I am not certain how to just get the properties declared by the user.

//This method gets all properties in the Spring environment

public Properties getSpringEnvironmentProperties() {
    Properties props = new Properties();
    MutablePropertySources propSrcs = ((AbstractEnvironment) env).getPropertySources();
    StreamSupport.stream(propSrcs.spliterator(), false)
        .filter(ps -> ps instanceof EnumerablePropertySource)
        .map(ps -> ((EnumerablePropertySource) ps).getPropertyNames())
        .flatMap(Arrays::<String>stream)
        .forEach(propName -> props.setProperty(propName, env.getProperty(propName)));
    return props;
  }

Is there any way to only get properties that are user declared in files like: application.properties?

1

1 Answers

1
votes

You can use @ConfigurationProperties for the Complex Properties file, following the steps:

Step 1: Create application.properties which store these properties

#App
app.menus[0].label=Home
app.menus[0].path=/
app.menus[1].label=Java Core
app.menus[1].path=/javacore
app.menus[2].label=Spring
app.menus[2].path=/spring

Step 2: Declare java properties class

@ConfigurationProperties("app")
public class AppProperties {

    private List<Menu> menus = new ArrayList<Menu>();
    //do stuff
}

Step 3: Inject properties into your service

private AppProperties app;

You can find the working example from the post Spring boot configuration properties example

Hope this help!