我有一个example spring boot project,它使用mongo db来保存对象。我想用嵌入式mongo db测试它,但是我得到一个与this question非常相似的错误(请参阅接受回答的评论):由于某些原因IFeatureAwareVersion
来自flapdoodle
不能被发现。
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引导启动父2.0.0.RELEASE
管理flapdoodle
2.0.3
的版本,我在IFeatureAwareVersion
中看到了de.flatpdoodle.embed.mongo-2.0.3.jar
个类。
当我查看依赖性层次结构时,我看到了
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;
}
}
感谢您的帮助
答案 0 :(得分:0)
embedmongo-spring有点过时了,它已嵌入mongo,现在无需通过第三方依赖项直接集成到spring boot中。
只需删除此依赖关系,您就应该完成。
您也可以看一下这个问题,它进一步解释了这种情况 https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/260
答案 1 :(得分:-2)
删除
<scope>test</scope>
来自
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
在pom.xml中