【问题标题】:Inject CRUD Repository in Spring在 Spring 中注入 CRUD 存储库
【发布时间】:2019-03-10 19:44:19
【问题描述】:

我无法在 Spring 中注入 CRUD 存储库。

存储库

@Repository
public interface EntityRepository extends CrudRepository<entity,Long>{    
}
@Autowired EntityRepository eR

错误: .. 需要一个找不到的 EntityRepository 类型的 Bean

Consider defining a bean of type 'EntityRepository' in your configuration.

我的主要

@SpringBootApplication
@ComponentScan({"de.xyz.*"}) 
@EntityScan("de.xyz.entities")
@EnableJpaRepositories("de.xyz.*") 
//@EnableEurekaClient
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }    
}

【问题讨论】:

    标签: spring spring-data-jpa repository


    【解决方案1】:

    另一种方法是使用 basePackages 字段;这是 ComponentScan 注释中的一个字段。

    @ComponentScan(basePackages = {"de.xyz.repository"})
    public class Application extends SpringBootServletInitializer {
        //  
    }
    

    【讨论】:

    • 我有一个@Configuration 的类来定义应该在哪里以及如何注入哪个类,函数看起来像这样pastebin.com/iXHBuaHr
    【解决方案2】:

    第一步: 尝试将这两个类包含在同一个包中。通过这种方式,您可以缩小组件扫描问题的范围。删除所有其他注释并仅保留@SpringBootApplication

    Note : 
    By default spring loads all the classes under the package of Application Class. 
    

    第 2 步:查看您的依赖项,确认您已包含 JPA 存储库的依赖项。

    Step3 : 贴出代码的GIT Hub链接,方便查看。

    否则添加组件扫描注解内的所有包,如下所示。 @ComponentScan({ "a.b.c", "a.b.c.dao" })

    正如您所提到的,有一个配置类正在创建 bean,请尝试将该类包包含在同一个包中或将其包含在组件扫描中。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-27
      相关资源
      最近更新 更多