Repository annotation is not working on Spring data JPA
Solution 1
Try to add @EnableJpaRepositories("com.adv.dao")
on AdvMain
as suggested by @hang321 on Can't Autowire @Repository annotated interface in Spring Boot
Ask
Solution 2
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.
Sumit Ghosh
Updated on July 24, 2022Comments
-
Sumit Ghosh almost 2 years
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 interfaceCustomerDao
is not working.But I am unable to figure out the issue.Can anyone figure out the problem?
-
Al-Mustafa Azhari over 6 yearsTry to add
@EnableJpaRepositories("com.adv.dao")
onAdvMain
as suggested on Can't Autowire @Repository annotated interface in Spring Boot -
Sumit Ghosh over 6 yearsok.thanks.It worked after adding @EnableJpaRepositories("com.adv.dao")
-
-
Sumit Ghosh over 6 yearsComponentScan 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 over 6 yearsNo 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.
-
Alessandro Argentieri over 6 yearsI 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.