title: Spring Boot Starter-Web tries to connect to Mongo at startup
body: <p>I am experiencing some problems using Spring Boot and MongoDB external driver. I can't use the project Spring Data MongoDB, because I need to use the ufficial async driver given by Mongo. However, I need to use Spring Boot, because the module I am developing is part of a bigger project using this library.</p>
  
  <p>Here is my <code>pom.xml</code> file.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;artifactId&gt;tx-view&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
      &lt;!-- omissis --&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
              &lt;artifactId&gt;mongodb-driver-async&lt;/artifactId&gt;
              &lt;version&gt;${mongodb.version}&lt;/version&gt;
          &lt;/dependency&gt;
       &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                   &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                   &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;com.spotify&lt;/groupId&gt;
                  &lt;artifactId&gt;docker-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>In detail, I am using Spring Boot 1.4.1 and Mongo Async Driver 3.2.2. </p>
  
  <p>Here is my application.</p>
  
  <pre><code>@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
  public class TxViewApplication {
      public static void main(String[] args) {
          SpringApplication.run(TxViewApplication.class, args);
      }
  
      @Value("${mongo.uri}")
      private String mongoUri;
  
      @Bean
      public MongoClient mongoClient() {
          return MongoClients.create(mongoUri);
      }
  }
  </code></pre>
  
  <p>It follows the only (empty) test I have at the moment.</p>
  
  <pre><code>@SpringBootTest
  @RunWith(SpringRunner.class)
  public class ApplicationTest {
      @Test
      public void loadContext() throws Exception {}
  }
  </code></pre>
  
  <p>I have <strong>no other code</strong> in this project. When I run the test, I have the following error:</p>
  
  <pre><code>2016-11-22 15:43:58.597  INFO 4572 --- [null'}-db:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server db:27017
  
  com.mongodb.MongoException: java.io.IOException: Il computer remoto ha rifiutato la connessione di rete.
  
  at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:125) ~[mongodb-driver-core-3.2.2.jar:na]
  at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128) ~[mongodb-driver-core-3.2.2.jar:na]
  at java.lang.Thread.run(Thread.java:745) [na:1.8.0_101]
  Caused by: java.io.IOException: Il computer remoto ha rifiutato la connessione di rete.
  
  at sun.nio.ch.Iocp.translateErrorToIOException(Iocp.java:309) ~[na:1.8.0_101]
  at sun.nio.ch.Iocp.access$700(Iocp.java:46) ~[na:1.8.0_101]
  at sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:399) ~[na:1.8.0_101]
  at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112) ~[na:1.8.0_101]
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_101]
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_101]
  ... 1 common frames omitted
  </code></pre>
  
  <p>As you can see, I have properly inserted the <code>exclude</code> clause in the <code>SpringBootApplication</code> annotation in order to stop Spring Boot to try handle its own the connection to Mongo, as suggested in <a href="https://stackoverflow.com/questions/34414367/mongo-tries-to-connect-automatically-to-port-27017localhost">Mongo tries to connect automatically to port 27017(localhost)</a>.</p>
  
  <p>I have also noticed that I started to have the error after the addition to the <code>pom.xml</code> of the dependency to <code>spring-boot-starter-web</code>.</p>
  
  <p>How can I inhibit Spring Boot to try to connect automatically to Mongo at startup? The same problem is present with the synchronous version of MongoDB driver.</p>
  
  <p><strong>--- EDIT ---</strong></p>
  
  <p>I have also try to build a wrapper around the <code>async.MongoClient</code> object, in this way:</p>
  
  <pre><code>public class MongoWrapper {
      private final MongoClient mongo;
  
      public MongoWrapper() {
          mongo = MongoClients.create();
      }
  
      public MongoClient getMongo() {
          return mongo;
      }
  }
  </code></pre>
  
  <p>The configuration was changed accordingly.</p>
  
  <pre><code>@Bean
  public MongoWrapper mongo() {
      return new MongoWrapper();
  }
  </code></pre>
  
  <p>Unfortunately, nothing had changed. Spring Boot seems to intercept the <code>MongoClient</code> object also in this way :(</p>
  
  <p>Thanks a lot.</p>