[编程]-defined in null, could not be registered
之前已定义:
public interface AntiForgeCodeRepository extends MongoRepository<AntiForgeCode, String> {
}
程序启动、使用该接口正常。
再加一个存mongo的文档类型, 相应多了个接口:
public interface AntiForgeCodeCheckRecordRepository extends MongoRepository<AntiForgeCodeCheckRecord, String> {
}
结果程序启动报错:
14:39:57.611 logback [localhost-startStop-13] INFO o.linlinjava.litemall.DevApplication - Starting DevApplication v0.1.0 on win.local with \
PID 20380 (/Users/win/gg/apache-tomcat-8.5.64/webapps/api/WEB-INF/classes started by win in /Users/win/gg/apache-tomcat-8.5.64)
14:39:57.615 logback [localhost-startStop-13] DEBUG o.linlinjava.litemall.DevApplication - Running with Spring Boot v2.1.5.RELEASE, Spring v\
5.1.7.RELEASE
14:39:57.615 logback [localhost-startStop-13] INFO o.linlinjava.litemall.DevApplication - The following profiles are active: db-dev,core,ad\
min,vip,store
14:40:02.864 logback [localhost-startStop-13] ERROR o.s.b.d.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'antiForgeCodeCheckRecordRepository', defined in null, could not be registered. A bean with that name has already been defined in n\
ull and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
25-May-2021 14:40:02.865 严重 [localhost-startStop-13] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start\
:
org.apache.catalina.LifecycleException: 无法启动组件[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/api]]
at org.apache.catalina.util.LifecycleBase.handleSubClassException( LifecycleBase.java:440 )
1241681 Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'ant\
i ForgeCodeCheckRecordRepository' defined in null: Cannot register bean definition [Root bean: class [org.springframework.data.mongod\
b. repository.support.MongoRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowir\
eCa ndidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] for bean \
'ant iForgeCodeCheckRecordRepository': There is already [Root bean: class [org.springframework.data.mongodb.repository.support.MongoR\
eposi toryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=fa\
lse; f actoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.
原因分析:
The bean 'antiForgeCodeCheckRecordRepository', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.
这句关键报错,是说antiForgeCodeCheckRecordRepository被定义了两次。
但是代码里其实只有一个地方定义。
也可能是说MongoRepositoryFactoryBean被定义了两次,具体待查:
There is already [Root bean: class [org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean];
解决:
spring.main.allow-bean-definition-overriding=true
如果项目里有多个工程,多个配置文件,这个设置就需要找对地方。
一般是住工程那个配置,具体看tomact报错指向那个工程的Application, 比如这里的是DevApplication。
参考:
Feign调用报错The bean 'XXX.FeignClientSpecification', defined in null, could not be registered....的解决办法
The bean 'xxxxx', defined in null, could not be registered. A bean with that name has already ...