ConfigurationProperties
时间: 2023-10-12 19:11:49 浏览: 29
@ConfigurationProperties is an annotation in Spring Boot that is used to bind the properties defined in the application configuration files to the fields of a Java class. This annotation can be used on a class, a nested class, or a field to specify the prefix of the properties to bind.
For example, consider an application that has the following properties defined in the application.yml file:
```
myapp:
name: MyApp
version: 1.0.0
```
To bind these properties to a Java class, we can define a class as follows:
```
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {
private String name;
private String version;
// getters and setters
}
```
With this configuration, the properties defined in the application.yml file will be bound to the corresponding fields in the MyAppProperties class. The prefix "myapp" specifies the prefix of the properties to bind.
We can then use the MyAppProperties class in our application to access the properties:
```
@Service
public class MyService {
@Autowired
private MyAppProperties appProperties;
public String getAppName() {
return appProperties.getName();
}
public String getAppVersion() {
return appProperties.getVersion();
}
}
```
By using the @ConfigurationProperties annotation, we can easily bind the properties defined in the application configuration files to a Java class and use them in our application.
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)