了解 Spring Data JPA @NoRepositoryBean 接口
在阅读 Spring Data 文档时,我多次遇到 @NoRepositoryBean
接口.
I encountered the @NoRepositoryBean
interface several times whilst reading the Spring Data documentation.
引用文档:
如果您使用自动存储库接口检测,则使用使用接口的 Spring 命名空间原样会导致 Spring试图创建 MyRepository 的实例.这当然不是需要,因为它只是充当存储库和您要为每个实体定义的实际存储库接口.到排除扩展存储库的接口被实例化为存储库实例使用 @NoRepositoryBean
对其进行注释.
If you're using automatic repository interface detection using the Spring namespace using the interface just as is will cause Spring trying to create an instance of MyRepository. This is of course not desired as it just acts as indermediate between Repository and the actual repository interfaces you want to define for each entity. To exclude an interface extending Repository from being instantiated as repository instance annotate it with
@NoRepositoryBean
.
但是,我仍然不确定何时何地使用它.有人可以建议并给我一个具体的用法示例吗?
However, I am still not sure when and where to use it. Can someone please advise and give me a concrete usage example?
推荐答案
注解用于避免为实际匹配 repo 接口标准但不打算成为一个的接口创建存储库代理.仅当您开始使用功能扩展所有存储库时才需要它.举个例子吧:
The annotation is used to avoid creating repository proxies for interfaces that actually match the criteria of a repo interface but are not intended to be one. It's only required once you start going into extending all repositories with functionality. Let me give you an example:
假设您想将方法 foo() 添加到所有存储库.您可以从添加这样的 repo 接口开始
Assume you'd like to add a method foo() to all of your repositories. You would start by adding a repo interface like this
public interface com.foobar.MyBaseInterface<…,…> extends CrudRepository<…,…> {
void foo();
}
您还可以添加相应的实现类、工厂等.现在,您的具体存储库接口将扩展该中间接口:
You would also add the according implementation class, factory and so on. You concrete repository interfaces would now extend that intermediate interface:
public interface com.foobar.CustomerRepository extends MyBaseInterface<Customer, Long> {
}
现在假设你引导 - 比如说 Spring Data JPA - 如下:
Now assume you bootstrap - let's say Spring Data JPA - as follows:
<jpa:repositories base-package="com.foobar" />
你使用 com.foobar
因为你在同一个包中有 CustomerRepository
.Spring Data 基础设施现在无法判断 MyBaseRepository
不是具体的存储库接口,而是充当中间存储库以公开附加方法.所以它会尝试为它创建一个存储库代理实例并失败.您现在可以使用 @NoRepositoryBean
来注释这个中间接口,本质上告诉 Spring Data:不要为这个接口创建存储库代理 bean.
You use com.foobar
because you have CustomerRepository
in the same package. The Spring Data infrastructure now has no way to tell that the MyBaseRepository
is not a concrete repository interface but rather acts as intermediate repo to expose the additional method. So it would try to create a repository proxy instance for it and fail. You can now use @NoRepositoryBean
to annotate this intermediate interface to essentially tell Spring Data: don't create a repository proxy bean for this interface.
这种情况也是CrudRepository
和PagingAndSortingRepository
也带有这个注解的原因.如果包扫描偶然发现了这些(因为您不小心以这种方式配置了它),则引导程序将失败.
That scenario is also the reason why CrudRepository
and PagingAndSortingRepository
carry this annotation as well. If the package scanning picked those up by accident (because you've accidentally configured it this way) the bootstrap would fail.
长话短说:使用注释来防止存储库接口被选为候选最终成为存储库 bean 实例.
Long story short: use the annotation to prevent repository interfaces from being picked up as candidates to end up as repository bean instances eventually.
相关文章