An application can have multiple configuration properties. Example: database connection properties and other custom configurations properties. Also, putting all the configuration properties inside our java classes might be a bad idea as it becomes very difficult to maintain the code. Spring boot provides all the support to externalize the application’s configuration properties.
Also, keeping all the application configuration properties in a static property file is a recommended approach. This also makes it easier to maintain the code base, as we can make all the configuration properties available in one file, and separate the code from configuration properties.
In this article, we will learn how to externalize spring boot application configuration properties.
Table of Contents
Using @Value Type safe annotation
Let us say we have a property defined in our spring boot application’s application.properties configuration file as given below.
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/testdb jdbc.username=admin jdbc.password=password
We can use the @Value annotation to bind the above configuration values into our java class variables as given below.
@Configuration public class DbConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Bean public DataSource dataSource() { return DataSourceBuilder.create() .username(username) .password(password) .url(url) .driverClassName(driver) .build(); } }
But, the problem with the above approach is, if we have a configuration class with a huge amount of configuration variables, it becomes tedious to configure the @Value annotation for each property.
Using @ConfigurationProperties annotation
With the @ConfigurationProperties, we can easily map each configuration property to corresponding java configuration class variables.
The above configuration can be rewritten as given below.
@Configuration @ConfigurationProperties(prefix="jdbc") public class DbConfig { private String driver; private String url; private String username; private String password; //Define data source bean.. }
Mapping configuration properties into List
We can also read configuration properties and set them into a list inside the configuration class as given below.
app.props[0]=value1 app.props[1]=value2 app.props[2]=value3
And then inside the configuration class, we can read the values as given below.
@Component @ConfigurationProperties("app") public class AppConfig { private List<String> props; public List<String> getProps() { return prop; } public void setProps(List<String> props) { this.props = props; } }
The @Component annotation registers this configuration class as a spring bean, which can be later injected wherever required.
Mapping configuration properties into Map
We can read configuration properties into a map as shown below.
#Map Properties app.props.key1=value1 app.props.key2=value2
And then in the configuration class, we can use a map as shown below.
@Component @ConfigurationProperties("app") public class AppConfig { private Map<String, String> props; //Getters and setters }
Mapping configuration properties into an object
We can map the configuration properties into an object as given below.
This is very useful if we are having hierarchical configuration properties.
Let us say we have configuration properties as given below.
#Object values: app.myConfig.id=id1 app.myConfig.description=my description!
And then inside the configuration class, we can map it to an object with static class MyConfig as given below.
@Component @ConfigurationProperties("app") public class AppConfig { private MyConfig myConfig; //Getters and Setters.. public static class MyConfig { String id; String description; //Getters and Setters } }
Using validations on configuration properties
We can use validation checks on the configuration properties by adding validation annotations on configuration property class.
An example of configuration properties with validation is given below.
@Configuration @ConfigurationProperties("app") @Validated public class AppConfig { @NotNull String id; @NotEmpty String description; //Getters and Setters }
Conclusion
With Spring boot it is very easy to externalize application configuration properties as it provides good support to handle configuration properties.
Externalizing configurations is very useful while developing spring boot applications, which separates configuration properties from actual code, which makes maintenance of the code easy.
Example project code is available on github.