SpringMVC conflicts with existing, non-compatible bean definition of same name and class 的解决办法,sprin

来源:互联网 发布:武汉淘宝美工培训学校 编辑:程序博客网 时间:2024/05/29 04:51

http://www.bkjia.com/Javabc/1007408.html


SpringMVC conflicts with existing, non-compatible bean definition of same name and class 的解决办法,springmvc获取bean


问题起因

最近,项目组的里的同事遇到一个问题,他自己负责的模块,SpringMVC的Controller与其他模块的Controller 类名重名了,导致整个工程都起不来了。

后台报的错误是这样的:

××Controller' for bean class [××ontroller] conflicts with existing, non-compatible bean definition of same name and class 

午饭时,他一直和我抱怨这个问题,还说找不到办法。

后面我想了一下,SpringMVC的Controller 应该是采用类似键值对(key/value)的映射方式处理的。而当中的键,默认是用cotroller的类名(非全类名)作为键。这样,如果不同包下面的两个Contoller 重名的话,就会导致SpringMVC的容器管理中的controller map中的key重复了。

解决这个问题也比较简单。

在@Controller 中,使用重名名就可以了

如 下例子:

test.controller.bill.BillSaveController
package test.controller.bill;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * Created by liuch on 5/27/15. */@Controller@RequestMapping("/billsave")public class BillSaveController {    @RequestMapping("/dosave")    public String saveBill(){        return "billsave";    }}

及 test.controller.bill.BillSaveController

package test.controller.billsave;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * Created by liuch on 5/27/15. */@Controller@RequestMapping("/billsave_test")public class BillSaveController {    @RequestMapping("/test")    public String test(){        return "test";    }}

上面这两个代码虽然在不同的包下面,即全类名不同,但是类名却是相同。

这样,在Tomcat 启动的时候,后台会报错:

SEVERE: Context initialization failedorg.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource[/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.IllegalStateException: Annotation-specified bean name 'billSaveController' for bean class [test.controller.billsave.BillSaveController] conflicts with existing, non-compatible bean definition of same name and class [test.controller.bill.BillSaveController]

问题原因:

因为如果在使用注解 @Controller 时候,如果不使用命名,而SpringMVC会默认把类名的头一个字母小写,然后放到一个map中。

比如上面的例子,尽管上面两个类全类名不同,但是他们使用了@Controller 注解的时候,都没有使用命名。在SpringMVC在扫描Controller的时候,会把他们都默认解析为 billSaveController.然后以这个billSaveController为键(key), 放到一个全局的map中。

这样,就会出现两个键完全一样的Controller。由于SpringMVC不使用覆盖的方式处理具有相同键的不同全类名的Controller,、扫描的时候就会包上面的错误。

解决的办法:

在@Controller上使用名称

如:test.controller.bill.BillSaveController中

package test.controller.bill;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * Created by liuch on 5/27/15. */@Controller("testbillsave")@RequestMapping("/billsave")public class BillSaveController {    @RequestMapping("/dosave")    public String saveBill(){        return "billsave";    }}
test.controller.billsave.BillSaveController中,使用:
package test.controller.billsave;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * Created by liuch on 5/27/15. */@Controller("realbillsave")@RequestMapping("/billsave_test")public class BillSaveController {    @RequestMapping("/test")    public String test(){        return "test";    }}

上面两个Controller中,只要保证一个有命名即可,但是最好两个都使用上。

这是一种良好的编程方式,因为你无法保证其他人不会使用和你一样的类名的Controller。

后记:

下午让同事试了一下,果然可以了。


在项目中我遇到了一个同样的问题:

报错如下:
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [SpringConf/servlet-context.xml]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'checkWeakpasswordJob' for bean class [com.siemens.ct.its.iam.weakpassword.CheckWeakpasswordJob] conflicts with existing, non-compatible bean definition of same name and class [com.siemens.ct.its.iam.weakpassword.audit.task.CheckWeakpasswordJob]
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:414)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:243)
... 71 more
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'checkWeakpasswordJob' for bean class [com.siemens.ct.its.iam.weakpassword.CheckWeakpasswordJob] conflicts with existing, non-compatible bean definition of same name and class [com.siemens.ct.its.iam.weakpassword.audit.task.CheckWeakpasswordJob]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:345)
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:283)
at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:87)
at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74)

结果像上面类似方法处理成功.





阅读全文
0 0