kingkongs
V2EX  ›  Java

@NoRepositoryBean 注解的作用是什么?

  •  
  •   kingkongs · Aug 16, 2019 · 6759 views
    This topic created in 2484 days ago, the information mentioned may be changed or developed.

    刚接触 JPA,发现 CrudRepository 接口使用了 @NoRepositoryBean 这个注解,并且继承了 CrudRepository 接口的接口不需要添加 @Repository 注解也能够注入成功

    hantsy
        1
    hantsy  
       Aug 17, 2019   ❤️ 2
    任何 extends JpaRepository 或者 CrudRepostory 等接口,如 UserRepository 在 Spring Boot 启动时,Spring Data 会创建一个 Bean。但是 JpaRepository, CrudRepository 自己需要过滤掉(用 @NoRepositoryBean 标记)。

    interface UserRepository extends JpaRepository{}
    interface PostRepository extends JpaRepository{}

    在其它 Spring Component 可以 Inject, 如添加 @Service, @Controller,或者 @Component Annoation 的类中。
    @Inject UserRepository userRepo;
    @Inject PostRepository userRepo;
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5581 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 06:32 · PVG 14:32 · LAX 23:32 · JFK 02:32
    ♥ Do have faith in what you're doing.