Article directory
@ConfigurationProperties and @Value annotations are used to obtain property definitions in configuration files and bind them to Java Beans or properties
1. Easy to use
@Configuration
@ConfigurationProperties(prefix = "mail")
public class ConfigProperties {
private String hostName;
private int port;
private String from;
// standard getters and setters
}
@ConfigurationProperties works best withall hierarchical attributes with the same prefix, which is used to bind the attributes starting with mail in the configuration file to POJO. @Configuration can also be replaced with other annotations such as @Component and @Service.
Spring uses some loose rules for binding properties. Therefore, the following variants are all bound to the property hostName:
mail.hostName
mail.hostname
mail.host_name
mail.host-name
mail.HOST_NAME
The content of the configuration file can be as follows (application.properties)
#Simple properties
[email protected]
mail.port=9000
[email protected]
Note: If the @Configuration annotation is not added,
You need to add @EnableConfigurationProperties annotation in the main class to bind properties to POJO, as follows:
@SpringBootApplication
@EnableConfigurationProperties(ConfigProperties.class)
public class EnableConfigurationDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EnableConfigurationDemoApplication.class, args);
}
}
2. Use with @ConfigurationPropertiesScan annotation
Starting with Spring Boot 2.2, Spring scans the classpath to find and register @ConfigurationProperties classes. Therefore, there is no need to annotate such classes with @Component (and other meta-annotations like @Configuration ), not even with @EnableConfigurationProperties :
@ConfigurationProperties(prefix = "mail")
public class ConfigProperties {
private String hostName;
private int port;
private String from;
// standard getters and setters
}
The classpath scanner enabled by @SpringBootApplication found the ConfigProperties class even though we did not annotate this class with @Component.
Additionally, we can use the @ConfigurationPropertiesScan annotation to scan custom locations for configuration properties classes:
@SpringBootApplication
@ConfigurationPropertiesScan("com.xxx.configurationproperties")
public class EnableConfigurationDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EnableConfigurationDemoApplication.class, args);
}
}
3. Nested properties
We can nest List, Map and class in properties
//定义Credentials 类
public class Credentials {
private String authMethod;
private String username;
private String password;
// standard getters and setters
}
We also need to update the ConfigProperties class to use List, Map and class
@Configuration
@ConfigurationProperties(prefix = "mail")
public class ConfigProperties {
private String host;
private int port;
private String from;
private List<String> defaultRecipients;
private Map<String, String> additionalHeaders;
private Credentials credentials;
// standard getters and setters
}
The following properties file will set all fields:
#Simple properties
mail.hostname=mailer@mail.com
mail.port=9000
mail.from=mailer@mail.com
#List properties
mail.defaultRecipients[0]=admin@mail.com
mail.defaultRecipients[1]=owner@mail.com
#Map Properties
mail.additionalHeaders.redelivery=true
mail.additionalHeaders.secure=true
#Object properties
mail.credentials.username=john
mail.credentials.password=password
mail.credentials.authMethod=SHA1
4. Use @ConfigurationProperties to act on @Bean annotated methods
This method is very suitable for binding to third-party components that we cannot control
#Item 类
public class Item {
private String name;
private int size;
// standard getters and setters
}
#将属性绑定到Item实例上
@Configuration
public class ConfigProperties {
@Bean
@ConfigurationProperties(prefix = "item")
public Item item() {
return new Item();
}
}
5. Immutable @ConfigurationProperties bindings
Starting with Spring Boot 2.2, we can use the @ConstructorBinding annotation to bind our configuration properties. This essentially means that classes annotated with @ConfigurationProperties may now be immutable. It is important to emphasize that to use constructor binding we need to explicitly enable our configuration class using @EnableConfigurationProperties or @ConfigurationPropertiesScan
#ImmutableCredentials 类
@ConfigurationProperties(prefix = "mail.credentials")
@ConstructorBinding
public class ImmutableCredentials {
private final String authMethod;
private final String username;
private final String password;
public ImmutableCredentials(String authMethod, String username, String password) {
this.authMethod = authMethod;
this.username = username;
this.password = password;
}
public String getAuthMethod() {
return authMethod;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
Binding parameters through the constructor, without providing a setter, the final modification of the property becomes an immutable property binding.
6. Summary
According to the above summary, we can see several ways to use @ConfigurationProperties
one:
@Configuration //可以换成@Component
@ConfigurationProperties(prefix = "mail")
public class ConfigProperties {
private String hostName;
private int port;
private String from;
// standard getters and setters
}
Method 2:
@ConfigurationProperties(prefix = "mail")
public class ConfigProperties {
private String hostName;
private int port;
private String from;
// standard getters and setters
}
@SpringBootApplication
@EnableConfigurationProperties(ConfigProperties.class)
public class EnableConfigurationDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EnableConfigurationDemoApplication.class, args);
}
}
Note: The @Configuration annotation is not specified, you need to add the @EnableConfigurationProperties annotation to the startup class
Method 3:
@ConfigurationProperties(prefix = "mail")
public class ConfigProperties {
private String hostName;
private int port;
private String from;
// standard getters and setters
}
@SpringBootApplication
@ConfigurationPropertiesScan("com.baeldung.configurationproperties")
public class EnableConfigurationDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EnableConfigurationDemoApplication.class, args);
}
}
Assuming that the ConfigProperties class is under com.baeldung.configurationproperties, directly scan the classes under the package that contain @ConfigurationProperties annotations
Method 4:
@Configuration
public class ConfigProperties {
@Bean
@ConfigurationProperties(prefix = "item")
public Item item() {
return new Item();
}
}
Way five:
@ConfigurationProperties(prefix = "mail.credentials")
@ConstructorBinding
public class ConfigProperties {
private String hostName;
private int port;
private String from;
public ConfigProperties(String hostName, int port, String from) {
this.hostName = hostName;
this.port = port;
this.from = from;
}
// standard getters and setters
}
Through the above constructor parameter binding, we also need to explicitly enable our configuration class using @EnableConfigurationProperties or @ConfigurationPropertiesScan