Add auto-configuration support for Embedded MongoDB
See gh-2002
This commit is contained in:
parent
4aace564a2
commit
f2b2c085e9
|
@ -25,6 +25,11 @@
|
|||
<artifactId>spring-boot</artifactId>
|
||||
</dependency>
|
||||
<!-- Optional -->
|
||||
<dependency>
|
||||
<groupId>de.flapdoodle.embed</groupId>
|
||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.atomikos</groupId>
|
||||
<artifactId>transactions-jdbc</artifactId>
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package org.springframework.boot.autoconfigure.mongo;
|
||||
|
||||
import com.mongodb.Mongo;
|
||||
import de.flapdoodle.embed.mongo.MongodExecutable;
|
||||
import de.flapdoodle.embed.mongo.MongodStarter;
|
||||
import de.flapdoodle.embed.mongo.config.IMongodConfig;
|
||||
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
|
||||
import de.flapdoodle.embed.mongo.config.Net;
|
||||
import de.flapdoodle.embed.mongo.distribution.Version;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static de.flapdoodle.embed.process.runtime.Network.localhostIsIPv6;
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Mongo.class, MongodStarter.class})
|
||||
public class EmbedMongoAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private MongoProperties properties;
|
||||
|
||||
@Bean(initMethod = "start", destroyMethod = "stop")
|
||||
public MongodExecutable embedMongoServer() throws IOException {
|
||||
IMongodConfig mongodConfig = new MongodConfigBuilder()
|
||||
.version(Version.Main.PRODUCTION)
|
||||
.net(new Net(properties.getPort(), localhostIsIPv6()))
|
||||
.build();
|
||||
return MongodStarter.getDefaultInstance().prepare(mongodConfig);
|
||||
}
|
||||
|
||||
}
|
|
@ -46,6 +46,7 @@ org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration
|
|||
org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mongo.EmbedMongoAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.mongo;
|
||||
|
||||
import com.mongodb.CommandResult;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment;
|
||||
import static org.springframework.util.SocketUtils.findAvailableTcpPort;
|
||||
|
||||
public class EmbedMongoAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnVersionOfEmbeddedMongoServer() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
int mongoPort = findAvailableTcpPort();
|
||||
addEnvironment(context, "spring.data.mongodb.host=localhost", "spring.data.mongodb.port=" + mongoPort);
|
||||
context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, EmbedMongoAutoConfiguration.class);
|
||||
context.refresh();
|
||||
MongoTemplate mongo = context.getBean(MongoTemplate.class);
|
||||
CommandResult buildInfo = mongo.executeCommand("{ buildInfo: 1 }");
|
||||
assertEquals("2.6.1", buildInfo.getString("version"));
|
||||
}
|
||||
|
||||
}
|
|
@ -63,6 +63,7 @@
|
|||
<derby.version>10.11.1.1</derby.version>
|
||||
<dropwizard-metrics.version>3.1.2</dropwizard-metrics.version>
|
||||
<ehcache.version>2.10.0</ehcache.version>
|
||||
<embedmongo.version>1.46.4</embedmongo.version>
|
||||
<flyway.version>3.2.1</flyway.version>
|
||||
<freemarker.version>2.3.22</freemarker.version>
|
||||
<elasticsearch.version>1.5.2</elasticsearch.version>
|
||||
|
@ -473,6 +474,11 @@
|
|||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.flapdoodle.embed</groupId>
|
||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||
<version>${embedmongo.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.atomikos</groupId>
|
||||
<artifactId>transactions-jdbc</artifactId>
|
||||
|
|
Loading…
Reference in New Issue