尽管有@EnableJpaRepositories批注,Spring JPA仍未实现/自动装配存储库 [英] Spring JPA not implementing/autowiring repository despite @EnableJpaRepositories annotation

查看:384
本文介绍了尽管有@EnableJpaRepositories批注,Spring JPA仍未实现/自动装配存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启动应用程序时出现异常,Spring抱怨UnsatisfiedDependencyException:

I'm getting an exception when I start my application, where Spring complain about UnsatisfiedDependencyException:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationConfig': Unsatisfied dependency expressed through field 'controlRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [com.oak.api.finance.repository.ControlRepository]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)

我的应用程序以以下格式组织:

My application is organized in this format:

  1. 我用正确的Spring JPA注释声明了存储库接口:

  1. I declared my repository interfaces, with the proper Spring JPA annotations:

@RepositoryRestResource(collectionResourceRel = "exchange", path = "exchanges")
public interface ControlRepository extends PagingAndSortingRepository<Control, Long> { 
}

  • 我注释了包含main方法的EntryPoint

  • I annotated the EntryPoint class that contains the main method

    @SpringBootApplication
    @EntityScan(basePackages = {"com.oak.api.finance.model.dto"})
    @EnableJpaRepositories(basePackages = {"com.oak.api.finance.repository"})
    public class EntryPoint {
        public static void main(String[] args) {
            Logger logger = LogManager.getLogger(EntryPoint.class);
            logger.info("Starting application");
    
            ApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class);
    //      SpringApplication.run(EntryPoint.class, args);
    
            ctx.getBean(ApplicationServer.class).start();
        }
    

  • 我使用@Autowired将存储库注入到我的spring config(基于Java)ApplicationConfig类中:

  • I used @Autowired to inject my repository into my spring config (java based) ApplicationConfig class:

    @Autowired 
    private ControlRepository controlRepository;
    @Autowired
    private CompanyRepository companyRepository;
    @Autowired
    private SectorRepository sectorRepository;
    

  • 基本上,我想控制对Spring的依赖关系并将其限制为几个包(存储库,java配置和程序入口点-EntryPoint)

    Essentially I want to control the dependency on Spring and limit it to a couple of packages, (the repositories, the java config, and the program entry point - EntryPoint)

    我假设,通过在存储库所在的包中指定@EnableJpaRepositories,spring将为我的存储库创建一个代理,并实例化该存储库的一个实例,并在我调用时进行实例化:

    I assumed that, by specifying @EnableJpaRepositories with the package where my repositories are located, spring would create a proxy for my repository and instantiate an instance of that, and that by the time I call :

        ApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class)
    

    存储库实例将存在于bean poole中,并且可以将它们自动连接到我的ApplicationConfig上下文中,然后将它们注入到我的控制器中.

    The repositories instances would be present in the beans poole and would be possible to autowire them into my ApplicationConfig context, and then inject them into my controller.

    这显然没有发生,Spring抱怨缺少自动装配的Bean,但是我不确定我缺少什么.

    This is clearly not happening, and Spring is complaining about the missing Bean to autowire, but I'm not sure what am I missing.

    下面是我的包裹的快照: 有任何想法吗?

    Below a snapshot of my packages: any ideas?

    推荐答案

    我的猜测是您的存储库没有被扫描,因此没有创建bean.您可以尝试删除这2个注释

    My guess is your repositories are not being scanned, so as a result beans are not getting created. Can you try removing these 2 annotations

    @EntityScan(basePackages = {"com.oak.api.finance.model.dto"})
    @EnableJpaRepositories(basePackages = {"com.oak.api.finance.repository"})
    

    并仅保留@SpringBootApplication.如果这不起作用,则可能需要检查程序包的结构(如果可能,请在此处粘贴屏幕截图)

    And keep only @SpringBootApplication. If this is not working, you might need to check the package structure (if possible paste a screenshot here)

    @Configuration
    @EnableAutoConfiguration
    @ComponentScan("com.oak")
    

    Edit2

    使用

     new SpringApplicationBuilder() 
            .sources(SpringBootApp.class) 
            .web(false) 
            .run(args); 
    

    或者按照mh-dev建议将ComponentScan路径更改为"com.oak"后使用CommandLineRunner

    Or use CommandLineRunner after changing ComponentScan path to "com.oak" as mh-dev suggested

    这篇关于尽管有@EnableJpaRepositories批注,Spring JPA仍未实现/自动装配存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