title: Spring Boot multiple MongoDB configuration
body: <p>I have looked everywhere for this and it seems like I cannot find a solution that works.  I am using spring boot 1.5.10-RELEASE.  I am trying to configure two different mongodb instances in the same application.  Here is my code:</p>
  
  <p>Main Application:</p>
  
  <pre><code>@SpringBootApplication(exclude = {MongoAutoConfiguration.class})
  @ComponentScan("com.reef.reports")
  public class MainApplication
  {
      public static void main(String[] args)
      {
          SpringApplication.run(MainApplication.class, args);
      }
  }
  </code></pre>
  
  <p>1st Instance</p>
  
  <pre><code>@Configuration
  @EnableMongoRepositories(basePackages = {"com.reef.repository.mongousa"} , mongoTemplateRef = "USAMongo")
  public class MongoUsaConfig
  {
  
      @Value("${usa.mongodb.host}")
      private String host;
  
      @Value("${usa.mongodb.database:reef}")
      private String database;
  
      @Value("${usa.mongodb.port:27017}")
      private int port;
  
      @Value("${usa.mongodb.username:}")
      private String username;
  
      @Value("${usa.mongodb.password:}")
      private String password;
  
      @Value("${usa.mongodb.authdb:}")
      private String authdb;
  
      private final List&lt;MongoCredential&gt; credentials = new ArrayList&lt;&gt;();
      private final List&lt;ServerAddress&gt;   hosts = new ArrayList&lt;&gt;();
  
      /**
       * Method that creates MongoDbFactory
       * Common to both of the MongoDb connections
       */
      public MongoDbFactory mongoDbFactory()
      {
          return new SimpleMongoDbFactory(getMongoClient(), database);
      }
  
      /**
       * Method that creates MongoClient
       */
      @Bean(name = "USAClient")
      public MongoClient getMongoClient()
      {
  
          if ((null != username)&amp;&amp;(!username.isEmpty()))
          {
              hosts.add(new ServerAddress(host, port));
              credentials.add(MongoCredential.createMongoCRCredential(username, authdb, password.toCharArray()));
              return new MongoClient(hosts, credentials);
          }
          else
          {
              return new MongoClient(host, port);
          }
      }
  
      @Primary
      @Bean(name = "USAMongo")
      public MongoTemplate getMongoTemplate()
      {
          return new MongoTemplate(mongoDbFactory());
      }
  
  }
  </code></pre>
  
  <p>2nd Instance</p>
  
  <pre><code>@Configuration
  @EnableMongoRepositories(basePackages = {"com.reef.repository.mongocan"} , mongoTemplateRef = "CANMongo")
  public class MongoCanConfig
  {
  
      @Value("${can.mongodb.host}")
      private String host;
  
      @Value("${can.mongodb.database:reef}")
      private String database;
  
      @Value("${can.mongodb.port:27017}")
      private int port;
  
      @Value("${can.mongodb.username:}")
      private String username;
  
      @Value("${can.mongodb.password:}")
      private String password;
  
      @Value("${can.mongodb.authdb:}")
      private String authdb;
  
      private final List&lt;MongoCredential&gt; credentials = new ArrayList&lt;&gt;();
      private final List&lt;ServerAddress&gt;   hosts = new ArrayList&lt;&gt;();
  
      /**
       * Method that creates MongoDbFactory
       * Common to both of the MongoDb connections
       */
      public MongoDbFactory mongoDbFactory()
      {
          return new SimpleMongoDbFactory(getMongoClient(), database);
      }
  
      /**
       * Method that creates MongoClient
       */
      @Bean(name = "CANClient")
      public MongoClient getMongoClient()
      {
  
          if ((null != username)&amp;&amp;(!username.isEmpty()))
          {
              hosts.add(new ServerAddress(host, port));
              credentials.add(MongoCredential.createMongoCRCredential(username, authdb, password.toCharArray()));
              return new MongoClient(hosts, credentials);
          }
          else
          {
              return new MongoClient(host, port);
          }
      }
  
      @Bean(name = "CANMongo")
      public MongoTemplate getMongoTemplate()
      {
          return new MongoTemplate(mongoDbFactory());
      }
  }
  </code></pre>
  
  <p>When I run the application, it will run the configuration for the first instance.  However, it will not pick up the second instance.  I have put in breakpoints to debug and it never hits the breakpoint in the configuration.  The repositories in this package get loaded correctly:</p>
  
  <pre><code>com.reef.repository.mongousa
  </code></pre>
  
  <p>The errors happen with the repositories in this package:</p>
  
  <pre><code>com.reef.repository.mongocan
  </code></pre>
  
  <p>Please let me know what I am missing.  Why does one config work and the other does not?</p>