Charbel
Charbel

Reputation: 14697

Spring JPA not implementing/autowiring repository despite @EnableJpaRepositories annotation

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. I declared my repository interfaces, with the proper Spring JPA annotations:

    @RepositoryRestResource(collectionResourceRel = "exchange", path = "exchanges")
    public interface ControlRepository extends PagingAndSortingRepository<Control, Long> { 
    }
    
  2. 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();
        }
    
  3. 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;
    

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)

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)

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.

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: enter image description here any ideas?

Upvotes: 1

Views: 2203

Answers (1)

so-random-dude
so-random-dude

Reputation: 16565

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"})

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

Edit 1

replace @SpringBootApplication with

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

Edit2

Use

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

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

Upvotes: 1

Related Questions