Spring Boot 中的库不匹配问题
Posted
技术标签:
【中文标题】Spring Boot 中的库不匹配问题【英文标题】:Library Mismatch Issue in Spring Boot 【发布时间】:2018-10-17 12:40:28 【问题描述】:我的Spring-boot
应用程序运行良好,直到我开始摆弄弹簧靴和黄瓜测试。不能确切地说出了什么问题,但从我的研究来看,这似乎是我的pom.xml
中的库依赖不匹配@
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>EmploymentBootCamp</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>EmploymentBootCamp</name>
<description>Demo project for Spring Boot Employment Boot Camp</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<start-class>EmploymentBootCamp.demo.EmploymentBootCampApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.0.1.RELEASE</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
<configuration>
<mainClass>EmploymentBootCamp.demo.EmploymentBootCampApplication</mainClass>
<layout>ZIP</layout>
</configuration>
</plugin>
</plugins>
</build>
我得到的错误在下面
java.lang.NoClassDefFoundError: org/springframework/dao/support/PersistenceExceptionTranslator 在 java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_152] 在 java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~[na:1.8.0_152] 在 java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[na:1.8.0_152] 在 java.net.URLClassLoader.defineClass(URLClassLoader.java:467) ~[na:1.8.0_152] 在 java.net.URLClassLoader.access$100(URLClassLoader.java:73) ~[na:1.8.0_152] 在 java.net.URLClassLoader$1.run(URLClassLoader.java:368) ~[na:1.8.0_152] 在 java.net.URLClassLoader$1.run(URLClassLoader.java:362) ~[na:1.8.0_152] 在 java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_152] 在 java.net.URLClassLoader.findClass(URLClassLoader.java:361) ~[na:1.8.0_152] 在 java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_152] 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) ~[na:1.8.0_152] 在 java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_152] 在 org.springframework.data.jpa.util.BeanDefinitionUtils.(BeanDefinitionUtils.java:55) ~[spring-data-jpa-2.0.6.RELEASE.jar:2.0.6.RELEASE] 在 org.springframework.data.jpa.repository.support.EntityManagerBeanDefinitionRegistrarPostProcessor.postProcessBeanFactory(EntityManagerBeanDefinitionRegistrarPostProcessor.java:54) ~[spring-data-jpa-2.0.6.RELEASE.jar:2.0.6.RELEASE] 在 org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE] 在 org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:179) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE] 在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE] 在 EmploymentBootCamp.demo.EmploymentBootCampApplication.main(EmploymentBootCampApplication.java:10) [classes/:na] 引起:java.lang.ClassNotFoundException:org.springframework.dao.support.PersistenceExceptionTranslator 在 java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_152] 在 java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_152] 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) ~[na:1.8.0_152] 在 java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_152] ... 省略了 25 个常用帧
【问题讨论】:
在我看来更多是缺少依赖而不是不匹配的版本。 【参考方案1】:您可以安全地删除此依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<scope>test</scope>
</dependency>
通过 spring-boot-starter-data-jpa 你已经有了对 spring-tx 的传递依赖。
通过将 spring-tx 显式绑定到 test-scope,您可以将其从 compile-scope(mavens 默认范围)中删除
【讨论】:
谢谢!你救了我很多头痛以上是关于Spring Boot 中的库不匹配问题的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot如何在更新前检查数据库中的编码密码是不是与表单中的密码匹配
spring cloud 与spring boot 版本不匹配引发的问题总结