SpringMVC conflicts with existing, non-compatible bean definition of same name and class 的解决办法
问题起因
最近,项目组的里的同事遇到一个问题,他自己负责的模块,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 failed
org.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+Mybatis(使用AbstractRoutingDataSource实现多数据源切换时)事务管理未生效的解决办法
业务场景: A.B两个单位,系统部署同一套代码: A.B两系统能相互访问: 要求将数据从A系统同步到B系统,再将反馈信息回发给A: 实际开发情况: 因为系统比较小,最开始设计架构的时候没有考虑到消息互 ...
- spring项目报org.apache.tiles.definition.DefinitionsFactoryException: I/O错误原因及解决办法。
今天升级一个spring项目遇到如下错: HTTP Status 500 - Request processing failed; nested exception is org.apache.til ...
- springMVC的controller更改了,如何不重启,而自动刷新的解决办法(亲测,一招解决)
Tomcat con/ service.xml 配置如下一行代码: <Context reloadable="true"/> </Host> 然后以de ...
- spring 'arroudAspect' for bean class [com.dw.test.ArroudAspect] conflicts with existing, non-compatible bean definition of same name and class [com.dw.aspect.ArroudAspect]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: ...
- Annotation-specified bean name 'userDaoImpl' for bean class [***] conflicts with existing, non-compatible bean definition of same name and class [***]
使用Spring开发的时候报错如下: Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionExcept ...
- 【spring】non-compatible bean definition of same name and class
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected excep tion parsing XML do ...
- 解决:Could not resolve bean definition resource pattern [/WEB-INF/classes/spring/applicationContext-*.xml]
问题: 用Maven搭建spring.springmvc.mybatis时,运行报错: org.springframework.beans.factory.BeanDefinitionStoreExc ...
- Data Being Added Conflicts with Existing Data
While developing a page with multiple scrolls levels, and especially when using a grid, you may get ...
- [转载]Spring Bean Definition Inheritance
Following is the configuration file Beans.xml where we defined "helloWorld" bean which has ...
随机推荐
- Intent 传值和 Bundle传值的区别
http://blog.csdn.net/yanzi1225627/article/details/7802819 举个例子 我现在要从A界面 跳转到B界面或者C界面 这样的话 我就需要写2 ...
- VSS的运用小内容(针对于vs2008版本)(小的问题都是,仅供参考--只针对于菜鸟级的)
自己开始接触vss 的时候有些小的习惯没有很好的养成,下面的有关VSS内容都是简单的迁入迁出的问题,(仅供参考) 1.文件的迁入迁出:(.txt..xlsx..doc) a:文件的覆盖问题: 对于文件 ...
- HDU2017JAVA
字符串统计 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- yo bootstrap mui 使用对比
昨天晚上 又被问及职业发展方向,提及我的老本行css,切了几年的页面,近两年投入进css的时间屈指可数,被问及之前公司用的yo框架 对比业界内其他css 框架的优势. 1. yo模块化,碎片化 可自 ...
- php yield
php中关于 yield 关键字的介绍[点击查看] <?php function gen_one_to_three() { for ($i = 1; $i <= 3; $i++) { // ...
- 25、Javascript 事件
Javascript 事件 是指 Javascript 捕获到用户的操作,并做出正确的相应. Javascript 事件一般与DOM元素绑定. Javascript处理事件的基本机制 1.对DOM元素 ...
- Nico Game Studio 2.设置页面读写 纹理载入与选择
进度十分之慢... 配置读写一样采用之前写的自动绑定的方法: 分享一下代码: SetControl是把数据写到control上的. SetObject是把数据写到对象上 GetData是从控件读取数据 ...
- [上传下载] C#修改DownLoadHelper上传下载帮助类 (转载)
点击下载 DownLoadHelper.rar 主要功能如下 /// <summary> /// 输出硬盘文件,提供下载 支持大文件.续传.速度限制.资源占用小 /// </summ ...
- android - android Couldn't load runtimecore_java from loader
在写Arcgis Android 或百度Android的时候,有时会报诸如,java.lang.UnsatisfiedLinkError:android Couldn't load runtimeco ...
- QueryString传值的加密与解密方法 .
//加密 Response.Redirect("DetailInfo.aspx?id=" + Convert.ToBase64String(System.Text.Encoding ...