網站首頁 編程語言 正文
文章目錄
- @EntityScan
- @EnableJpaRepositories
在Springboot
應用開發中使用JPA
時,通常在主應用程序所在包或者其子包的某個位置定義我們的Entity
和Repository
,這樣基于Springboot
的自動配置,無需額外配置,我們定義的Entity
和Repository
即可被發現和使用。但有時候我們需要定義Entity
和Repository
不在應用程序所在包及其子包,那么這時候就需要使用@EntityScan
和@EnableJpaRepositories
了。
上面提到的Entity
和Repository
指的是通過類似下面的方式定義的Entity
和Repository
:
@Entity
@Table(name = "grade")
public class Grade {
// 省略具體內容
}
1234512345
@Repository
public interface GradeRepository extends JpaRepository<Grade, Long>, JpaSpecificationExecutor<Grade> {
// 省略具體內容
}
12341234
@EntityScan
@EntityScan
用來掃描和發現指定包及其子包中的Entity
定義。其用法如下:
@EntityScan(basePackages = {"com.department.entities","come.employee.entities"})
11
如果多處使用@EntityScan
,它們的basePackages
集合能覆蓋所有被Repository
使用的Entity
即可,集合有交集也沒有關系。但是如果不能覆蓋被Repository
使用的Entity
,應用程序啟動是會出錯,比如:
Not a managed type: com.customer.entities.Customer
@EnableJpaRepositories
@EnableJpaRepositories用來掃描和發現指定包及其子包中的Repository
定義。其用法如下:
@EnableJpaRepositories(basePackages = {"com.department.repositories","come.employee.repositories"})
11
如果多處使用@EnableJpaRepositories
,它們的basePackages
集合不能有交集,并且要能覆蓋所有需要的Repository
定義。
如果有交集,相應的Repository
會被嘗試反復注冊,從而遇到如下錯誤:
The bean ‘OrderRepository’, defined in xxx, could not be registered. A bean with that name has already been defined in xxx and overriding is disabled.
如果不能覆蓋所有需要的Repository
定義,會遇到啟動錯誤:
Parameter 0 of method setCustomerRepository in com.service.CustomerService required a bean of type ‘come.repo.OrderRepository’ that could not be found.
原文鏈接:https://blog.csdn.net/qq_43985303/article/details/135267421
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-06-22 C語言關于include順序不同導致編譯結果不同的問題_C 語言
- 2022-08-30 python中Requests請求的安裝與常見用法_python
- 2022-05-31 詳解python學習筆記之解釋器_python
- 2022-06-12 Python取讀csv文件做dbscan分析_python
- 2022-12-22 C語言如何使用函數求素數和舉例_C 語言
- 2022-02-23 fatal error: mpi.h: No such file or directory // f
- 2022-07-26 pyspark 中dataframe 按指定字段拆分為多列或者多行
- 2022-06-09 ASP.NET?Core使用EF創建模型(索引、備用鍵、繼承、支持字段)_實用技巧
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支