Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

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

 
Tony Evans
Ranch Hand
Posts: 632
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1880
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?




http://www.javabeat.net/2013/03/spring-tutorials/
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Krishna Srinivasan
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only get the error if i implement


The offending code is
 
Tony Evans
Ranch Hand
Posts: 632
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!