春季启动:ClassNotFoundException de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion
Posted
技术标签:
【中文标题】春季启动:ClassNotFoundException de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion【英文标题】:spring boot: ClassNotFoundException de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion 【发布时间】:2018-08-29 10:36:10 【问题描述】:我有一个example spring boot project,它使用 mongo db 来持久化对象。我想使用嵌入式 mongo db 对其进行测试,但我收到一个与 this question 非常相似的错误(请参阅已接受答案的 cmets):由于某种原因,无法找到来自 flapdoodle
的 IFeatureAwareVersion
。
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is java.lang.NoClassDefFoundError: de/flapdoodle/embed/mongo/distribution/IFeatureAwareVersion
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
... 65 common frames omitted
Caused by: java.lang.NoClassDefFoundError: de/flapdoodle/embed/mongo/distribution/IFeatureAwareVersion
at cz.jirutka.spring.embedmongo.EmbeddedMongoFactoryBean.<init>(EmbeddedMongoFactoryBean.java:47) ~[embedmongo-spring-1.3.1.jar:1.3.1]
at com.example.MongoConfig.mongoTemplate(MongoConfig.java:37) ~[test-classes/:na]
at com.example.MongoConfig$$EnhancerBySpringCGLIB$$108c2b8.CGLIB$mongoTemplate$0(<generated>) ~[test-classes/:na]
at com.example.MongoConfig$$EnhancerBySpringCGLIB$$108c2b8$$FastClassBySpringCGLIB$$629c796a.invoke(<generated>) ~[test-classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at com.example.MongoConfig$$EnhancerBySpringCGLIB$$108c2b8.mongoTemplate(<generated>) ~[test-classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_92]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_92]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_92]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
... 66 common frames omitted
Caused by: java.lang.ClassNotFoundException: de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_92]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_92]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_92]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_92]
... 78 common frames omitted
这里是 pom
<?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>com.example</groupId>
<artifactId>demo-mongodb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo-mongodb</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.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>
<spring-cloud.version>Finchley.M8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<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.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cz.jirutka.spring</groupId>
<artifactId>embedmongo-spring</artifactId>
<version>1.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>$spring-cloud.version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
Spring boot 启动父级2.0.0.RELEASE
管理flapdoodle
的版本,即2.0.3
,我在de.flatpdoodle.embed.mongo-2.0.3.jar
中看到了IFeatureAwareVersion
类。
当我查看我看到的依赖层次结构时
embedmongo-spring: 1.3.1 [test]
de.flatpdoodle.embed.mongo: 2.0.3 (managed from 1.46.4) (ommitted for conflict with 2.0.3) [test]
de.flatpdoodle.embed.process: 1.40.1 (ommitted for conflict with 2.0.2) [test]
这是测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes=MongoConfig.class)
@TestPropertySource(locations = "classpath:application-test.properties")
public class Test_TimezoneDao
private static final Logger LOG = LoggerFactory.getLogger(Test_TimezoneDao.class);
//@Autowired private TimezoneDao dao;
@Autowired private CounterService counterService;
@Autowired
private MongoTemplate mongoTemplate;
@Test
public void test()
TimeZone tz = new TimeZone();
tz.setId(counterService.getNextSequence());
tz.setOffset(9);
tz.setTz("Asia/Singapore");
TimeZone tz2 = new TimeZone();
tz2.setId(counterService.getNextSequence());
tz2.setOffset(11);
tz2.setTz("Australia/Sydney");
mongoTemplate.save(tz);
List<TimeZone> tzs = mongoTemplate.findAll(TimeZone.class, "timezones");
for(TimeZone t : tzs)
LOG.info(t.toString());
这是测试配置
@SpringBootConfiguration
@Profile("test")
@ComponentScan(basePackages= "com.example.demomongodb")
@EnableJpaRepositories(basePackages= "com.example.demomongodb")
@EnableMongoRepositories(basePackages= "com.example.demomongodb")
@EntityScan(basePackages= "com.example.demomongodb")
@EnableAutoConfiguration
public class MongoConfig
public MongoConfig()
System.out.println("============= LOADING MONGO CONFIG ===============");
@Bean
public MongoTemplate mongoTemplate() throws IOException
EmbeddedMongoFactoryBean mongo = new EmbeddedMongoFactoryBean();
mongo.setBindIp("localhost");
MongoClient mongoClient = mongo.getObject();
MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, "test_or_whatever_you_want_to_call_this_db");
return mongoTemplate;
感谢您的帮助
【问题讨论】:
Spring Boot 1.5.12 出现同样的问题。 【参考方案1】:embedmongo-spring 有点过时了,它是 embed mongo 现在直接集成到 Spring Boot 中,而不是通过第三方依赖项。
只需删除此依赖项,您就应该完成。
您也可以看看这个问题,它可以进一步解释这种情况 https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/260
【讨论】:
【参考方案2】:de.flapdoodle.embed 依赖会通过 cz.jirutka.spring 添加。
[INFO] +- cz.jirutka.spring:embedmongo-spring:jar:1.3.1:compile
[INFO] | +- de.flapdoodle.embed:de.flapdoodle.embed.process:jar:1.40.1:compile
[INFO] | | +- net.java.dev.jna:jna:jar:4.5.2:compile
[INFO] | | \- net.java.dev.jna:jna-platform:jar:4.5.2:compile
[INFO] | \- org.mongodb:mongo-java-driver:jar:3.11.2:compile
[INFO] +- de.flapdoodle.embed:de.flapdoodle.embed.mongo:jar:2.2.0:test
通过从 cz.jirutka.spring 依赖项中排除 de.flapdoodle.embed.process 来解决此问题。
<dependency>
<groupId>cz.jirutka.spring</groupId>
<artifactId>embedmongo-spring</artifactId>
<version>RELEASE</version>
<exclusions>
<exclusion>
<artifactId>de.flapdoodle.embed.process</artifactId>
<groupId>de.flapdoodle.embed</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
【讨论】:
以上是关于春季启动:ClassNotFoundException de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion的主要内容,如果未能解决你的问题,请参考以下文章