Spring Boot 版本从 1.5.8 更改为 2.0.0

Posted

技术标签:

【中文标题】Spring Boot 版本从 1.5.8 更改为 2.0.0【英文标题】:Spring Boot version changed from 1.5.8 to 2.0.0 【发布时间】:2018-10-20 07:20:18 【问题描述】:

在尝试迁移到 Spring Boot 2.0 版时,我在尝试运行 Junits 时遇到了以下问题。相同的单元测试用于在 Spring Boot 版本 1.5.8 上工作,但在更改版本后它们开始失败。以下是该问题的堆栈跟踪。你能告诉我同样的根本原因是什么吗?

应用程序运行失败 java.lang.IllegalStateException:Error 处理条件 org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration.pageableCustomizer|at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:64)|at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:109)|at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:179)|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:328)|at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233)|at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273)|at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:93)|at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:693)|at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)|at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752)|at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388)|at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)|at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:138)|at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)|at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)|at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107)|at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)|at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)|at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242)|at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)|at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)|at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)|at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)|at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)|at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)|at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)|at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)|at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)|at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)|at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)|at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)|at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)|at org.junit.runners.ParentRunner.run(ParentRunner.java:363)|at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)|at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:367)|at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:274)|at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)|at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:161)|at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)|at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)|at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)|导致 by:java.lang.IllegalStateException:@ConditionalOnMissingBean 没有 使用类型、名称或注释指定 bean 并尝试推断 bean 的类型失败|在 org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.validate(OnBeanCondition.java:390)|at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.(OnBeanCondition.java:380)|at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:114)|at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47)|...42 常见框架省略|导致 通过:org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanTypeDeductionException:失败 推断 bean 类型 org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration.pageableCustomizer|at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.addDeducedBeanTypeForBeanMethod(OnBeanCondition.java:443)|at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.addDeducedBeanType(OnBeanCondition.java:427)|at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.(OnBeanCondition.java:374)|...44 常见框架省略|导致 作者:java.lang.ClassNotFoundException:org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer|at java.net.URLClassLoader.findClass(URLClassLoader.java:381)|at java.lang.ClassLoader.loadClass(ClassLoader.java:424)|at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)|at java.lang.ClassLoader.loadClass(ClassLoader.java:357)|at org.springframework.util.ClassUtils.forName(ClassUtils.java:264)|at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.addDeducedBeanTypeForBeanMethod(OnBeanCondition.java:437)|...46 常用框架省略|2018-05-10 09:32:00,391 INFO[main]GenericWebApplicationContext[]>:关闭 org.springframework.web.context.support.GenericWebApplicationContext @71f bfa85:启动日期[Thu May 10 09:31:59 IST 2018];上下文根 层次结构

2018-05-10 09:32:00,393 错误[main]TestContextManager[]>:Caught 允许时出现异常 TestExecutionListener[org.springframework.test.context.web.ServletTestExecutionListener @3e3894 ce]准备测试 实例[com.sap.bs.businesslogging.util.ConfigValidatorTest @748500f 3]java.lang.IllegalStateException:Failed to load ApplicationContext|at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)|at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107)|at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)|at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)|at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242)|at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)|at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)|at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)|at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)|at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)|at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)|at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)|at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)|at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)|at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)|at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)|at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)|at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)|at org.junit.runners.ParentRunner.run(ParentRunner.java:363)|at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)|at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:367)|at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:274)|at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)|at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:161)|at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)|at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)|at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)|导致 by:java.lang.IllegalStateException:错误处理条件 on org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration.pageableCustomizer|at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:64)|at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:109)|at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:179)|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:328)|at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233)|at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273)|at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:93)|at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:693)|at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)|at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752)|at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388)|at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)|at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:138)|at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)|at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)|...26 常见框架省略|导致 by:java.lang.IllegalStateException:@ConditionalOnMissingBean 没有 使用类型、名称或注释指定 bean 并尝试推断 bean 的类型失败|在 org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.validate(OnBeanCondition.java:390)|at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.(OnBeanCondition.java:380)|at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:114)|at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47)|...42 常见框架省略|导致 通过:org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanTypeDeductionException:失败 推断 bean 类型 org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration.pageableCustomizer|at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.addDeducedBeanTypeForBeanMethod(OnBeanCondition.java:443)|at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.addDeducedBeanType(OnBeanCondition.java:427)|at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.(OnBeanCondition.java:374)|...44 常见框架省略|导致 作者:java.lang.ClassNotFoundException:org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer|at java.net.URLClassLoader.findClass(URLClassLoader.java:381)|at java.lang.ClassLoader.loadClass(ClassLoader.java:424)|at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)|at java.lang.ClassLoader.loadClass(ClassLoader.java:357)|at org.springframework.util.ClassUtils.forName(ClassUtils.java:264)|at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.addDeducedBeanTypeForBeanMethod(OnBeanCondition.java:437)|...46 常用框架省略

【问题讨论】:

看起来很简单:Caused by:java.lang.ClassNotFoundException:org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer 【参考方案1】:

正如 Roddy 指出的那样,错误是由于缺少 org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer 而发生的。

您的POM.xml 中是否有任何 Spring Data 依赖项?例如,spring-data-jpa

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
    </dependency>

【讨论】:

【参考方案2】:

在我的情况下,触发的是SpringDataWebAutoConfiguration(当它不应该被触发时)并且在类路径PageableHandlerMethodArgumentResolverCustomizer 上缺少类有问题。但是我没有在我的依赖项中包含 spring-data,所以我不得不排除它。 以下是如何在 gradle 中执行此操作的示例代码:

configurations 
    compile.exclude module: 'spring-data-commons'

【讨论】:

【参考方案3】:

您需要添加对 spring-boot-starter-data-redis 的依赖项,如果有,则删除对 spring-data-redis 的依赖项。

【讨论】:

以上是关于Spring Boot 版本从 1.5.8 更改为 2.0.0的主要内容,如果未能解决你的问题,请参考以下文章

将 Spring Boot 版本从 2.0.3.RELEASE 更改为 2.1.0.M4 时出现问题

为啥从 Spring Boot 版本 2.1.4 更改为 2.1.5 会出现未知配置 Maven 错误?

Spring Boot - RabbitMQ - 将消息内容类型从八位字节流更改为 json

Spring Boot:java.time.Duration 的默认序列化从 String 更改为 Number

Spring Boot webservice (REST) - 如何将 JUnit 5 测试从基本身份验证更改为 OAuth2 (Keycloak)

拦截器在 Spring Boot 1.5.8 版本中无法接收请求