spring boot项目中出现spring版本冲突问题

在配置Test时引入了一些spring的依赖,在运行Test的时候,出现了一个java.lang.NoSuchMethodError:org.springframework.beans.factory.annotation.AnnotatedBeanDefinition.setRole(I)V的错误。错误详情如下:

Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
java.lang.NoSuchMethodError: org.springframework.beans.factory.annotation.AnnotatedBeanDefinition.setRole(I)V
at org.springframework.context.annotation.AnnotationConfigUtils.processCommonDefinitionAnnotations(AnnotationConfigUtils.java:259)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.registerBeanDefinitionForImportedConfigurationClass(ConfigurationClassBeanDefinitionReader.java:158)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:138)
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:693)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:530)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4641)

在一通Google、baidu以及bing之后,仍然没有解决问题。
但是通过这些搜索,发现了问题的所在(虽然当时没解决),其中,在stackoverflow中一篇回答很好,说By Changing Spring version to 5.0.3 and spring batch to 4.0.0, the service started successfully.但是经过尝试,并没有解决我的问题。但是我也从中发现问题出在了spring的版本冲突上。所以开始探索spring boot版本冲突如何解决,最后还是在一篇stackoverflow的回答中找到了答案,豁然开朗。
以下为文章问答链接:
stackoverflow问答链接
文章中提到,why are u using version numbers, could you try to remove all tags, spring boot might automatically know which versions to use.幡然醒悟,version tag不需要每个都自己标识版本号,spring boot可以自动匹配版本号,所以解决方案是:

  • 定位了出错的类在哪写jar包里
  • 将这些jar包的所在依赖的版本号注释掉,即可解决问题

你可能感兴趣的:(后端Spring,Boot,spring)