Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Autowired cant find repository bean identified with @Repository

 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you cant find the bug at least say yes or no that i have carried out all the correct steps, that way at least I a can think that there is another error in my context xml file


I have a Junit test which cant load the a repository bean. 1) I created a interface FundRepository


The interface has the @Repository

2) I created the FundRepositoryImpl which implements my interface




Created the spring data context

in this context i added the following



According to everything i have read this should be enough to allow me to @Autowired in FundRepository.

The full context is as follows which may be where the bug is

 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error is as follows :

Caused by: org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private net.com.htts.transaction.data.repository.FundRepositoryImpl net.com.htts.transaction.test.data.repository.FundRepositoryTest.repository;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [net.com.htts.transaction.data.repository.FundRepositoryImpl] f
ound for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
... 32 more

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [net.com.htts.transaction.data.repository.FundRepositoryImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:948)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:817)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:731)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
... 34 more
 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for any help.
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

Why you have these lines two times in your configuration?

 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Krishna,

Do you think, that duplication of these two lines might be the reason of this error ?
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if that would cause the problems. But, why he added two times?
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Tony,

@Repository annotation should be used with class implementing the DAO interface not in the interface itself. Please refer the following link for same information -
http://www.techferry.com/articles/spring-annotations.html#Repository


Although duplicate entry should not cause any harm yet I do not see any reason why duplicate entry should be made. Please remove that as well from your code.
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. This should be annotated on implementation classes.
 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed this is how i should have wrote the code

The interface


The repository class


The @Repository should be on the main class not the interface, but still call the interface on the junit.
 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all those who posted
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic