Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring发现多个bean定义错误_Java_Spring_Spring Mvc - Fatal编程技术网

Java Spring发现多个bean定义错误

Java Spring发现多个bean定义错误,java,spring,spring-mvc,Java,Spring,Spring Mvc,我的基于Spring的web项目有以下代码: 控制器: @Controller @RequestMapping("mycontroller") public class MyObjectController { @Autowired private MyService service; // Code omitted } @Service public class MyServiceImpl implements MyService { @Autowired

我的基于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
}
<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" /> 
服务:

@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
}
<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" /> 
Context.xml(Spring):

@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
}
<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,myserviceinpl]

您可以将
@Service
放在
MyServiceImpl
上,或者在
Context.xml
中声明bean。不要两个都做,否则你会吃到两颗豆子

从XML文件中删除
myService
bean定义,就可以开始了


此外,您不需要声明
DefaultAnnotationHandlerMapping
AnnotationMethodHandlerAdapter
bean-默认情况下这些bean可用。

您的bean定义了两次,在这里(
@Service
注释导致注册
myServiceImpl
bean):

这里(在
Context.xml
中,bean带有
myService
id):


从XML中删除定义或删除注释