我的基于 Spring 的 Web 项目有以下代码:
控制器:
@Controller
@RequestMapping("mycontroller")
public class MyObjectController {
@Autowired
private MyService service;
// Code omitted
}
服务:
@Service
public class MyServiceImpl implements MyService {
@Autowired
@Qualifier("mydao")
private MyDao mydao;
@Autowired
@Qualifier("mydao2")
private MyDao2 mydao2;
// Code omitted
}
Context.xml(春天):
<annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.mycompany" />
<beans:bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<beans:bean id="myService" class="com.mycompany.serviceimpl.MyServiceImpl" />
但是它会引发此错误:
NoSuchBeanDefinitionException:没有定义类型 [com.mycompany.service.MyService] 的唯一 bean:预期单个匹配 bean 但找到 2:[myService,myServiceImpl]