1. <tfoot id='uhnry'></tfoot>
  2. <legend id='uhnry'><style id='uhnry'><dir id='uhnry'><q id='uhnry'></q></dir></style></legend>

    <small id='uhnry'></small><noframes id='uhnry'>

    <i id='uhnry'><tr id='uhnry'><dt id='uhnry'><q id='uhnry'><span id='uhnry'><b id='uhnry'><form id='uhnry'><ins id='uhnry'></ins><ul id='uhnry'></ul><sub id='uhnry'></sub></form><legend id='uhnry'></legend><bdo id='uhnry'><pre id='uhnry'><center id='uhnry'></center></pre></bdo></b><th id='uhnry'></th></span></q></dt></tr></i><div id='uhnry'><tfoot id='uhnry'></tfoot><dl id='uhnry'><fieldset id='uhnry'></fieldset></dl></div>
    • <bdo id='uhnry'></bdo><ul id='uhnry'></ul>

      了解 Spring Data JPA @NoRepositoryBean 接口

      时间:2024-08-24

          <tbody id='A5elM'></tbody>

        <small id='A5elM'></small><noframes id='A5elM'>

          • <bdo id='A5elM'></bdo><ul id='A5elM'></ul>
            <i id='A5elM'><tr id='A5elM'><dt id='A5elM'><q id='A5elM'><span id='A5elM'><b id='A5elM'><form id='A5elM'><ins id='A5elM'></ins><ul id='A5elM'></ul><sub id='A5elM'></sub></form><legend id='A5elM'></legend><bdo id='A5elM'><pre id='A5elM'><center id='A5elM'></center></pre></bdo></b><th id='A5elM'></th></span></q></dt></tr></i><div id='A5elM'><tfoot id='A5elM'></tfoot><dl id='A5elM'><fieldset id='A5elM'></fieldset></dl></div>
          • <tfoot id='A5elM'></tfoot>
              <legend id='A5elM'><style id='A5elM'><dir id='A5elM'><q id='A5elM'></q></dir></style></legend>

              1. 本文介绍了了解 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.

                这种情况也是CrudRepositoryPagingAndSortingRepository也带有这个注解的原因.如果包扫描偶然发现了这些(因为您不小心以这种方式配置了它),则引导程序将失败.

                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.

                这篇关于了解 Spring Data JPA @NoRepositoryBean 接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:FetchMode join 对 Spring JPA 存储库中的 ManyToMany 关系没有影响 下一篇:Spring Data JPA findBy/findAllBy 的区别

                相关文章

              2. <tfoot id='3l7QY'></tfoot>
              3. <small id='3l7QY'></small><noframes id='3l7QY'>

                  <i id='3l7QY'><tr id='3l7QY'><dt id='3l7QY'><q id='3l7QY'><span id='3l7QY'><b id='3l7QY'><form id='3l7QY'><ins id='3l7QY'></ins><ul id='3l7QY'></ul><sub id='3l7QY'></sub></form><legend id='3l7QY'></legend><bdo id='3l7QY'><pre id='3l7QY'><center id='3l7QY'></center></pre></bdo></b><th id='3l7QY'></th></span></q></dt></tr></i><div id='3l7QY'><tfoot id='3l7QY'></tfoot><dl id='3l7QY'><fieldset id='3l7QY'></fieldset></dl></div>
                  <legend id='3l7QY'><style id='3l7QY'><dir id='3l7QY'><q id='3l7QY'></q></dir></style></legend>

                    <bdo id='3l7QY'></bdo><ul id='3l7QY'></ul>