Inject Properties from Properties File using @ConfigurationProperties Annotation
This tutorial will show you the better way to handle properties using @ConfigurationProperties Annotation.
In previous article, we have learned about using @Value annotation to inject properties from properties file. This approach has disadvantage that if we wanna change any property, we have to update all files which referenced it.
– Java 1.8 – Maven 3.3.9 – Spring Tool Suite – Version 3.8.1.RELEASE (It’s OK if you use Eclipse)
II. Overview
1. Project Structure
– In the project, we use a separate configuration properties classes annotated with @ConfigurationProperties, so property values can be bound to structured objects conveniently. With this approach, when we wanna find or change the handling properties object, we just notice a separate component class. In this example, it is class AppicationProperties.
– For configuration class of the whole application (AppConfig.java), we should enable class above by using @EnableConfigurationProperties annotation.
– To get environment properties value, we create a Service and inject AppicationProperties object and use getter methods easily.
2. Step to do
– Create Maven project – Add Dependencies & Plugins – Add Properties File – Create Properties Class – Create Configuration Class – Create a Service – Create MainApplication Class – Run Application & Enjoy Result
III. Practice
1. Create Maven project
– Open Spring Tool Suite, on Menu, choose File -> New -> Maven Project. – Check Create a simple project, choose Workspace Location and click Next. – Fill all fields in Artifact group box, then click Finish.
Under package config/properties, create ApplicationProperties.java:
package com.javasampleapproach.configproperties.config.properties;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(ignoreUnknownFields = false, prefix = "app")
public class ApplicationProperties {
@NotNull
private Connection connection;
@NotBlank
private String host;
@Max(value = 65535)
private int port;
@NotBlank
private String username;
// @NotBlank
private String password;
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static class Connection {
private String url;
private String name;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
– prefix = “app” indicates that any property defined with the app prefix will be mapped onto ApplicationProperties bean.
– ignoreUnknownFields = false helps to throw an exception when there is any property which doesn’t match any declared field in the configuration properties class.
– to make sure that our application fails fast at the beginning instead of misbehaving, we add Validation Annotation @NotNull, @NotBlank, @Max… before fields of class.
– in case of nested key-values, we can use a custom structure comfortably, just create an inner class with appropriate properties and getters/setters. In this example, to match with connection[url,name], we have Connection class with 2 members: url and name.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. ACCEPT
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.