Skip to content

Nested configuration properties Spring Boot

  • by
Nested configuration properties Spring Boot

1. Overview

In this article, we will learn to implement nested configuration properties in a Spring boot application. To learn more about using the external properties in your Spring application, refer to these articles.

2. Spring boot Configuration Properties

You may have used the @Value annotation to inject the configuration properties in your spring application. However, if you are working with multiple properties or properties which are hierarchical or nested, it could be cumbersome and introduce boilerplate code.

So Spring Boot provides an alternative method @ConfigurationProperties of working with properties that let strongly typed beans govern and validate the configuration of your application.

3. Nested Configuration Properties

You can add this @ConfigurationProperties annotation to a class definition or a @Bean method in a @Configuration class if you want to bind and use some external Properties (e.g. from an application.properties file).

Consider that the application property file contains the following properties.

server.error.path=/error
server.port=9000

You can bind the above properties to Java fields by annotating the class using the @ConfigurationProperties. Note that the @ConfigurationProperties support properties that are hierarchical.

For example, the server.error.path is hierarchical and nested property i.e., the root server prefix property contains the error nested property. The @ConfigurationProperties support multiple prefixes that have the same root prefix like below:

@Configuration
@ConfigurationProperties(value = "server")
public class AppProperties {
    private String address;
    private Error error = new Error();
    public class Error {
        private String path;
        public String getPath() {
            return path;
        }
        public void setPath(String path) {
            this.path = path;
        }
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public Error getError() {
        return error;
    }
    public void setError(Error error) {
        this.error = error;
    }
}

However, there are scenarios where the properties have completely different prefixes.

For example, the following property file contains different prefixes serverA and serverB.

serverA.address=127.0.0.1
serverA.port=9000
serverB.error=custom
serverB.port=8080

Let’s create a Java POJO class that maps the properties to fields.

public class AppProperties {
    private String address;
    private String port;
    private String error;
}

You can create different beans for each prefix by using the same AppPoperties file in your application configuration file.

@Configuration
public class AppConfig {
    @Bean
    @ConfigurationProperties(prefix = "serverA")
    public AppProperties serverA() {
        return new AppProperties();
    }
    @Bean
    @ConfigurationProperties(prefix = "serverB")
    public AppProperties serverB(){
        return new AppProperties();
    }
}

You can use the AppConfig bean to access both the property prefixes in your spring boot application.

@SpringBootApplication
public class ProfilesApplication implements CommandLineRunner {
	@Autowired
	AppConfig appConfig;
	private Logger logger = LoggerFactory.getLogger(ProfilesApplication.class);
	public static void main(String[] args) {
		SpringApplication.run(ProfilesApplication.class, args);
	}
	@Override
	public void run(String... args) throws Exception {
		logger.info(appConfig.serverA.getAddress());
		logger.info(appConfig.serverB.getError());
	}
}

If you want the Spring container to manage the configuration properties as a bean, you can use @EnableConfigurationProperties or in a standard way. See this article for more details.

4. Conclusion

To sum up, we have learned to use nested configuration properties in a Spring boot application. You can find code samples in our GitHub repository.