V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kingkongs
V2EX  ›  Java

@NoRepositoryBean 注解的作用是什么?

  •  
  •   kingkongs · 2019-08-16 15:30:02 +08:00 · 5842 次点击
    这是一个创建于 1708 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    hantsy
        1
    hantsy  
       2019-08-17 00:01:05 +08:00   ❤️ 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;
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2673 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 02:01 · PVG 10:01 · LAX 19:01 · JFK 22:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.