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 ...
随机推荐
- 【转】Cannot change version of project facet Dynamic Web Module to 3.1 (Eclipse Maven唯一解决方案)
If you want to use version 3.1 you need to use the following schema: http://xmlns.jcp.org/xml/ns/jav ...
- GestureDetector封装手势检測上下滑动
项目中须要检測ListView的上滑下滑隐藏顶部View控件,之前在网上也有非常多实现案例.在git上发现个封装非常不错的样例,记录下来. GestureDetector是一个手势检測类,内部有个Si ...
- 50个Android开发技巧(24 处理ListView数据为空的情况)
在移动平台上为用户展示数据的一个经常用法是将数据填充进一个List内,而此时须要注意的一点就是: 原文地址:(http://blog.csdn.net/vector_yi/article/d ...
- 使用GridView自带分页的代码
关于GridView分页页码的讨论 在GridView中实现分页的效果方法非常easy,仅仅须要在"GridView任务"对话框中进行设置就能够了.在"GridView任 ...
- 【转】C++:在程序中获取全球唯一标识号(GUID或UUID)
Windows:使用CoCreateGuid函数(GUID) #include <objbase.h> #include <stdio.h> #define GUID_LEN ...
- App升级时数据库的迁移更新
前一段时间在进行App升级的时候,由于一开始版本初期没有考虑完善,导致走了很多弯路,后来经过自己的一些思考,总结出了一些在app升级的时候,数据库内文件同步保持更新的经验,希望能给大家带来帮助. 总体 ...
- Windows Server 2008 R2 安装及配置指南
一.安装需求: 1. 硬件需求条件 硬件 需求 处理器 最低:1.4 GHz(x64处理器) 注意:Windows Server 2008 for Itanium-Based Systems 版本需要 ...
- thinkphp 常见问题
0.写在最前面的不断更新 (1)trace不起作用 A:必须要输出到模板,才会有trace信息 (2)提示“您浏览的页面暂时发生了错误!请稍后再试-” A:检查控制器(看看能进到控制器没有,设断点输出 ...
- Razor的理解
[原创]Razor非常智能非常实用,不了解的人可能会觉得有没有都无所谓,其实不然,起初对Razor不是太了解,现在想想Razor就是来标示出C#语法的,但是HTML和C#混合输出时到底@这个小老鼠到底 ...
- ECshop--搜索模块细究
ecshop细究 今天看了下ecshop搜索这块,前台数据一直到后台查询再返回前台.大致是这么个过程,首先,在index.dwt文件内,body下面引入了 <!-- #BeginLibraryI ...