網站首頁 編程語言 正文
文章目錄
- @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-11-16 詳解Python中range()與xrange()的區別_python
- 2022-03-28 c語言經典習題之逆序字符串詳解_C 語言
- 2022-01-11 for循環調用接口返回的數據放在同一個列表中
- 2022-11-23 Android移除Message的方法分享_Android
- 2022-11-28 Android開發框架MVC-MVP-MVVM-MVI的演變Demo_Android
- 2022-03-06 C#多種操作excel的方法比較_C#教程
- 2022-04-20 Android實現環信修改頭像和昵稱_Android
- 2022-07-16 淺談常見的加密算法
- 欄目分類
-
- 最近更新
-
- 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同步修改后的遠程分支