required a single bean, but 2 were found

Description:

Field resArticleClassifyService in com.wenwen.blog.controller.admin.AdminArticleController required a single bean, but 2 were found:
- resArticleClassifyServiceImpl: defined in file [D:\learn\life\life-blog\target\classes\com\wenwen\blog\service\impl\ResArticleClassifyServiceImpl.class]
- IResArticleClassifyService: defined in file [D:\learn\life\life-blog\target\classes\com\wenwen\blog\service\IResArticleClassifyService.class]

0、写在前面

编写程序时,SpringBoot项目使用@SpringBootApplication、@MapperScan(“com.wenwen”)、@Controller和@Service注解
运行后报了这个错误
造成原因:
在controller引用serIve

@RestController
public class IndexController {
    
    
    @Autowired
    UserMapper userMapper;

    @Autowired
    IIndexService indexService;
    @ApiOperation(value = "分类列表")
    @GetMapping("/listClassify")
    public ResponseListBase<String> listClassify(@RequestParam("userId") Integer userId){
    
    
        return indexService.listClassify(userId);
    }
}

service
中只使用了@Service,
IndexServiceImpl继承了IIndexService。调用indexServiceImpl中的方法

1、问题描述

引起的原因

2、解决

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

在实现类上标记一个默认注入注解@Primary

@Service
@Primary
public class IndexServiceImpl implements IIndexService {
    
    

猜你喜欢

转载自blog.csdn.net/weixin_42119415/article/details/109668583