This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Spring and the fly likes No unique bean of type is defined: expected single matching bean but found 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "No unique bean of type is defined: expected single matching bean but found 2" Watch "No unique bean of type is defined: expected single matching bean but found 2" New topic
Author

No unique bean of type is defined: expected single matching bean but found 2

Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 573
I understand how this problem can occur but in this case I have not made any of the classic mistakes

The error is expected single matching bean but found 2: [fundRepositoryImpl, fundRepository] In this case I just have a single bean fundRepositoryImpl implementing a single interface



This implements the FundRepository which is labled with the @Repository


which implements DomainRepository




I have not id any of the beans in the spring context.



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?




http://www.javabeat.net/2013/03/spring-tutorials/
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Tony,

"No unique bean of type is defined: expected single matching bean but found 2" error normally appear at the time of wiring. As you are using component scan, I assume there should be more code where the error might be appearing during wiring.

If above is not the case, Can you please provide the error stack trace. That would help us in identifying the root cause of the problem?

As suggested by Krishna, please remove duplicate entry from you xml file.

Regards
Salil Verma

Regards
Salil Verma
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

As salil mentioned, some time when spring trying to wire the bean two times because it is declared twice, there is chance of getting this error.
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 573
Have removed the duplicate entries, also in the code made the follwoing changes

Removed @Repository from the interface


Added it to the class


Still getting the error


Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 573
I only get the error if i implement


The offending code is
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 573
Ok worked it out, as I am using JpaRepository I do not need to create a concrete implemention class its using the bean FundRepository as the repository type.
By adding a concrete repository class FundRepositoryImpl which is defined as a repository @Repository

Spring then saw two repository type beans FundRepositoryImpl and FundRepository .

The solution is to not define a concrete class FundRepositoryImpl if using JpaRepository.
 
 
subject: No unique bean of type is defined: expected single matching bean but found 2
 
Similar Threads
@Autowired cant find repository bean identified with @Repository
Spring3/JPA2 configuration problem
org.springframework.beans.factory.BeanCreationException
Why is deleting an entity that doesn't exist succeeding?
XML name space error in Spring data JPA hibernate applicationContext.xml