具有@RequestMapping的相同bean的ApplicationContext失败

时间:2015-05-20 10:10:55

标签: spring spring-mvc

在我的applicationContext.xml中,我有2个具有相同类和不同id的bean(test和test1)。正确加载应用程序上下文,但是当我将@RequestMapping添加到一个方法时,bean创建失败并出现以下错误。这曾用于AnnotationMethodHandlerAdapter但与RequestMappingHandlerMappingRequestMappingHandlerAdapter失败。

错误:

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 'test1' bean method 
public java.lang.String com.test.render()
to {[/render],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'test' bean method

请建议如何解决此问题。

代码:

的applicationContext.xml

 <bean id="test" class="com.abc.test" />

 <bean id="test1" class="com.abc.test" />

控制器

@Controller
 @RequestMapping( value ={"/test/", "/test1/"})
public class test {

    @RequestMapping("render")
    public String render ()
    {
        //some code here.
    }
}

2 个答案:

答案 0 :(得分:0)

你可以这样做...... 从单例方法切换到原型和xml内部

以编程方式定义

    class P1 {
       @Autowire
       NotSoSingleton prototypedBean;
    }

    class P2 {
       @Autowire
       NotSoSingleton prototypedBean;
    }

    class P3 {
       @Autowire
       NotSoSingleton prototypedBean;
    }

这种做法会吗?

答案 1 :(得分:0)

我可以找到解决这个问题的方法。这个解决方案,如果我在5月21日发布的评论

我使用order属性在xml文件中实例化自定义的RequestMappingHandlerMapping并且它工作了!! 我的自定义RequestMappingHandlerMapping取代了<annotation-driven>加载的默认值。

相关问题