Java项目中,不同包下有重名的Java文件,启动的时候为什么会报错?
巴扎黑
巴扎黑 2017-04-18 09:16:30
[Java讨论组]

org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/classes/springContext.xml]; nested exception is java.lang.IllegalStateException: Annotation-specified bean name 'cardController' for bean class [com.qdzq.wlpt.controller.api.CardController] conflicts with existing, non-compatible bean definition of same name and class [com.qdzq.wlpt.controller.admin.CardController]

不同包下的文件不是可以重名吗?

巴扎黑
巴扎黑

全部回复(4)
高洛峰

你使用了Spring的Annotation,@Controller标记的bean的id为类名(首字母小写),这两个CardController的bean id均为cardController,所以id冲突

黄舟

可以贴出你xml文件看看

巴扎黑

你有2个CardController,自动注入后bean的id就冲突了,如果2个都想要,在spring文件里面配置2个bean,设置成不同的id,就不会冲突了,spring默认bean的id就是类名首字母小写

高洛峰

Auto-Scan的话Controller默认不允许重名,Service和Dao之类的也是。
默认类是ClassName则beanID为className。
可以自定义bean id,@Service("xxxService"),Controller没重复过,应该也可以同样的处理。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号