File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes @Autowired cant find repository bean identified with @Repository Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "@Autowired cant find repository bean identified with @Repository" Watch "@Autowired cant find repository bean identified with @Repository" New topic
Author

@Autowired cant find repository bean identified with @Repository

Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 579
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

Joined: Jun 29, 2002
Posts: 579
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

Joined: Jun 29, 2002
Posts: 579
Thanks for any help.
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

HI,

Why you have these lines two times in your configuration?



Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey Krishna,

Do you think, that duplication of these two lines might be the reason of this error ?

Regards
Salil Verma
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

Not sure if that would cause the problems. But, why he added two times?
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

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

Joined: Jul 28, 2003
Posts: 1844

Yes. This should be annotated on implementation classes.
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 579
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

Joined: Jun 29, 2002
Posts: 579
Thanks for all those who posted
 
 
subject: @Autowired cant find repository bean identified with @Repository