0

My component is defined as below

@Component("myBo")
public class MyBO {

    @Autowired
    JpaRepository<MyData, Long> repository;

Spring Data Interface:

public interface MyDataRepository extends JpaRepository<MyData, Long> {

Entitymanager definition:

    @Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory( EntityManagerFactoryBuilder builder, @Qualifier("dmDs") final DataSource dmDs) {

    LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = builder.dataSource(dmDs).packages(new String[]{"my.packages"}).build();
    return localContainerEntityManagerFactoryBean;

}

and my test fails with this error

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBO': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.jpa.repository.JpaRepository<MyData, java.lang.Long>' available: 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:588)    

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.jpa.repository.JpaRepository<MyData, java.lang.Long>' available: expected at least 1 bean which qualifies as autowire candidate.     

whereas the following works fine. Need help in understanding this behavior.

@Component
public class MyBO {

@Autowired
JpaRepository<MyData, Long> repository;

Thanks

patb23
  • 387
  • 5
  • 21
  • if my question got down-vote for lack of any meaningful details, the down vote without any explanation resulting in the same. – patb23 Jul 13 '17 at 10:03
  • When you say I remove the value for @Component, are you removing "myBo" or the annotation completely? – yaswanth Jul 13 '17 at 10:15
  • MyBO class still contains JpaRepository Autowired. Can you try autowiring MyDataRepository? – yaswanth Jul 13 '17 at 10:17
  • Thanks for helping but it does look fine to me as all my Repos are declared like that and since I included the package for scanning, the interface is being wired. – patb23 Jul 13 '17 at 11:43

1 Answers1

1

You should not autowire JpaRepository<MyData, Long> as is. You should extend it and create your own interface as follows.

public interface MyRepository extends JpaRepository<MyData, Long> {
}

The reason you won't be able to autowire JpaRepository directly is because it is annotated with NoRepositoryBean annotation and that prevents creating an instance of it. It is always recommended to extend the base repo classes and create your own interfaces.

P.S: Do not forget to enable Jpa Repositories on these repo interfaces you are going to create. Otherwise you won't be able to autowire them.

Similar to this in your xml config.

<jpa:repositories base-package="com.acme.repositories"/>
yaswanth
  • 2,349
  • 1
  • 23
  • 33
  • Forgot to add that I have defined an interface, and test runs fine when I remove the 'value' – patb23 Jul 13 '17 at 10:08
  • Can you update your question with the interface code? It would also help if you could post your xml config. Please update the stacktrace as well after running the app with this change. – yaswanth Jul 13 '17 at 10:11