已经映射了bean方法

时间:2014-05-13 15:17:56

标签: spring spring-mvc mapping

我收到以下异常:

</pre></p><p><b>root cause</b> <pre>org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': 
Invocation of init method failed; nested exception is java.lang.IllegalStateException: 
Ambiguous mapping found. Cannot map 'sdtm-controller' bean method 
public java.util.List&lt;java.lang.String&gt; com.sas.hls.clc.clinicalstandards.sdtm.SDTM_Controller.getVersions()
to {[/getVersions],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: 
There is already 'SDTM_Controller' bean method
public java.util.List&lt;java.lang.String&gt; com.sas.hls.clc.clinicalstandards.sdtm.SDTM_Controller.getVersions() mapped.
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
    javax.servlet.GenericServlet.init(GenericServlet.java:212)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:555)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    java.lang.Thread.run(Thread.java:662)
</pre></p><p><b>root cause</b> <pre>java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'sdtm-controller' bean method 
public java.util.List&lt;java.lang.String&gt; com.sas.hls.clc.clinicalstandards.sdtm.SDTM_Controller.getVersions()
to {[/getVersions],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'SDTM_Controller' bean method
public java.util.List&lt;java.lang.String&gt; com.sas.hls.clc.clinicalstandards.sdtm.SDTM_Controller.getVersions() mapped.

我的控制器类只有一个名为getVersions()的方法。当我在我的控制器上使用AutoWired并且没有明确地将任何bean连接在一起时,我的应用程序工作正常。但后来我转而定义了一些bean,以便我可以挂钩其他人在项目中编写的bean。我开始获得重复的bean异常,所以我注释掉了@Autowired注释。为什么我得到这个。自动装配的残余物是否仍在我的部署环境中?

@Controller
public class SDTM_Controller {
    private SDTM_Service service;

//  @Autowired
    public SDTM_Controller(SDTM_Service service){
        this.service = service;
    }

    public SDTM_Controller(){

    }

    public void setSDTM_Service(SDTM_Service service){
        this.service = service;
    }

    // URL: sdtm/getVersions

    @RequestMapping(value="/getVersions", method=RequestMethod.GET)
    public @ResponseBody List<String> getVersions(){
        return service.getVersions();
    }

我的web.xml包含以下内容:

<servlet>
    <servlet-name>sdtm</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/sdtm-servlet.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>sdtm</servlet-name>
    <url-pattern>/sdtm/*</url-pattern>
</servlet-mapping>

我有一个名为sdtm-servlet.xml的文件,其中包含以下内容:

    <context:component-scan base-package="com.sas.hls.clc.clinicalstandards.sdtm" />    
<mvc:annotation-driven/>

    <bean id="sdtm-controller" class="com.sas.hls.clc.clinicalstandards.sdtm.SDTM_Controller" >    
            <property name="SDTM_Service" ref="SDTM_Service" />  
    </bean>

最后,我有一个定义服务的service-config.xml文件。

   <bean id="sdtmService" class="com.sas.hls.clc.clinicalstandards.sdtm.SDTM_ServiceImpl" >
        <property name="clinicalStandardsInterface" ref="clinicalStandardsInterface" />
   </bean>

1 个答案:

答案 0 :(得分:3)

您的<mvc:annotation-driven/>标记告诉spring通过您的类查找注释并创建带注释的类的实例。您的<bean id="sdtm-controller .../>也会创建控制器的实例。所以创建的第二个是给出错误,因为已经创建了一个错误。正如M. Deinum指出的那样,你只需要其中一个,因为你正在使用注释,只需删除bean配置。