An application can have multiple configuration properties. Example : database connection related properties and other custom configurations properties.

Putting all the configuration properties inside our java classes might be a bad idea as it becomes very difficult to maintain the code.

Keeping all the application related configuration properties in a static property file is a recommended approach. This makes it easier to maintain the code base, as all the configuration properties can be made available in one file. This also separates the code from configuration properties.

Spring boot configuration properties

In this article, we will learn how to externalize spring boot application configuration properties.

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 @Value annotation to bind above given 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();
	}
}

Problem with the above approach is, if we have a configuration class with huge amount of configuration variables, it becomes tedious to configure @Value annotation for each properties.

Using @ConfigurationProperties annotation

With @ConfigurationProperties, we can easily map each configuration property to corresponding java configuration class variables.

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 read configuration properties and set it into a list inside configuration class as given below.

app.props[0]=value1
app.props[1]=value2
app.props[2]=value3

And inside 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;
    }
}

@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 in 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 inside 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, whcih makes maintenance of the code easy.

Example project code is available on github. Happy Coding 🙂 🙂

You may also interested in :