I have this code for my Spring-based web project:
Controller:
@Controller
@RequestMapping("mycontroller")
public class MyObjectController {
@Autowired
private MyService service;
// Code omitted
}
Service:
@Service
public class MyServiceImpl implements MyService {
@Autowired
@Qualifier("mydao")
private MyDao mydao;
@Autowired
@Qualifier("mydao2")
private MyDao2 mydao2;
// Code omitted
}
Context.xml (Spring):
<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" />
However it throws this error:
NoSuchBeanDefinitionException: No unique bean of type [com.mycompany.service.MyService] is defined: expected single matching bean but found 2: [myService, myServiceImpl]
Your bean is defined twice, here (
@Serviceannotation, results in registeringmyServiceImplbean):and here (in
Context.xml, bean withmyServiceid):Either remove the definition from XML or remove the annotation.