#BAEL-7484: add main source
This commit is contained in:
parent
e1dcefafa8
commit
fabd8328a6
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.boot.connection.via.builder;
|
||||
|
||||
import com.mongodb.ConnectionString;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoClientSettingsBuilderCustomizer;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringMongoConnectionViaBuilderApp {
|
||||
|
||||
public static void main(String... args) {
|
||||
SpringApplicationBuilder app = new SpringApplicationBuilder(SpringMongoConnectionViaBuilderApp.class);
|
||||
app.web(WebApplicationType.NONE);
|
||||
app.run(args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MongoClientSettingsBuilderCustomizer customizer(@Value("${custom.uri}") String uri) {
|
||||
ConnectionString connection = new ConnectionString(uri);
|
||||
return settings -> settings.applyConnectionString(connection);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.boot.connection.via.client;
|
||||
|
||||
import com.mongodb.client.ListDatabasesIterable;
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
import org.bson.Document;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
|
||||
|
||||
@SpringBootApplication(exclude={EmbeddedMongoAutoConfiguration.class})
|
||||
public class SpringMongoConnectionViaClientApp extends AbstractMongoClientConfiguration {
|
||||
|
||||
public static void main(String... args) {
|
||||
SpringApplicationBuilder app = new SpringApplicationBuilder(SpringMongoConnectionViaClientApp.class);
|
||||
app.web(WebApplicationType.NONE);
|
||||
app.run(args);
|
||||
}
|
||||
|
||||
@Value("${spring.data.mongodb.uri}")
|
||||
private String uri;
|
||||
|
||||
@Value("${spring.data.mongodb.database}")
|
||||
private String db;
|
||||
|
||||
@Override
|
||||
public MongoClient mongoClient() {
|
||||
MongoClient client = MongoClients.create(uri);
|
||||
ListDatabasesIterable<Document> databases = client.listDatabases();
|
||||
databases.forEach(System.out::println);
|
||||
return client;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDatabaseName() {
|
||||
return db;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.boot.connection.via.factory;
|
||||
|
||||
import com.mongodb.ConnectionString;
|
||||
import com.mongodb.client.MongoClient;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.mongodb.core.MongoClientFactoryBean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringMongoConnectionViaFactoryApp {
|
||||
|
||||
public static void main(String... args) {
|
||||
SpringApplicationBuilder app = new SpringApplicationBuilder(SpringMongoConnectionViaFactoryApp.class);
|
||||
app.web(WebApplicationType.NONE);
|
||||
app.run(args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MongoClientFactoryBean mongo(@Value("${custom.uri}") String uri) throws Exception {
|
||||
MongoClientFactoryBean mongo = new MongoClientFactoryBean();
|
||||
ConnectionString conn = new ConnectionString(uri);
|
||||
mongo.setConnectionString(conn);
|
||||
|
||||
mongo.setSingleton(false);
|
||||
|
||||
MongoClient client = mongo.getObject();
|
||||
client.listDatabaseNames()
|
||||
.forEach(System.out::println);
|
||||
return mongo;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.boot.connection.via.properties;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@PropertySource("classpath:connection.via.properties/application.properties")
|
||||
@SpringBootApplication(exclude={EmbeddedMongoAutoConfiguration.class})
|
||||
public class SpringMongoConnectionViaPropertiesApp {
|
||||
|
||||
public static void main(String... args) {
|
||||
SpringApplication.run(SpringMongoConnectionViaPropertiesApp.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
spring.data.mongodb.host=localhost
|
||||
spring.data.mongodb.port=27017
|
||||
spring.data.mongodb.database=baeldung
|
||||
spring.data.mongodb.username=admin
|
||||
spring.data.mongodb.password=password
|
|
@ -0,0 +1,99 @@
|
|||
package com.baeldung.boot.connection.via;
|
||||
|
||||
import com.baeldung.boot.connection.via.client.SpringMongoConnectionViaClientApp;
|
||||
import com.baeldung.boot.connection.via.properties.SpringMongoConnectionViaPropertiesApp;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.bson.Document;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class MongoConnectionApplicationLiveTest {
|
||||
private static final String HOST = "localhost";
|
||||
private static final String PORT = "27017";
|
||||
private static final String DB = "baeldung";
|
||||
private static final String USER = "admin";
|
||||
private static final String PASS = "password";
|
||||
|
||||
private void assertInsertSucceeds(ConfigurableApplicationContext context) {
|
||||
String name = "A";
|
||||
|
||||
MongoTemplate mongo = context.getBean(MongoTemplate.class);
|
||||
Document doc = Document.parse("{\"name\":\"" + name + "\"}");
|
||||
Document inserted = mongo.insert(doc, "items");
|
||||
|
||||
assertNotNull(inserted.get("_id"));
|
||||
assertEquals(inserted.get("name"), name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPropertiesConfig_thenInsertSucceeds() {
|
||||
SpringApplicationBuilder app = new SpringApplicationBuilder(SpringMongoConnectionViaPropertiesApp.class);
|
||||
app.run();
|
||||
|
||||
assertInsertSucceeds(app.context());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPrecedence_whenSystemConfig_thenInsertSucceeds() {
|
||||
System.setProperty("spring.data.mongodb.host", HOST);
|
||||
System.setProperty("spring.data.mongodb.port", PORT);
|
||||
System.setProperty("spring.data.mongodb.database", DB);
|
||||
System.setProperty("spring.data.mongodb.username", USER);
|
||||
System.setProperty("spring.data.mongodb.password", PASS);
|
||||
|
||||
SpringApplicationBuilder app = new SpringApplicationBuilder(SpringMongoConnectionViaPropertiesApp.class)
|
||||
.properties(
|
||||
"spring.data.mongodb.host=oldValue",
|
||||
"spring.data.mongodb.port=oldValue",
|
||||
"spring.data.mongodb.database=oldValue",
|
||||
"spring.data.mongodb.username=oldValue",
|
||||
"spring.data.mongodb.password=oldValue"
|
||||
);
|
||||
app.run();
|
||||
|
||||
assertInsertSucceeds(app.context());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConnectionUri_whenAlsoIncludingIndividualParameters_thenInvalidConfig() {
|
||||
System.setProperty(
|
||||
"spring.data.mongodb.uri",
|
||||
"mongodb://" + USER + ":" + PASS + "@" + HOST + ":" + PORT + "/" + DB
|
||||
);
|
||||
|
||||
SpringApplicationBuilder app = new SpringApplicationBuilder(SpringMongoConnectionViaPropertiesApp.class)
|
||||
.properties(
|
||||
"spring.data.mongodb.host=" + HOST,
|
||||
"spring.data.mongodb.port=" + PORT,
|
||||
"spring.data.mongodb.username=" + USER,
|
||||
"spring.data.mongodb.password=" + PASS
|
||||
);
|
||||
|
||||
BeanCreationException e = assertThrows(BeanCreationException.class, () -> {
|
||||
app.run();
|
||||
});
|
||||
|
||||
Throwable rootCause = e.getRootCause();
|
||||
assertTrue(rootCause instanceof IllegalStateException);
|
||||
assertThat(rootCause.getMessage()
|
||||
.contains("Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenClientConfig_thenInsertSucceeds() {
|
||||
SpringApplicationBuilder app = new SpringApplicationBuilder(SpringMongoConnectionViaClientApp.class);
|
||||
app.web(WebApplicationType.NONE)
|
||||
.run(
|
||||
"--spring.data.mongodb.uri=mongodb://" + USER + ":" + PASS + "@" + HOST + ":" + PORT + "/" + DB,
|
||||
"--spring.data.mongodb.database=" + DB
|
||||
);
|
||||
|
||||
assertInsertSucceeds(app.context());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue