• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tony Evans
Ranch Hand
Posts: 598
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: 1876
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: 1876
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: 598
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: 598
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: 598
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic