3

I am building a Spring-boot application where I am using Spring data jpa feature.

Please find below my dao layer code

package com.adv.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CustomerDao extends JpaRepository<Customer, String> {

}

I am using a DaoProvider class as follows:

package com.adv.dao;

import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DaoProvider implements Serializable {

private static final long serialVersionUID = 1L; 

@Autowired
private CustomerDao customerDao;

public CustomerDao getCustomerDao() {
    return customerDao;
}
}

My spring boot main class is defined as follows:

@SpringBootApplication
@ComponentScan(basePackages="com.adv")
public class AdvMain extends SpringBootServletInitializer {

   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      return application.sources(AdvMain.class);
   }

    public static void main(String[] args) {
       SpringApplication.run(AdvMain.class, args);
    }
 }

Now during runtime I am getting following exception:

Field customerDao in com.adv.dao.DaoProvider required a bean of type 'com.adv.dao.CustomerDao' that could not be found.

I guess that @Repository annotation on interface CustomerDao is not working.

But I am unable to figure out the issue.Can anyone figure out the problem?

Sumit Ghosh
  • 484
  • 4
  • 13
  • 36
  • 2
    Try to add `@EnableJpaRepositories("com.adv.dao")` on `AdvMain` as suggested on [Can't Autowire @Repository annotated interface in Spring Boot](https://stackoverflow.com/questions/29221645/cant-autowire-repository-annotated-interface-in-spring-boot) – Al-Mustafa Azhari Mar 18 '18 at 10:05
  • ok.thanks.It worked after adding @EnableJpaRepositories("com.adv.dao") – Sumit Ghosh Mar 18 '18 at 11:58

3 Answers3

8

Try to add @EnableJpaRepositories("com.adv.dao") on AdvMain as suggested by @hang321 on Can't Autowire @Repository annotated interface in Spring Boot Ask

Al-Mustafa Azhari
  • 850
  • 1
  • 9
  • 24
1

Remove the annotation @Repository from the dao interface. That annotation must be put only on implented classes.

Be careful also to implement both the empty constructor than the all args constructor of the Customer class.

Alessandro Argentieri
  • 2,901
  • 3
  • 32
  • 62
  • No Alex.We require this Repository annotation at interface level while working with JpaRepository.It is little different than other normal interfaces.Whatever you have pointed out is suitable for all other user defiend interfaces. – Sumit Ghosh Mar 19 '18 at 07:00
  • 1
    I always use without the annotation @Repository and it works perfectly. I know it's not a normal interface, still the real bean is implemented by Spring, so Spring knows it's a repo bean. – Alessandro Argentieri Mar 19 '18 at 08:04
0

Just remove the @ComponentScan annotation altogether.The @SpringBootApplication annotation already includes the component scan as specified here.

NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • ComponentScan is required since I have package segregation for various classes.By deault spring scan only the package in which main class is located.anyways the answer to my problem was use of EnableJpaRepositories. – Sumit Ghosh Mar 19 '18 at 06:57