@ConfigurationProperties(prefix = "spring.datasource")是从配置文件取值吗
时间: 2024-01-29 16:23:04 浏览: 30
是的,@ConfigurationProperties(prefix = "spring.datasource") 注解表示将从配置文件中读取以 "spring.datasource" 为前缀的属性值,并将其绑定到对应的属性字段上。这样可以方便地在代码中使用配置文件中的属性值,避免硬编码。在Spring Boot应用中,通常会将配置信息存储在 application.properties 或 application.yml 文件中。
相关问题
@ConfigurationProperties(prefix = "spring.datasource.
@ConfigurationProperties(prefix = "spring.datasource.")是一个注解,用于将配置文件中以"spring.datasource."开头的配置项注入给当前对象的属性。该注解可以加在方法上,也可以加在类上。当加在方法上时,会调用该方法返回的对象的属性的set方法,将配置文件的内容注入到对应的属性中。当加在类上时,会调用该类声明的属性的set方法,将配置文件的内容注入到对应的属性中。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [@Bean及@ConfigurationProperties(prefix = “spring.datasource“)](https://blog.csdn.net/qq_33524771/article/details/119211997)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* [Java 常用注解@Configuration,@Bean及@ConfigurationProperties(prefix = "spring.datasource")](https://blog.csdn.net/qq_34207422/article/details/99640655)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
@ConfigurationProperties(prefix = "spring.datasource")是直接从本地yml获取数据源吗
是的,`@ConfigurationProperties(prefix = "spring.datasource")` 是从本地 yml 文件中获取数据源配置的注解。具体来说,它会将以 `spring.datasource` 为前缀的配置项读取到一个 `DataSourceProperties` 对象中,并将该对象注入到 `DataSourceAutoConfiguration` 中,从而创建一个数据源。
例如,在你的 `application.yml` 文件中,如果有以下的数据源配置项:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
```
那么,使用 `@ConfigurationProperties(prefix = "spring.datasource")` 注解的数据源配置类会自动将这些配置项读取到一个 `DataSourceProperties` 对象中,然后将该对象注入到 `DataSourceAutoConfiguration` 中,从而创建一个数据源。
需要注意的是,如果你想从 Nacos 中获取数据源配置信息,那么就不能使用 `@ConfigurationProperties(prefix = "spring.datasource")` 注解了,需要使用其他的方式来获取 Nacos 中的配置信息,并将其注入到数据源中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)