Java dispatcherServlet bean冲突问题
我正在尝试运行一些spring boot应用程序,但遇到以下问题:Java dispatcherServlet bean冲突问题,java,spring,spring-boot,Java,Spring,Spring Boot,我正在尝试运行一些spring boot应用程序,但遇到以下问题: :: Built with Spring Boot :: 2.3.1.RELEASE
:: Built with Spring Boot :: 2.3.1.RELEASE
2020-08-30 17:43:17.953 GMT+05:30 INFO rid=NA cid=NA 21756 --- [ main] o.s.s.petclinic.PetClinicApplication : Starting PetClinicApplication v2.3.1.BUILD-SNAPSHOT on saasas-WX-3 with PID 21756 (C:
\work\upstream\fault-tolerance-demo\target\spring-petclinic-2.3.1.BUILD-SNAPSHOT.jar started by krsoni in C:\work\upstream\fault-tolerance-demo)
2020-08-30 17:43:17.959 GMT+05:30 INFO rid=NA cid=NA 21756 --- [ main] o.s.s.petclinic.PetClinicApplication : No active profile set, falling back to default profiles: default
2020-08-30 17:43:19.799 GMT+05:30 WARN rid=NA cid=NA 21756 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org
.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'dispatcherServlet' defined in class path resource [com/mycompany/asr/autoconfigure/asr/ASRAutoConfiguration
.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=com.mycompany.asr.
autoconfigure.asr.ASRAutoConfiguration; factoryMethodName=getDispatcherServlet; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/mycompany/asr/autoconfigure/asr/ASRAutoConfigur
ation.class]] for bean 'dispatcherServlet': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBea
nName=org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletConfiguration; factoryMethodName=dispatcherServlet; initMethodName=null; destroyMethodName=(inferred)
; defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfiguration$DispatcherServletConfiguration.class]] bound.
2020-08-30 17:43:19.811 GMT+05:30 INFO rid=NA cid=NA 21756 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-08-30 17:43:19.813 GMT+05:30 ERROR rid=NA cid=NA 21756 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'dispatcherServlet', defined in class path resource [com/mycompany/asr/autoconfigure/asr/ASRAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path r
esource [org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfiguration$DispatcherServletConfiguration.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
类,其中该bean定义位于下面。它是第三方jar,不能修改:
@Configuration
@PropertySource("asr.properties")
@EnableConfigurationProperties(AsrProperties.class)
public class ASRAutoConfiguration {
@Autowired
AsrProperties asrProperties;
@Bean
public EmbeddedServletContainerCustomizer getEmbeddedServletContainerCustomizer() {
// this property is accessed from many Contexts, so setting in system is the easiest
System.setProperty("org.apache.catalina.STRICT_SERVLET_COMPLIANCE", "true");
// configure Tomcat based on properties
AsrTomcatProperties asrTomcatProperties = new AsrTomcatProperties();
asrTomcatProperties.setConnectionTimeout(asrProperties.getServer().getConnectionTimeout());
asrTomcatProperties.setMaxSwallowSize(asrProperties.getServer().getMaxSwallowSize());
return new AsrEmbeddedTomcatCustomizer(asrTomcatProperties);
}
/**
* This is done to disable Spring MVC dispatcherServlet
*/
@Bean(name = "dispatcherServlet")
public String getDispatcherServlet() {
return "";
}
@Bean
public WebApplicationExceptionResponseBuilder getWebApplicationExceptionResponseBuilder() {
return new WebApplicationExceptionResponseBuilder();
}
@Bean
public ScmContext getScmContext() {
return new ScmContext();
}
}
我跟着这些线索走。但是如果我在application.properties中设置spring.main.allow bean definition overriding=true。我开始发现以下错误:
2020-08-30 18:24:14.438 GMT+05:30 INFO rid=NA cid=NA 13076 --- [ main] o.s.s.petclinic.PetClinicApplication : Starting PetClinicApplication v2.3.1.BUILD-SNAPSHOT on sasas-WX-3 with PID 13076 (C:
\work\upstream\fault-tolerance-demo\target\spring-petclinic-2.3.1.BUILD-SNAPSHOT.jar started by krsoni in C:\work\upstream\fault-tolerance-demo)
2020-08-30 18:24:14.444 GMT+05:30 INFO rid=NA cid=NA 13076 --- [ main] o.s.s.petclinic.PetClinicApplication : No active profile set, falling back to default profiles: default
2020-08-30 18:24:16.293 GMT+05:30 ERROR rid=NA cid=NA 13076 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Error processing condition on de.codecentric.spring.boot.chaos.monkey.configuration.ChaosMonkeyConfiguration.chaosMonkeyRestEndpoint
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:60)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:184)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:144)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:120)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:236)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:280)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:96)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:707)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.samples.petclinic.PetClinicApplication.main(PetClinicApplication.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.mycompany.asr.autoconfigure.asr.ASRAutoConfiguration] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@5197848c]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:358)
at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:414)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.lambda$getTypeForFactoryMethod$2(AbstractAutowireCapableBeanFactory.java:743)
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(Unknown Source)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:742)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:681)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:649)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1608)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 46 common frames omitted
2020-08-30 18:24:16.299 GMT+05:30 WARN rid=NA cid=NA 13076 --- [ main] o.s.boot.SpringApplication : Unable to close ApplicationContext
java.lang.IllegalStateException: Failed to introspect Class [com.mycompany.asr.autoconfigure.asr.ASRAutoConfiguration] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@5197848c]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481)
Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/context/embedded/EmbeddedServletContainerCustomizer
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getDeclaredMethods(Unknown Source)
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463)
... 29 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 33 common frames omitted
'如果您有额外的Servlet,您可以为每个Servlet或Servlet注册Bean声明一个@Bean类型,Spring Boot将透明地向容器注册它们。因为servlet是以这种方式注册的,所以可以将它们映射到DispatcherServlet的子上下文,而无需调用它
自行配置DispatcherServlet是不寻常的,但如果确实需要这样做,还必须提供DispatcherServletPath类型的@Bean,以提供自定义DispatcherServlet的路径。
spring boot starter web
自动配置名为DispatcherServlet
的org.springframework.web.servlet.DispatcherServlet
类型的Bean,因此,除非您允许,否则不能将其重新定义为String
类型的bean如果允许,类型为String
的bean将替换类型为DispatcherServlet
的bean。为什么要用这种类型定义一个具有这种特殊名称的bean?这毫无意义。Web自动配置查找名为dispatcherServlet
的bean,并尝试强制找到的bean键入dispatcherServlet
,但无法从字符串创建dispatcherServlet
。@Andreas:String类型bean在某个第三方jar中定义。我没有给它下定义。现在如何解决这个问题,因为我无法在thirdparty jar中进行更改创建名为dispatcherServlet
的String
类型bean的第三方jar不能与spring boot starter web
一起使用。它可能应该和弹簧一起使用,而不是弹簧靴尝试从组件扫描中排除具有@Bean
方法的@Configuration
组件,这样它就不会弄乱容器。