我有一个使用JPA的SpringBoot + Maven项目。尝试运行该应用程序时,出现以下错误堆栈。
org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / boot / autoconfigure / web / servlet / WebMvcAutoConfiguration $ EnableWebMvcConfiguration.class]中创建名称为'requestMappingHandlerAdapter'的bean时出错。失败嵌套的异常是java.lang.NoSuchFieldError:defaultInstance 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1794)〜[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)〜[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)〜[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:324)〜[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)〜[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)〜[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)〜[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)〜[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)〜[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)〜[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)〜[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] 在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)上[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] 在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)上[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] 在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)上[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:315)上[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)上[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)上[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] 在com.pasindu.dockerexample.DockerExampleApplication.main(DockerExampleApplication.java:14)[classes /:na] 造成原因:java.lang.NoSuchFieldError:defaultInstance 在org.springframework.web.servlet.mvc.method.annotation.ServletCookieValueMethodArgumentResolver。(ServletCookieValueMethodArgumentResolver.java:40)〜[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.getDefaultArgumentResolvers(RequestMappingHandlerAdapter.java:648)〜[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.afterPropertiesSet(RequestMappingHandlerAdapter.java:561)〜[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853)〜[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790)〜[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE] ...省略了17个通用框架 流程结束,退出代码为1
下面是我的pom.xml文件。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>docker-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>docker-example</name>
<description>Docker Example</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
</dependencies>
<build>
<finalName>app</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
关于如何解决此问题的任何想法?
答案 0 :(得分:1)
找到了解决方案。
在我的pom.xml中,有两个依赖项,例如
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
我所做的工作已删除 spring-web 依赖性,并重新加载了所有Maven插件并尝试再次运行该项目。
成功了。
答案 1 :(得分:0)
所描述的问题与不同的Maven依赖项之间的冲突有关。在尝试将最新版本的Spring Boot与Apache Camel一起使用时,我遇到了同样的问题。
分析依赖关系树后,发现spring-webmvc的版本为5.2.9,而所有其他spring依赖项的版本均为5.2.8。
就我而言,我已经通过使用更新版本的camel-spring-boot-dependencies(3.6.0,与以前使用的3.5.0不同)解决了该问题。