问题起因

最近,项目组的里的同事遇到一个问题,他自己负责的模块,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 的解决办法的更多相关文章

  1. springMVC+Mybatis(使用AbstractRoutingDataSource实现多数据源切换时)事务管理未生效的解决办法

    业务场景: A.B两个单位,系统部署同一套代码: A.B两系统能相互访问: 要求将数据从A系统同步到B系统,再将反馈信息回发给A: 实际开发情况: 因为系统比较小,最开始设计架构的时候没有考虑到消息互 ...

  2. spring项目报org.apache.tiles.definition.DefinitionsFactoryException: I/O错误原因及解决办法。

    今天升级一个spring项目遇到如下错: HTTP Status 500 - Request processing failed; nested exception is org.apache.til ...

  3. springMVC的controller更改了,如何不重启,而自动刷新的解决办法(亲测,一招解决)

    Tomcat  con/ service.xml  配置如下一行代码: <Context reloadable="true"/> </Host> 然后以de ...

  4. 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: ...

  5. 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 ...

  6. 【spring】non-compatible bean definition of same name and class

    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected excep tion parsing XML do ...

  7. 解决:Could not resolve bean definition resource pattern [/WEB-INF/classes/spring/applicationContext-*.xml]

    问题: 用Maven搭建spring.springmvc.mybatis时,运行报错: org.springframework.beans.factory.BeanDefinitionStoreExc ...

  8. Data Being Added Conflicts with Existing Data

    While developing a page with multiple scrolls levels, and especially when using a grid, you may get ...

  9. [转载]Spring Bean Definition Inheritance

    Following is the configuration file Beans.xml where we defined "helloWorld" bean which has ...

随机推荐

  1. Araxis Merge Professional v2014.4565 特别版 | 文件比较合并

    http://www.ttrar.com/html/AraxisMerge.html Araxis Merge 是一个可视化的文件比较.合并和同步的软件,能够方便的被软件工程师和 web 站点开发者使 ...

  2. ARCproject中加入非ARC文件,或者非ARC环境中加入ARC文件

    ARC与非ARC在一个项目中同一时候使用, 选择项目中的Targets,选中你所要操作的Target,选Build Phases,在当中Complie Sources中选择须要ARC的文件双击,并在输 ...

  3. Android应用源码图书馆管理系统带服务端数据库

    本项目是一套基于安卓的图书馆管理系统,包括jsp服务端源码,安卓客户端源码和mysql数据库.代码比较简单,供学习anroid与j2ee交互.例如Sqlite的使用.安卓客户端与jsp的web服务端的 ...

  4. 使用QEMU创建虚拟机

    下载安装: wget http://wiki.qemu-project.org/download/qemu-2.0.0.tar.bz2 tar xjvf qemu- ./configure --ena ...

  5. Quartz Enterprise Job Scheduler 1.x Tutorial---转载

    Lesson 10: Configuration, Resource Usage and SchedulerFactory http://www.quartz-scheduler.org/docume ...

  6. Redis Windows版安装及简单使用

    1.Redis简介及优势 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. 特点: Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次 ...

  7. Oracle存储过程中不支持DML语言的解决方法(针对遇见的DROP关键字)

    ---存储过程中的原语句: ---删除表 DROP TABLE A_NEWTDDATA; --报错 经查询:存储过程不支持DML语言: 解决方法: execute immediate 'DROP TA ...

  8. HDU-1114(背包DP)

    Piggy-Bank Problem Description Before ACM can do anything, a budget must be prepared and the necessa ...

  9. 经典关于多态的demo

    class Foo { public int a; public Foo() { a = 3; } public int addFive() { a += 5; return a; } public ...

  10. J2EE入门必备

    1,J2EE是什么 J2EE(Java 2 platform Enterprise Edition)是软件平台,适于创建服务器端的大型应用软件和服务系统. J2EE适合开发大规模的业务系统,这种级别的 ...