Fah*_*nej 4 java spring-data-jpa spring-boot
我正在从事 Spring Boot 项目。我的项目中有存储库文件,但它会在存储库类中显示一条警告消息Unnecessary @Repository。我正在JpaRepository<>用我的存储库扩展 a 。我的 Spring 版本是4,JDK 版本是17。
这是我的依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是我的存储库
@Repository // Here I get a warning to remove this annotation becasue its unnecessary
public interface CollegeRepo extends JpaRepository<College, Integer>{
}
Run Code Online (Sandbox Code Playgroud)
你正在扩展JpaRepository<T, ID>接口,这意味着spring boot必须为你自动配置这个repository bean,即它将配置一个SimpleJpaRepository<T, ID>.
简而言之,我们不只是使用@Repository或@Component注释创建 bean,我们扩展spring-data接口,然后我们的存储库 bean 将被自动配置。
@Repository您想要提供自己的实现,了解如何访问数据层以及应该做什么。在这种情况下,用 标记你的实现类@Repository将允许你让这个类由 spring 管理,这样你就可以自动装配必要的字段来访问数据层,例如EntityManager,JdbcTemplate...等。虽然Component,@Repository在最基本的层面上只是注册 spring bean,但有一些轻微的增强,@Repository这可能使得它有必要使用,并且在当前情况下也是最佳实践。
根据doc1
当与 PersistenceExceptionTranslationPostProcessor 结合使用时,带有这样注释的类
@Repository可以进行 Spring DataAccessException 转换。
和文档2
持久异常翻译后处理器
Bean 后处理器,自动将持久性异常转换应用到任何使用 Spring 的 @Repository 注解标记的 Bean,将相应的 PersistenceExceptionTranslationAdvisor 添加到公开的代理中
上述案例与 一起使用的示例@Repository。
@Repository
public class CustomCarRepositoryImpl implements CustomCarRepository {
@PersistenceContext
private EntityManager entityManager;
@Override
public List<CarEntity> findCarsWithSpeed(Integer speed) {
return entityManager.createQuery("Query to execute")
.setMaxResults(50).getResultList();
}
}
public interface CustomCarRepository {
List<CarEntity> findCarsWithSpeed(Integer speed);
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在其他组件中自动装配CustomCarRepository并访问已实现的数据层。
@RepositoryRepository当您刚刚声明您的接口并从from的任何 Spring 子接口扩展时org.springframework.data.repository。
例子
public interface CarRepository extends JpaRepository<CarEntity, Long> {
List<CarEntity> findCarsWithSpeed(Integer speed);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下Spring Boot,将能够自动从auto configuration.
唯一需要采取的进一步行动是,如果您自己的接口扩展不存在于您的或所在Repository的同一包或子包中,那么您将需要@Configuration@SpringBootApplication
@EnableJpaRepositories(basePackages = {"base-package-where-repositories-exist"})@AutoConfigurationPackage(basePackages = {"base-package-where-repositories-exist"})为了帮助 spring boot 识别它应该查找该存储库的自动配置的包。(后者@AutoConfigurationPackage会影响存储库和自动配置所需的其他内容,例如实体扫描等。因此,在项目中应谨慎使用它,而不仅仅是存储库。)
| 归档时间: |
|
| 查看次数: |
1757 次 |
| 最近记录: |