ConfigurationProperties
时间: 2023-10-12 11:13:48 浏览: 59
@ConfigurationProperties is a Spring Framework annotation that is used to bind external properties or configuration files to a Java object. It allows developers to externalize configuration and change the configuration without modifying the code.
@ConfigurationProperties is used to create a Java bean that holds the properties specified in an external configuration file. The values of the properties are set at runtime using the Spring Environment and PropertyResolver.
To use @ConfigurationProperties, you need to define the properties in an external file, such as application.properties or application.yml. The properties can be defined with a prefix, which is used to group related properties together. For example, if you have properties for a database connection, you can group them under a prefix such as "spring.datasource".
Here is an example of using @ConfigurationProperties in Spring Boot:
```
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties {
private String url;
private String username;
private String password;
// getters and setters
}
```
In this example, the DataSourceProperties class is annotated with @ConfigurationProperties and has a prefix of "spring.datasource". The properties "url", "username", and "password" are defined in the external configuration file with the prefix "spring.datasource".
When the Spring application starts up, the values of these properties are set automatically by Spring using the PropertyResolver. This allows developers to externalize the configuration of their application and change it without modifying the code.
阅读全文