There is already bean method mapped

mapping, spring, spring-mvc

Solution

Your `<mvc:annotation-driven/>` tag tells spring to go through your classes looking for annotations and create instances of the annotated classes. Your `<bean id="sdtm-controller .../>` also creates an instance of your controller. So the second one created is giving an error because one has already been created. As M. Deinum indicated, you only need one of them, and since you are using annotations, simply remove the bean configuration.

Problem

I am getting the following exception: ``` </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. ``` My controller class only has a single method called getVersions(). My application was working fine when I was using AutoWired on my Controller and did not explicit wire together any beans. But then I switched to defining some beans so that I could hook into beans written by others on the project. I started getting duplicate beans exceptions, so I commented out the @Autowired annotation. Why am I getting this. Are there remnants of the autowiring still in my deployed environment??? ``` @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(); } ``` My web.xml includes the following: ``` <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> ``` I have a file named sdtm-servlet.xml that includes the following: ``` <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> ``` Lastly, I have a services-config.xml file that defines a service. ``` <bean id="sdtmService" class="com.sas.hls.clc.clinicalstandards.sdtm.SDTM_ServiceImpl" > <property name="clinicalStandardsInterface" ref="clinicalStandardsInterface" /> </bean> ```

Original source

Related problems