could not be registered. A bean with that name has already been defined in file

今天在使用SpringBoot中的拦截器的功能是遇到了如下的BUG,为了让屏幕前的您尽快排雷,特此记录一下 ,报错信息如下:

java 复制代码
Description:

The bean 'noLoginInterceptor', defined in class path resource [com/zwl/crm/config/MyConfig.class], could not be registered. A bean with that name has already been defined in file [E:\workplace\MyProject\crm\target\classes\com\zwl\crm\Interceptor\NoLoginInterceptor.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

简单看一下报错信息,说"could not be registered",注入失败"?我的代码如下图:

首先想到的是@Bean没有被识别,于是改变写法使用IOC注入:

项目成功的运行起来了,很纳闷,这是怎么回事?仔细阅读报错信息" A bean with that name has already been defined in file",说明Bean被重复注册了,通过查找资料得知:

在启动类上加@MapperScan("需要扫描的包路径"),在项目启动时会自动将扫描到的Bean进行注册,我的是在配置拦截器时加了@Component

这个和配置类中的@Bean重复的为一个Bean进行注册,到此彻底明白了。

解决方案总结:

1.删除拦截器类级别的@Component注解

2.将配置类中的@Bean改为IOC自动注入

3.application配置文件里加一行:spring.main.allow-bean-definition-overriding: true 。当遇到同样名字的时候,是否允许覆盖注册

相关推荐
学长毕设19 分钟前
基于springboot的社区养老监护系统的设计与实现
java·spring boot·后端
会编程的吕洞宾22 分钟前
RAG检索质量怎么评估?3个指标+LLM裁判实战
java·开发语言·人工智能·后端
卢总霸气25 分钟前
上海交大机动学院学位论文参考文献Zotero CSL 样式-2026
java
段一凡-华北理工大学31 分钟前
高炉能源系统智能化:热风炉、煤气与极致能效~系列文章07:高炉煤气:产生规律与全厂用户平衡
java·前端·能源
咖啡八杯36 分钟前
Redis 工具封装:RedisCache 对 Operations 体系的二次封装
java·数据库·redis·缓存·框架·springboot·若依
wno70442 分钟前
Springboot国际化信息(i18n)解析
java·spring boot·后端
eleven_4u1 小时前
QPS如何计算,如何部署架构
java·linux·运维·jvm·中间件
牧瀬クリスだ1 小时前
Redis Hash实战:高效存储对象与购物车
java·数据库·redis·算法·哈希算法
文丑颜不良啊1 小时前
LeetCode1141~1160题目解析
java·leetcode
木井巳1 小时前
【动态规划: 路径问题】下降路径最小和
java·算法·leetcode·动态规划·推荐算法