程序启动报错org.springframework.beans.factory.support.BeanDefinitionOverrideException(实例冲突)

一、报错信息

org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'taskScheduler' defined in class path resource [com/xxx/config/ScheduleConfig.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=scheduleConfig; factoryMethodName=taskScheduler; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/xxx/config/ScheduleConfig.class]] for bean 'taskScheduler': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=scheduledConfiguration; factoryMethodName=taskScheduler; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/xxx/config/ScheduledConfiguration.class]] bound.
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:897)
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:274)
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:141)
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:117)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:327)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
	at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:157)
	at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:137)
	at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:91)
	at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:171)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:743)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:719)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705)
	at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1720)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

二、原因

从报错信息可以看出,是
"com/xxx/config/ScheduleConfig.class"与"com/xxx/config/ScheduledConfiguration.class"产生冲突

三、排查

1、ScheduledConfiguration是本地项目代码中的类

2、ScheduleConfig是引入的自定义Jar包中的类

3、两个类都使用了@Bean注解,并且示例的名称都是一样的,导致冲突

程序启动报错org.springframework.beans.factory.support.BeanDefinitionOverrideException(实例冲突)_第1张图片
程序启动报错org.springframework.beans.factory.support.BeanDefinitionOverrideException(实例冲突)_第2张图片

四、解决

1、只保留其中一个类,这里是将本地项目的配置类去掉

程序启动报错org.springframework.beans.factory.support.BeanDefinitionOverrideException(实例冲突)_第3张图片

2、更改本地类@Bean实例后对象的名称

注:如果使用以上方法,在使用该实例时,要注意注入对象时所使用的名称

你可能感兴趣的:(Java开发问题,SpringBoot,java,spring,spring,boot)