mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Polish contribution
Closes gh-8230
This commit is contained in:
parent
48b0f1577b
commit
b30d4303d5
@ -100,16 +100,6 @@
|
||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-async</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-reactivestreams</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.cache</groupId>
|
||||
<artifactId>cache-api</artifactId>
|
||||
@ -309,6 +299,16 @@
|
||||
<artifactId>jboss-transaction-spi</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-async</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-reactivestreams</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
|
@ -39,6 +39,7 @@ import org.springframework.core.env.Environment;
|
||||
* @author Oliver Gierke
|
||||
* @author Phillip Webb
|
||||
* @author Mark Paluch
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(MongoClient.class)
|
||||
@ -46,22 +47,16 @@ import org.springframework.core.env.Environment;
|
||||
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory")
|
||||
public class MongoAutoConfiguration {
|
||||
|
||||
private final MongoProperties properties;
|
||||
|
||||
private final MongoClientOptions options;
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
private final MongoClientFactory factory;
|
||||
|
||||
private MongoClient mongo;
|
||||
|
||||
public MongoAutoConfiguration(MongoProperties properties,
|
||||
ObjectProvider<MongoClientOptions> options, Environment environment) {
|
||||
this.properties = properties;
|
||||
this.options = options.getIfAvailable();
|
||||
this.environment = environment;
|
||||
this.factory = new MongoClientFactory(properties);
|
||||
this.factory = new MongoClientFactory(properties, environment);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
@ -74,7 +69,7 @@ public class MongoAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MongoClient mongo() throws UnknownHostException {
|
||||
this.mongo = this.factory.createMongoClient(this.options, this.environment);
|
||||
this.mongo = this.factory.createMongoClient(this.options);
|
||||
return this.mongo;
|
||||
}
|
||||
|
||||
|
@ -33,29 +33,37 @@ import org.springframework.core.env.Environment;
|
||||
/**
|
||||
* A factory for a blocking {@link MongoClient} that applies {@link MongoProperties}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
* @author Josh Long
|
||||
* @author Andy Wilkinson
|
||||
* @author Eddú Meléndez
|
||||
* @author Stephane Nicoll
|
||||
* @author Nasko Vasilev
|
||||
* @author Mark Paluch
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class MongoClientFactory {
|
||||
|
||||
private final MongoProperties properties;
|
||||
private final Environment environment;
|
||||
|
||||
public MongoClientFactory(MongoProperties properties) {
|
||||
public MongoClientFactory(MongoProperties properties, Environment environment) {
|
||||
this.properties = properties;
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link MongoClient} using the given {@code options} and
|
||||
* {@code environment}. If the configured port is zero, the value of the
|
||||
* {@code local.mongo.port} property retrieved from the {@code environment} is used to
|
||||
* Creates a {@link MongoClient} using the given {@code options}. If the configured
|
||||
* port is zero, the value of the {@code local.mongo.port} property is used to
|
||||
* configure the client.
|
||||
* @param options the options
|
||||
* @param environment the environment
|
||||
* @return the Mongo client
|
||||
* @throws UnknownHostException if the configured host is unknown
|
||||
*/
|
||||
public MongoClient createMongoClient(MongoClientOptions options,
|
||||
Environment environment) throws UnknownHostException {
|
||||
public MongoClient createMongoClient(MongoClientOptions options)
|
||||
throws UnknownHostException {
|
||||
|
||||
if (hasCustomAddress() || hasCustomCredentials()) {
|
||||
if (this.properties.getUri() != null) {
|
||||
throw new IllegalStateException("Invalid mongo configuration, "
|
||||
@ -75,7 +83,7 @@ public class MongoClientFactory {
|
||||
}
|
||||
String host = this.properties.getHost() == null ? "localhost"
|
||||
: this.properties.getHost();
|
||||
int port = determinePort(environment);
|
||||
int port = determinePort();
|
||||
return new MongoClient(
|
||||
Collections.singletonList(new ServerAddress(host, port)),
|
||||
credentials, options);
|
||||
@ -94,13 +102,13 @@ public class MongoClientFactory {
|
||||
&& this.properties.getPassword() != null;
|
||||
}
|
||||
|
||||
private int determinePort(Environment environment) {
|
||||
private int determinePort() {
|
||||
if (this.properties.getPort() == null) {
|
||||
return MongoProperties.DEFAULT_PORT;
|
||||
}
|
||||
if (this.properties.getPort() == 0) {
|
||||
if (environment != null) {
|
||||
String localPort = environment.getProperty("local.mongo.port");
|
||||
if (this.environment != null) {
|
||||
String localPort = this.environment.getProperty("local.mongo.port");
|
||||
if (localPort != null) {
|
||||
return Integer.valueOf(localPort);
|
||||
}
|
||||
@ -118,4 +126,5 @@ public class MongoClientFactory {
|
||||
}
|
||||
return MongoClientOptions.builder();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -135,15 +135,6 @@ public class MongoProperties {
|
||||
this.fieldNamingStrategy = fieldNamingStrategy;
|
||||
}
|
||||
|
||||
public void clearPassword() {
|
||||
if (this.password == null) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < this.password.length; i++) {
|
||||
this.password[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public String getUri() {
|
||||
return this.uri;
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ import org.springframework.core.env.Environment;
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Reactive Mongo.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Configuration
|
||||
@ -41,22 +42,16 @@ import org.springframework.core.env.Environment;
|
||||
@EnableConfigurationProperties(MongoProperties.class)
|
||||
public class ReactiveMongoAutoConfiguration {
|
||||
|
||||
private final MongoProperties properties;
|
||||
|
||||
private final MongoClientSettings settings;
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
private final ReactiveMongoClientFactory factory;
|
||||
|
||||
private MongoClient mongo;
|
||||
|
||||
public ReactiveMongoAutoConfiguration(MongoProperties properties,
|
||||
ObjectProvider<MongoClientSettings> settings, Environment environment) {
|
||||
this.properties = properties;
|
||||
this.settings = settings.getIfAvailable();
|
||||
this.environment = environment;
|
||||
this.factory = new ReactiveMongoClientFactory(properties);
|
||||
this.factory = new ReactiveMongoClientFactory(properties, environment);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
@ -69,7 +64,7 @@ public class ReactiveMongoAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MongoClient reactiveStreamsMongoClient() {
|
||||
this.mongo = this.factory.createMongoClient(this.settings, this.environment);
|
||||
this.mongo = this.factory.createMongoClient(this.settings);
|
||||
return this.mongo;
|
||||
}
|
||||
|
||||
|
@ -39,27 +39,29 @@ import org.springframework.core.env.Environment;
|
||||
* A factory for a reactive {@link MongoClient} that applies {@link MongoProperties}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class ReactiveMongoClientFactory {
|
||||
|
||||
private final MongoProperties properties;
|
||||
|
||||
public ReactiveMongoClientFactory(MongoProperties properties) {
|
||||
private final Environment environment;
|
||||
|
||||
public ReactiveMongoClientFactory(MongoProperties properties,
|
||||
Environment environment) {
|
||||
this.properties = properties;
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link MongoClient} using the given {@code options} and
|
||||
* {@code environment}. If the configured port is zero, the value of the
|
||||
* {@code local.mongo.port} property retrieved from the {@code environment} is used to
|
||||
* Creates a {@link MongoClient} using the given {@code options}. If the configured
|
||||
* port is zero, the value of the {@code local.mongo.port} property is used to
|
||||
* configure the client.
|
||||
* @param settings the settings
|
||||
* @param environment the environment
|
||||
* @return the Mongo client
|
||||
*/
|
||||
public MongoClient createMongoClient(MongoClientSettings settings,
|
||||
Environment environment) {
|
||||
public MongoClient createMongoClient(MongoClientSettings settings) {
|
||||
if (hasCustomAddress() || hasCustomCredentials()) {
|
||||
if (this.properties.getUri() != null) {
|
||||
throw new IllegalStateException("Invalid mongo configuration, "
|
||||
@ -79,7 +81,7 @@ public class ReactiveMongoClientFactory {
|
||||
}
|
||||
String host = this.properties.getHost() == null ? "localhost"
|
||||
: this.properties.getHost();
|
||||
int port = determinePort(environment);
|
||||
int port = determinePort();
|
||||
ClusterSettings clusterSettings = ClusterSettings.builder()
|
||||
.hosts(Collections.singletonList(new ServerAddress(host, port)))
|
||||
.build();
|
||||
@ -134,13 +136,13 @@ public class ReactiveMongoClientFactory {
|
||||
&& this.properties.getPassword() != null;
|
||||
}
|
||||
|
||||
private int determinePort(Environment environment) {
|
||||
private int determinePort() {
|
||||
if (this.properties.getPort() == null) {
|
||||
return MongoProperties.DEFAULT_PORT;
|
||||
}
|
||||
if (this.properties.getPort() == 0) {
|
||||
if (environment != null) {
|
||||
String localPort = environment.getProperty("local.mongo.port");
|
||||
if (this.environment != null) {
|
||||
String localPort = this.environment.getProperty("local.mongo.port");
|
||||
if (localPort != null) {
|
||||
return Integer.valueOf(localPort);
|
||||
}
|
||||
|
@ -234,7 +234,8 @@ public class EmbeddedMongoAutoConfiguration {
|
||||
* {@code embeddedMongoServer} bean.
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ com.mongodb.reactivestreams.client.MongoClient.class, ReactiveMongoClientFactoryBean.class })
|
||||
@ConditionalOnClass({ com.mongodb.reactivestreams.client.MongoClient.class,
|
||||
ReactiveMongoClientFactoryBean.class })
|
||||
protected static class EmbeddedReactiveMongoDependencyConfiguration extends
|
||||
ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor {
|
||||
|
||||
|
@ -112,6 +112,12 @@
|
||||
"description": "Enable LDAP repositories.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.reactive-repositories.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Enable Mongo reactive repositories.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.repositories.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
|
@ -41,6 +41,8 @@ org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.data.mongo.ReactiveMongoDataAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.data.mongo.ReactiveMongoRepositoriesAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
|
||||
@ -83,6 +85,7 @@ org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoCo
|
||||
org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mongo.ReactiveMongoAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.reactor.core.ReactorCoreAutoConfiguration,\
|
||||
|
@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.data.alt.mongo;
|
||||
import org.springframework.boot.autoconfigure.data.mongo.city.City;
|
||||
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
|
||||
|
||||
public interface ReactiveCityMongoDbRepository extends ReactiveCrudRepository<City, Long> {
|
||||
public interface ReactiveCityMongoDbRepository
|
||||
extends ReactiveCrudRepository<City, Long> {
|
||||
|
||||
}
|
||||
|
@ -17,9 +17,7 @@
|
||||
package org.springframework.boot.autoconfigure.data.mongo;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||
@ -36,9 +34,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
public class ReactiveMongoDataAutoConfigurationTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@After
|
||||
@ -54,8 +49,8 @@ public class ReactiveMongoDataAutoConfigurationTests {
|
||||
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class,
|
||||
MongoDataAutoConfiguration.class, ReactiveMongoAutoConfiguration.class,
|
||||
ReactiveMongoDataAutoConfiguration.class);
|
||||
assertThat(this.context.getBeanNamesForType(ReactiveMongoTemplate.class).length)
|
||||
.isEqualTo(1);
|
||||
assertThat(this.context.getBeanNamesForType(ReactiveMongoTemplate.class))
|
||||
.hasSize(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -52,7 +52,9 @@ public class ReactiveMongoRepositoriesAutoConfigurationTests {
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
this.context.close();
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -35,7 +35,7 @@ import org.springframework.test.util.ReflectionTestUtils;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link MongoClientFactory} via {@link MongoProperties}.
|
||||
* Tests for {@link MongoClientFactory}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
@ -51,7 +51,7 @@ public class MongoClientFactoryTests {
|
||||
public void portCanBeCustomized() throws UnknownHostException {
|
||||
MongoProperties properties = new MongoProperties();
|
||||
properties.setPort(12345);
|
||||
MongoClient client = new MongoClientFactory(properties).createMongoClient(null, null);
|
||||
MongoClient client = createMongoClient(properties);
|
||||
List<ServerAddress> allAddresses = extractServerAddresses(client);
|
||||
assertThat(allAddresses).hasSize(1);
|
||||
assertServerAddress(allAddresses.get(0), "localhost", 12345);
|
||||
@ -61,7 +61,7 @@ public class MongoClientFactoryTests {
|
||||
public void hostCanBeCustomized() throws UnknownHostException {
|
||||
MongoProperties properties = new MongoProperties();
|
||||
properties.setHost("mongo.example.com");
|
||||
MongoClient client = new MongoClientFactory(properties).createMongoClient(null, null);
|
||||
MongoClient client = createMongoClient(properties);
|
||||
List<ServerAddress> allAddresses = extractServerAddresses(client);
|
||||
assertThat(allAddresses).hasSize(1);
|
||||
assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017);
|
||||
@ -72,7 +72,7 @@ public class MongoClientFactoryTests {
|
||||
MongoProperties properties = new MongoProperties();
|
||||
properties.setUsername("user");
|
||||
properties.setPassword("secret".toCharArray());
|
||||
MongoClient client = new MongoClientFactory(properties).createMongoClient(null, null);
|
||||
MongoClient client = createMongoClient(properties);
|
||||
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret",
|
||||
"test");
|
||||
}
|
||||
@ -83,7 +83,7 @@ public class MongoClientFactoryTests {
|
||||
properties.setDatabase("foo");
|
||||
properties.setUsername("user");
|
||||
properties.setPassword("secret".toCharArray());
|
||||
MongoClient client = new MongoClientFactory(properties).createMongoClient(null, null);
|
||||
MongoClient client = createMongoClient(properties);
|
||||
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret",
|
||||
"foo");
|
||||
}
|
||||
@ -94,7 +94,7 @@ public class MongoClientFactoryTests {
|
||||
properties.setAuthenticationDatabase("foo");
|
||||
properties.setUsername("user");
|
||||
properties.setPassword("secret".toCharArray());
|
||||
MongoClient client = new MongoClientFactory(properties).createMongoClient(null, null);
|
||||
MongoClient client = createMongoClient(properties);
|
||||
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret",
|
||||
"foo");
|
||||
}
|
||||
@ -104,7 +104,7 @@ public class MongoClientFactoryTests {
|
||||
MongoProperties properties = new MongoProperties();
|
||||
properties.setUri("mongodb://user:secret@mongo1.example.com:12345,"
|
||||
+ "mongo2.example.com:23456/test");
|
||||
MongoClient client = new MongoClientFactory(properties).createMongoClient(null, null);
|
||||
MongoClient client = createMongoClient(properties);
|
||||
List<ServerAddress> allAddresses = extractServerAddresses(client);
|
||||
assertThat(allAddresses).hasSize(2);
|
||||
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
|
||||
@ -123,7 +123,7 @@ public class MongoClientFactoryTests {
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.thrown.expectMessage("Invalid mongo configuration, "
|
||||
+ "either uri or host/port/credentials must be specified");
|
||||
new MongoClientFactory(properties).createMongoClient(null, null);
|
||||
createMongoClient(properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -135,7 +135,12 @@ public class MongoClientFactoryTests {
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.thrown.expectMessage("Invalid mongo configuration, "
|
||||
+ "either uri or host/port/credentials must be specified");
|
||||
new MongoClientFactory(properties).createMongoClient(null, null);
|
||||
createMongoClient(properties);
|
||||
}
|
||||
|
||||
private MongoClient createMongoClient(MongoProperties properties)
|
||||
throws UnknownHostException {
|
||||
return new MongoClientFactory(properties, null).createMongoClient(null);
|
||||
}
|
||||
|
||||
private List<ServerAddress> extractServerAddresses(MongoClient client) {
|
||||
|
@ -78,7 +78,8 @@ public class MongoPropertiesTests {
|
||||
builder.requiredReplicaSetName("testReplicaSetName");
|
||||
MongoClientOptions options = builder.build();
|
||||
MongoProperties properties = new MongoProperties();
|
||||
MongoClient client = new MongoClientFactory(properties).createMongoClient(options, null);
|
||||
MongoClient client = new MongoClientFactory(properties, null)
|
||||
.createMongoClient(options);
|
||||
MongoClientOptions wrapped = client.getMongoClientOptions();
|
||||
assertThat(wrapped.isAlwaysUseMBeans()).isEqualTo(options.isAlwaysUseMBeans());
|
||||
assertThat(wrapped.getConnectionsPerHost())
|
||||
|
@ -57,7 +57,8 @@ public class ReactiveMongoAutoConfigurationTests {
|
||||
@Test
|
||||
public void clientExists() {
|
||||
this.context = new AnnotationConfigApplicationContext(
|
||||
PropertyPlaceholderAutoConfiguration.class, ReactiveMongoAutoConfiguration.class);
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
ReactiveMongoAutoConfiguration.class);
|
||||
assertThat(this.context.getBeanNamesForType(MongoClient.class).length).isEqualTo(1);
|
||||
}
|
||||
|
||||
@ -67,10 +68,11 @@ public class ReactiveMongoAutoConfigurationTests {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.data.mongodb.host:localhost");
|
||||
this.context.register(OptionsConfig.class,
|
||||
PropertyPlaceholderAutoConfiguration.class, ReactiveMongoAutoConfiguration.class);
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
ReactiveMongoAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(MongoClient.class).getSettings().getSocketSettings()
|
||||
.getReadTimeout(TimeUnit.SECONDS)).isEqualTo(300);
|
||||
assertThat(this.context.getBean(MongoClient.class).getSettings()
|
||||
.getSocketSettings().getReadTimeout(TimeUnit.SECONDS)).isEqualTo(300);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -79,10 +81,11 @@ public class ReactiveMongoAutoConfigurationTests {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.data.mongodb.uri:mongodb://localhost/test");
|
||||
this.context.register(OptionsConfig.class,
|
||||
PropertyPlaceholderAutoConfiguration.class, ReactiveMongoAutoConfiguration.class);
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
ReactiveMongoAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(MongoClient.class).getSettings().getReadPreference())
|
||||
.isEqualTo(ReadPreference.nearest());
|
||||
assertThat(this.context.getBean(MongoClient.class).getSettings()
|
||||
.getReadPreference()).isEqualTo(ReadPreference.nearest());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -91,7 +94,8 @@ public class ReactiveMongoAutoConfigurationTests {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.data.mongodb.uri:mongodb://localhost/test");
|
||||
this.context.register(SslOptionsConfig.class,
|
||||
PropertyPlaceholderAutoConfiguration.class, ReactiveMongoAutoConfiguration.class);
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
ReactiveMongoAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
MongoClient mongo = this.context.getBean(MongoClient.class);
|
||||
MongoClientSettings settings = mongo.getSettings();
|
||||
|
@ -31,7 +31,7 @@ import org.junit.rules.ExpectedException;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ReactiveMongoClientFactory} via {@link MongoProperties}.
|
||||
* Tests for {@link ReactiveMongoClientFactory}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@ -44,8 +44,7 @@ public class ReactiveMongoClientFactoryTests {
|
||||
public void portCanBeCustomized() throws UnknownHostException {
|
||||
MongoProperties properties = new MongoProperties();
|
||||
properties.setPort(12345);
|
||||
MongoClient client = new ReactiveMongoClientFactory(properties).createMongoClient(null,
|
||||
null);
|
||||
MongoClient client = createMongoClient(properties);
|
||||
List<ServerAddress> allAddresses = extractServerAddresses(client);
|
||||
assertThat(allAddresses).hasSize(1);
|
||||
assertServerAddress(allAddresses.get(0), "localhost", 12345);
|
||||
@ -55,8 +54,7 @@ public class ReactiveMongoClientFactoryTests {
|
||||
public void hostCanBeCustomized() throws UnknownHostException {
|
||||
MongoProperties properties = new MongoProperties();
|
||||
properties.setHost("mongo.example.com");
|
||||
MongoClient client = new ReactiveMongoClientFactory(properties).createMongoClient(null,
|
||||
null);
|
||||
MongoClient client = createMongoClient(properties);
|
||||
List<ServerAddress> allAddresses = extractServerAddresses(client);
|
||||
assertThat(allAddresses).hasSize(1);
|
||||
assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017);
|
||||
@ -67,8 +65,7 @@ public class ReactiveMongoClientFactoryTests {
|
||||
MongoProperties properties = new MongoProperties();
|
||||
properties.setUsername("user");
|
||||
properties.setPassword("secret".toCharArray());
|
||||
MongoClient client = new ReactiveMongoClientFactory(properties).createMongoClient(null,
|
||||
null);
|
||||
MongoClient client = createMongoClient(properties);
|
||||
assertMongoCredential(extractMongoCredentials(client).get(0), "user", "secret",
|
||||
"test");
|
||||
}
|
||||
@ -79,9 +76,9 @@ public class ReactiveMongoClientFactoryTests {
|
||||
properties.setDatabase("foo");
|
||||
properties.setUsername("user");
|
||||
properties.setPassword("secret".toCharArray());
|
||||
MongoClient client = new ReactiveMongoClientFactory(properties).createMongoClient(null,
|
||||
null);
|
||||
assertMongoCredential(extractMongoCredentials(client).get(0), "user", "secret", "foo");
|
||||
MongoClient client = createMongoClient(properties);
|
||||
assertMongoCredential(extractMongoCredentials(client).get(0), "user", "secret",
|
||||
"foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -90,9 +87,9 @@ public class ReactiveMongoClientFactoryTests {
|
||||
properties.setAuthenticationDatabase("foo");
|
||||
properties.setUsername("user");
|
||||
properties.setPassword("secret".toCharArray());
|
||||
MongoClient client = new ReactiveMongoClientFactory(properties).createMongoClient(null,
|
||||
null);
|
||||
assertMongoCredential(extractMongoCredentials(client).get(0), "user", "secret", "foo");
|
||||
MongoClient client = createMongoClient(properties);
|
||||
assertMongoCredential(extractMongoCredentials(client).get(0), "user", "secret",
|
||||
"foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -100,8 +97,7 @@ public class ReactiveMongoClientFactoryTests {
|
||||
MongoProperties properties = new MongoProperties();
|
||||
properties.setUri("mongodb://user:secret@mongo1.example.com:12345,"
|
||||
+ "mongo2.example.com:23456/test");
|
||||
MongoClient client = new ReactiveMongoClientFactory(properties).createMongoClient(null,
|
||||
null);
|
||||
MongoClient client = createMongoClient(properties);
|
||||
List<ServerAddress> allAddresses = extractServerAddresses(client);
|
||||
assertThat(allAddresses).hasSize(2);
|
||||
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
|
||||
@ -120,7 +116,7 @@ public class ReactiveMongoClientFactoryTests {
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.thrown.expectMessage("Invalid mongo configuration, "
|
||||
+ "either uri or host/port/credentials must be specified");
|
||||
new MongoClientFactory(properties).createMongoClient(null, null);
|
||||
createMongoClient(properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -132,7 +128,11 @@ public class ReactiveMongoClientFactoryTests {
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.thrown.expectMessage("Invalid mongo configuration, "
|
||||
+ "either uri or host/port/credentials must be specified");
|
||||
new MongoClientFactory(properties).createMongoClient(null, null);
|
||||
createMongoClient(properties);
|
||||
}
|
||||
|
||||
private MongoClient createMongoClient(MongoProperties properties) {
|
||||
return new ReactiveMongoClientFactory(properties, null).createMongoClient(null);
|
||||
}
|
||||
|
||||
private List<ServerAddress> extractServerAddresses(MongoClient client) {
|
||||
|
@ -589,6 +589,7 @@ content into your application; rather pick only the properties that you need.
|
||||
spring.data.mongodb.host=localhost # Mongo server host. Cannot be set with uri.
|
||||
spring.data.mongodb.password= # Login password of the mongo server. Cannot be set with uri.
|
||||
spring.data.mongodb.port=27017 # Mongo server port. Cannot be set with uri.
|
||||
spring.data.mongodb.reactive-repositories.enabled=true # Enable Mongo reactive repositories.
|
||||
spring.data.mongodb.repositories.enabled=true # Enable Mongo repositories.
|
||||
spring.data.mongodb.uri=mongodb://localhost/test # Mongo database URI. Cannot be set with host, port and credentials.
|
||||
spring.data.mongodb.username= # Login user of the mongo server. Cannot be set with uri.
|
||||
|
@ -3299,7 +3299,8 @@ pooled connection factory by default.
|
||||
http://www.mongodb.com/[MongoDB] is an open-source NoSQL document database that uses a
|
||||
JSON-like schema instead of traditional table-based relational data. Spring Boot offers
|
||||
several conveniences for working with MongoDB, including the
|
||||
`spring-boot-starter-data-mongodb` '`Starter`'.
|
||||
`spring-boot-starter-data-mongodb` and `spring-boot-starter-data-mongodb-reactive`
|
||||
'`Starters`'.
|
||||
|
||||
|
||||
|
||||
|
@ -23,6 +23,16 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-mongodb</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongo-java-driver</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver</artifactId>
|
||||
@ -39,15 +49,5 @@
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-mongodb</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongo-java-driver</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -1 +1 @@
|
||||
provides: spring-data-mongodb-reactive
|
||||
provides: spring-data-mongodb,mongodb-driver-async,mongodb-driver-reactivestreams
|
@ -1 +1 @@
|
||||
provides: spring-data-mongodb
|
||||
provides: spring-data-mongodb,mongodb-driver
|
Loading…
Reference in New Issue
Block a user