SpringMVC 启动报错,conflicts with existing, non-compatible bean definition of same name and class ...
来源:互联网 发布:java jface 编辑:程序博客网 时间:2024/06/05 09:56
问题起因
最近,项目组的里的同事遇到一个问题,他自己负责的模块,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。
后记:
下午让同事试了一下,果然可以了。
- SpringMVC 启动报错,conflicts with existing, non-compatible bean definition of same name and class ...
- SpringMVC conflicts with existing, non-compatible bean definition of same name and class 的解决办法
- [工作点滴]SpringMVC conflicts with existing, non-compatible bean definition of same name and class
- SpringMVC conflicts with existing, non-compatible bean definition of same name and class 的解决办法
- SpringMVC conflicts with existing, non-compatible bean definition of same name and class 的解决办法,sprin
- ....conflicts with existing, non-compatible bean definition of same name and class
- conflicts with existing, non-compatible bean definition of same name and class
- conflicts with existing, non-compatible bean definition of same name and class
- SpringMVC中类名重名(SpringMVC conflicts with existing, non-compatible bean definition of same name )
- non-compatible bean definition of same name and class [x
- 异常解决:non-compatible bean definition of same name and class【com.xxx.xxx.XXX】
- 记录:non-compatible bean definition of same name and class [com.XXX.XXX]
- Annotation-specified bean name 'drugInfoService' for bean class conflicts with existing
- spring boot启动报错Error creating bean with name 'entityManagerFactory' defined in class path resource [
- Invalid bean definition with name 'dataSource' defined in class
- invalid bean definition with name
- vs 2015 编译cocos2dx 报错C1189 #error: Macro definition of snprintf conflicts with Standard Library func
- vs 2015 编译cocos2dx 报错C1189 #error: Macro definition of snprintf conflicts with Standard Library func
- union的疑问
- centos6 pptp l2tp搭建
- mysql事务处理用法与实例详解
- 操作符重载_复数运算
- Android手机Fiddler2抓包教程
- SpringMVC 启动报错,conflicts with existing, non-compatible bean definition of same name and class ...
- ArcGIS制图表达Representation实战篇1-边界线和行道树制作
- 格雷码
- Soap UI Groovy 脚本访问授权的MongoDB
- 广播接收者
- 数据抽取
- ConcurrentHashMap原理分析
- git 创建别名
- 深入理解javascript原型和闭包(13)-【作用域】和【上下文环境】