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

在一次将工程从war包部署转为spring boot应用过程报以下错误:

2022-10-13 11:58:04.924 [main] [DEBUG] LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'mvcUrlPathHelper' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration; factoryMethodName=mvcUrlPathHelper; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]] for bean 'mvcUrlPathHelper': There is already [Root bean: class [org.springframework.web.util.UrlPathHelper]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:1004)
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:295)
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:153)
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:129)

Description:

The bean 'mvcUrlPathHelper', defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class], could not be registered. A bean with that name has already been defined and overriding is disabled.

意思是mvcUrlPathHelper这个bean被重复的注册
又因为spring.main.allow-bean-definition-overriding=false (默认值),所以报错。

解决方式:
将xml文件中包含标签:<mvc:resources 删掉就好了,比如:
<mvc:resources mapping=“/login.html” location=“/login.html”/> 删除掉。

因为这个标签spring会实例化class org.springframework.web.util.UrlPathHelper 这个类,
会和springbootorg.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration自动注入以下bean冲突,代码如下

WebMvcConfigurationSupport.java

@Bean
public UrlPathHelper mvcUrlPathHelper() {
    
    
	return getPathMatchConfigurer().getUrlPathHelperOrDefault();
}

猜你喜欢

转载自blog.csdn.net/huangdi1309/article/details/127303624