简体   繁体   English

Spring引导框架@Repository最佳实践

[英]Best Practises of @Repository in Spring boot framework

I have a Spring boot application which has a UserRepository but I have not annotated that repository with @Repository but still my application works fine.我有一个 Spring 启动应用程序,它有一个 UserRepository 但我没有用 @Repository 注释该存储库,但我的应用程序仍然可以正常工作。

Why @Repository annotation is optional in Spring boot and what is the best practise that should we annotate it or not in repository classes.为什么 @Repository 注释在 Spring 引导中是可选的,我们应该在存储库类中注释它的最佳实践是什么。

It is working because spring will scan the class path and identify given class is repository (DAO) based on the imports, when use @Repositary spring context know how to handle expections like re-throw to pre defined methods..etc, and also this annotation help readability of the code.它之所以有效,是因为 spring 将扫描 class 路径并识别给定的 class 是基于导入的存储库 (DAO),当使用 @Repositary spring 上下文时,它知道如何处理诸如重新抛出到预定义方法等的期望,还有这个注释有助于代码的可读性。

For @Repository annotation even if you haven't mentioned Spring recognizes the repositories by the fact that they are extending Repository interfaces like JPARepository or CrudRepository.对于@Repository注释,即使您没有提到 Spring 也可以通过它们扩展 Repository 接口(如 JPARepository 或 CrudRepository)来识别存储库。

So, it's not mandatory to mention annotation, you can check in your code whether you have mentioned @EnableJpaRepositories("packages") above Main class or not.所以,不是必须要提到注释,你可以检查你的代码是否在 Main class 上面提到@EnableJpaRepositories("packages") That might also be one of the reasons why it is working.这也可能是它起作用的原因之一。

As per best practices for annotations, they have a purpose to fulfill, @Repository is important from a database connection perspective, where it has lots of proper exceptions throw, or pre-defined methods.根据注释的最佳实践,它们有一个实现的目的,@Repository 从数据库连接的角度来看很重要,它有很多适当的异常抛出或预定义的方法。

If you do not use the proper annotations, you may face commit exceptions overridden by rollback transactions.如果您不使用正确的注释,您可能会面临被回滚事务覆盖的提交异常。 You will see exceptions during the stress load test that is related to roll-back JDBC transactions.在与回滚 JDBC 事务相关的压力负载测试期间,您将看到异常。

For more clarity have a look at this post为了更清楚,请查看这篇文章

I assume you have somewhere implicitly or explicitly a EnableJpaRepositories annotation which scans all Spring repositories and your repositories are extending CrudRepository or other Spring Data base classes.我假设您在某个地方隐式或显式地扫描所有 Spring 存储库的EnableJpaRepositories注释,并且您的存储库正在扩展CrudRepository或其他 Spring 数据库基类。

The annotation is only needed when defining you own repositories without using Spring Data mechanism.只有在不使用 Spring 数据机制的情况下定义您自己的存储库时才需要注释。 Note, that Repository is just a special Component , latter should also do it to have DI enabled, so this is more for better recognizing the purpose of the class.请注意, Repository只是一个特殊的Component ,后者也应该这样做才能启用 DI,因此这更多是为了更好地识别 class 的用途。

@Repository is used to indicate interface that extends a jpa repository, for example.例如,@Repository 用于指示扩展 jpa 存储库的接口。 Notice in the example below, the interface extends the Beer class and the BeerQueries query interface.请注意在下面的示例中,该接口扩展了 Beer class 和 BeerQueries 查询接口。

package com.algaworks.brewer.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.algaworks.brewer.model.Beer;
import com.algaworks.brewer.repository.helper.beer.BeersQueries;

@Repository
public interface Beers extends JpaRepository<Beer, Long>, BeersQueries {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM