Search code examples
javaspringspring-boot

How to fix this error: Could not autowire. There is more than one bean of 'UserBusService' type


I am learning from other people's projects and following along with the code. After I finished writing the relevant code for a certain chapter, the following error occurred.

Could not autowire. There is more than one bean of 'UserBusService' type.
Beans:
userBusService   (UserBusService.java) 
userBusServiceImpl   (UserBusServiceImpl.java)

The specific corresponding gitHub submission is The above error occurred after my latest (fourth) code submission.

The project code I ran for study was normal. I checked for a long time but couldn't find the problem. Some information said to replace @Autowired with @Resource, or use @Qualifier, but this was not used in the reference code.

I would like to ask what went wrong. Looking forward to your reply


Solution

  • I suspect your @MapperScan annotation on CommonApp. According to the MyBatis documentation, it creates a bean for every interface it finds in your package (the emphasis in the following is mine:)

    <mybatis:scan/> supports filtering the mappers created by either specifying a marker interface or an annotation. The annotation property specifies an annotation to search for. The marker-interface attribute specifies a parent interface to search for. If both properties are specified, mappers are added for interfaces that match either criteria. By default, these two properties are null, so all interfaces in the given base package(s) will be loaded as mappers.

    I suggest you try replacing @MapperScan("org.example") with @MapperScan("org.example.common.domain.service.user.mapper") as that's where your mapper lives.