ConflictingBeanDefinitionException: Annotation-specified bean name ‘XXX‘ for bean class

Spring Boot应用中的Conflicting Bean Definition异常分析与解决策略 解决项目启动报错Annotation-specified bean name ‘xxx‘问题 阅读详情

报错信息

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'mybatisInterceptor' for bean class [com.sinandata.service.commons.config.MybatisInterceptor] conflicts with existing, non-compatible bean definition of same name and class [com.sinandata.kg.report.config.MybatisInterceptor]
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:349)
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:287)
	at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132)
	at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:296)
	at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250)
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207)
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:175)

这个错误是因为在 Spring context 中有两个名称相同但类不兼容的 bean 定义而引起的。在您的项目中,有两个 XXX类被定义,并且都使用了相同的 bean 名称XXX,这导致了冲突。

如果该两个Bean都是你自己定义的,可以通过以下几种方式解决:

  1. 修改 bean 名称 - 将其中一个 MybatisInterceptor 的 bean 名称修改为不同的名称,避免名称冲突。

  2. 区分类名或包名 - 将两个 MybatisInterceptor 类的类名或所在包名进行区分,以避免类名冲突。

  3. 使用 @Qualifier 注解 - 在注入 bean 的过程中,使用 @Qualifier 注解指定要注入的具体 bean,而不是使用默认名称。例如:@Autowired @Qualifier(“mybatisInterceptor1”) private MybatisInterceptor mybatisInterceptor;

 如果其中一个是外部Jar包自带的依赖Bean,则通过以下方式排除Bean:
在启动类上加上注解
@ComponentScan( excludeFilters = [ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = [MybatisInterceptor.class()])])

解释:
 在 Spring 框架中,@ComponentScan 注解用于搜索标有 @Component、@Service、@Controller、@Repository 等注解的类,并将它们注册为 Spring 容器中的 Bean。有时候,您可能希望排除某些类,比如第三方库的类或者框架自带的类,以避免造成冲突。

 excludeFilters 属性是 @ComponentScan 注解的一个元素,可以用来指定要排除的类。其中,Filter 是一个枚举类型,包含四个过滤器类型:ANNOTATION、ASSIGNABLE_TYPE、ASPECTJ 和 CUSTOM。

 在提供的代码中,excludeFilters 属性使用了 ASSIGNABLE_TYPE 类型的过滤器,并指定了一个名为 MybatisInterceptor 的类。这意味着,如果某个类和 MybatisInterceptor 类型相同,它将被排除在扫描范围外,不会被注册为 Spring 容器中的 Bean。

【异常】ConflictingBeanDefinitionException: Annotation-specified bean name ‘voiceSettingMapper‘ for bean 具体来说,在项目中,有一个名为voiceSettingMapper的bean定义,这个bean的类是com.xxx.mapper.VoiceSettingMapper。但同时,Spring还试图创建另一个同名的bean,其类是org.mybatis.spring.mapper.MapperFactoryBean。如果你有两个同名的bean,但其中一个是你优先想使用的,你可以在那个bean上使用@Primary注解。为了解决这个问题,你有以下几种选择,请根据你的具体需求选择最适合的解决方案。 阅读详情

相关推荐

【GD32篇】驱动AD7616完成数据采集

GD32F10X驱动AD7616完成数据采集

we_long的博客 1万+

SpringBoot项目启动失败报错Annotation-specified bean name ‘xx‘ for bean class [xxx] conflicts with existing

问题描述 SpringBoot项目,更改醒目内容之后启动报错 Annotation-specified bean name ‘xx’ for bean class [xxx] conflicts with existing, non-compatible bean definition of same name and class[xxx] 问题分析 正在做的工作是将之前旧的项目进行功能拆分的微服务,导致其中一个部分在更新目录结构的过程中出现了工程中不同文件夹中复制了多分旧的工程的同一个文件的类,导致这

Mak1sΔe的博客 8万+

SBP依赖计算中的蝴蝶效应:一个Prefab修改如何触发全量重建?

