Repository annotation is not working on Spring data JPA

Sum*_*osh 1 java spring spring-data-jpa spring-boot

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> {

}
Run Code Online (Sandbox Code Playgroud)

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;
}
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
 }
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

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?

Al-*_*ari 6

尝试按照@hang321在 Spring Boot Ask 中 Can't Autowire @Repository annotated interface@EnableJpaRepositories("com.adv.dao")上的AdvMain建议添加