Sumit Ghosh
Sumit Ghosh

Reputation: 514

Repository annotation is not working on Spring data JPA

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?

Upvotes: 3

Views: 13627

Answers (3)

Alessandro Argentieri
Alessandro Argentieri

Reputation: 3215

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.

Upvotes: 2

NiVeR
NiVeR

Reputation: 9786

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

Upvotes: 0

Almustafa Azhari
Almustafa Azhari

Reputation: 860

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

Upvotes: 8

Related Questions