bean mvcUrlPathHelper を登録できませんでした。その名前の Bean は既に定義されています

プロジェクトを 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 はクラス org.springframework.web.util.UrlPathHelper をインスタンス化するため、
次の Bean の競合を springbootorg.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration と自動的に挿入します。コードは次のとおりです。

WebMvcConfigurationSupport.java

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

おすすめ

転載: blog.csdn.net/huangdi1309/article/details/127303624