org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryController': Unsatisfied dependency expressed through field 'categoryService'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.ssm.blog.service.CategoryService' available: expected single matching bean but found 2: AAService,categoryServiceImpl
注入失败原因是NoUniqueBeanDefinitionException定义的bean不唯一
@Controller
@RequestMapping("/category.html")
public class CategoryController {
@Autowired
private CategoryService categoryService;
controller中通过@Autowared注入的CategoryService,而现在我的CategoryService有两个实现类,AAService和CategoryServiceImpl。@Autowared默认按类型注入,现在这个类型有多个实现类,所以就不知道注入哪个了。
解决办法是:如果是AAService写错了,它不该实现CategoryService接口,那就改一下AAService。如果一个接口需要有多个实现类,就换成@Resource按名称注入指明调用哪个实现类
@Controller
@RequestMapping("/category.html")
public class CategoryController {
@Resource(name="categoryServiceImpl")
private CategoryService categoryService;