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 ...
随机推荐
- Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of ...
- 在LAMP环境下搭建JSP动态网页
开发环境Linux的版本号Linux localhost.localdomain 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29 11:47:41 EST 2013 x ...
- ViewPager的用法和实现过程
看图先: 页面中填充内容是随机关键词飞入和飞出动画效果,随后会更新,如今请先无视吧 首先是 导入jar包 下载地址:android-support-v4.jar 布局文件中加入v ...
- Windows 下启动Apache服务(转)
Win下apache出现问题:“No services installed”安装完apache之后(不是按照默认路径安装的,我的是 D:\ )右下方那个小羽毛图标是没有启动的,左键不好使,而且提示“N ...
- C#生成XML的三种途径
C#生成XML的三种途径 为了全面,这里都将XML保存到文件中,有三种生成XML的方式: 1.我认为是最原始,最基本的一种:利用XmlDocument向一个XML文件里写节点,然后再利用XmlDocu ...
- web服务构架
以我的理解大流量电商网站,一般构架如下: CDN 负载均衡集群 < === > 缓存服务器集群 反向代理服务器集群 web服务器集群(日志采集) < === > 缓存系统集群 ...
- .NET设计模式(8):适配器模式(Adapter Pattern)
):适配器模式(Adapter Pattern) 适配器模式(Adapter Pattern) --.NET设计模式系列之八 Terrylee,2006年2月 概述 在软件系统中,由于应用环境的 ...
- setTimeout 和 setInterval区别
setTimeout和setIntelval都有定时的功能!!!取消定时功能的时候,都有对应的clearTimeout以及clearInterval与之对应. 但是他们之间是有区别的! setTime ...
- 数据库msqlserver的几种类型及解决MSSQLServer服务启动不了的问题
从08年开始学习了sqlserver数据库之后,就一直以为sqlserver只有版本的区分,没有类型的差异:总以为从Sql2000. sql2005到sql2008.sql2012,微软出口的数据库, ...
- python中文问题汇总
1.中文路径 #-*-coding:utf-8-*- path=ur'E:\accumulate\Python\语法\08输入和输出\1.txt' #python内部使用的是unicode,不加前缀u ...