本文深入探讨了Unity中SBP(Scriptable Build Pipeline)依赖计算的蝴蝶效应,解析了Prefab修改如何触发全量重建的机制。通过分析Asset与Object的层级关系、依赖计算的三种维度及CacheEntry系统,揭示了全量重建的根本原因,并提供了依赖可视化工具开发、优化策略与最佳实践,帮助开发者提升构建效率。

weixin_29015051的博客 454

Failed to parse configuration ... ConflictingBeanDefinitionException: Annotation-specified bean name

Failed to parse configuration class conflicts with existing, non-compatible bean definition of same name and class 类名

test 8579

AIGC实战:用ComfyUI和Lora微调打造专属原神角色(含完整工作流)

本文详细介绍了如何利用ComfyUI和Lora微调技术打造专属原神风格角色,涵盖从环境配置、数据集准备到工作流构建和Lora训练的全过程。通过模块化节点设计和高级控制技术,创作者可实现高度个性化的AIGC艺术创作,提升二次元角色生成的效率与质量。

weixin_29185199的博客 370

Keil5保姆级安装指南:从破解到STM32开发环境配置(附常见错误解决方案)

本文提供Keil5从安装到STM32开发环境配置的完整指南,包含破解步骤、常见错误解决方案及多芯片支持配置。详细讲解环境预检、核心安装、许可证管理及高效开发环境设置,帮助开发者快速搭建嵌入式开发平台。特别针对安装和卸载过程中的关键问题提供专业解决方案。

weixin_30369041的博客 136

ConflictingBeanDefinitionException异常,Annotation-specified bean name ‘redisConfig‘ for bean class

项目在启动的时候会报错: Annotation-specified bean name ‘xx’ for bean class [xxx] conflicts with existing, non-compatible bean definition of same name and class[xxx] 原因: spring中有相同名称的类时,使用时没有明确指定bean名称,则会抛出异常: 最简单的方法: 在项目中查找重复的Bean名称,将部分重复的bean名称更改一下;更改原则是:公共包下面的Bean保

qq_35146059的博客 4605

CUDA编译踩坑实录:如何正确设置-gencode参数适配你的GPU算力

本文深入解析CUDA编译中-gencode参数的核心原理与实战配置,帮助开发者解决常见的'Unsupported gpu architecture'错误。通过厘清虚拟架构与真实架构的区别,并提供多版本兼容的CMake配置方案,确保编译出的代码能精准适配不同GPU算力,兼顾性能与广泛兼容性。

weixin_28829903的博客 347

Java:启动报错:ConflictingBeanDefinitionException: Annotation-specified bean name ‘xxx‘ for be [xxx] ...

Java:启动报错:ConflictingBeanDefinitionException: Annotation-specified bean name 'xxx' for be [xxx] conflicts with existing, non-compatible bean definition of same name and class [tk.mybatis.spring.mapper.MapperFactoryBean]

weixin_52116015的博客 532

FairyGUI深度解析:Unity复杂UI开发的高效解决方案与性能优化

在Unity游戏和应用开发中,UI系统的性能与开发效率是核心挑战。原生UGUI的组件化模型在应对复杂、动态界面时,常面临Draw Call过高、层级管理臃肿的问题。其原理在于,每个UI元素对应一个GameObject,渲染合批受限于Transform层级与材质状态,易导致性能瓶颈。FairyGUI(FGUI)作为一种专业的第三方UI解决方案,其技术价值在于采用了截然不同的“舞台-显示对象”渲染模型。该模型通过内部轻量级对象管理与自主合批系统,实现了极高的运行时效率与Draw Call优化。在应用场景上,FG

weixin_31315567的博客 357

从理论到实践:Zemax驱动下的离轴三反光学系统全流程构建

本文详细阐述了从理论推导到软件实现的离轴三反光学系统全流程设计方法。文章首先分析了离轴三反系统在空间遥感等领域的优势,然后手把手指导如何基于像差理论计算共轴三反的初始结构参数,包括曲率半径与圆锥系数。最后,重点介绍了在Zemax中完成从共轴建模、视场离轴设置到优化与性能评估的完整实战步骤,为光学工程师提供了宝贵的经验与避坑指南。

weixin_29205061的博客 252

ConflictingBeanDefinitionException异常解决

org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'exceptionHandlerAdvice' for bean class [com.xxx.o2o.exceptionHandlerAdvice] conflicts with existing, non-compatible bean definition of same name and

weixin_45733659的博客 8736

Creo模型装配总结

creo7.0的零件装配操作方法,建立基准面、新建约束、约束顺序

heda3的博客 1万+

Spring Bean名称重复 ConflictingBeanDefinitionException 解决

冲突分析 如果我们希望将相同名称的类放入spring中时,如果未指定bean名称,则会抛出异常: Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'xxxx' for bean class [xxx] con...

AI天才研究院 2万+

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

团队协作中如何处理ConflictingBeanDefinitionException异常

当使用Spring框架进行Java应用程序开发时,可能会遇到ConflictingBeanDefinitionException异常。如:这个异常通常发生在以下情况下:在应用程序上下文中存在多个相同名称的Bean定义,导致Spring无法确定应该使用哪一个Bean。这个问题可能出现在团队协作开发中,特别是当不同的开发者在不同的模块中定义了相同名称的Bean时。在本文中,我们将探讨这个异常出现的原因、解决方案以及在团队协作开发中的编码建议。

FirstMrRight的博客 2245

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Spring启动异常之ConflictingBeanDefinitionException: Annotation-specified bean name ‘XXXXXService‘

启动Spring项目报错ConflictingBeanDefinitionException Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'XXXXXService' for bean class [com.service.common.MgrFileService] conflicts with existing.

MieMieFly的专栏 1万+

记录一个SpringBoot启动报错 Annotation-specified bean name ‘xxx‘的问题解决,简单排查思路

记录一个SpringBoot启动报错 Annotation-specified bean name 'xxx'的问题解决,简单排查思路

qq_45904119的博客 5279

Annotation-specified bean name ‘xxxController‘ for bean class [] conflicts with existing non-compati

Spring中很多注解都会有一个的参数,可以修改该参数值,来修改加载Spring中该类的bean的名称!但是,在之后需要注入该bean实例时候,则同样需要指定该bean的名称来注入!!!

m0_62780794的博客 2161

ConflictingBeanDefinitionException异常快速处理

简单理解为项目中存在两个名字相同的 Bean ,这两个Bean可能是代码里的,你可以通过全局搜索找到它们并改变名称;全局搜索找不到就说明是在你引入的jar包里,这时处理方式是在程序入口中用@ComponentScan注解来排除掉其中一个,参考如下。

kungFuApe的博客 1947

【莫名的BUG】

在同一个 Spring 应用上下文中存在两个不同包下的 某某 类,但它们都被配置为同一个 bean 名称 。Spring 容器无法区分这两个类,因此抛出ConflictingBeanDefinitionException 异常。

qq_45176189的博客 3102

Annotation-specified bean name.. for bean class ...

clean一下,再运行。 可能是idea缓存没清理,依然是上次的,导致报类名重复的错。

qq_42014105的博客 8237

Annotation-specified bean name for bean class

1、发现BUG:bug详情 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'f

心若向阳 无谓悲伤 2万+

Annotation-specified bean name 'xxx' for bean class [com.xxx.xxx.xxx] conflicts with existing

Annotation-specified bean name ‘userMapper’ for bean class [com.promote.website.mapper.UserMapper] conflicts with existing, non-compatible bean definition of same name and class [com.promote.user.mapp...

promote的博客 9984
上一篇: 用Spring Boot轻松实现定时任务--原理详解
下一篇: 解析SpringBoot中@Autowire注解的实现原理
JavaWizard-M
博客等级 码龄6年 3159粉丝 148原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址