title: Multiple Beans of MongoTemplate with spring boot data mongodb
body: <p>At first i used MongoTemplate without Configuration,<br>
  just made @Autowired and spring makes one default instanse based on properties from my application.yml<br>
  properties was like:  </p>
  
  <pre><code>spring:
    profiles: default
    data:
      mongodb:
        uri: mongodb://localhost:27017/myDbName
  </code></pre>
  
  <p>Now i need too write in two different database, i desided to create MongoConfig class<br>
  Example i found here <a href="http://java.amitph.com/2015/06/multiple-mongo-templates-in-spring-boot.html#.WAiSIeCLSUk" rel="nofollow">Example</a></p>
  
  <pre><code>public abstract class AbstractMongoConfig {
  
  private String host;
  
  private int port;
  
  private String database;
  
  //here is Getters and Setters...
  
  public MongoDbFactory mongoDbFactory() throws Exception {
      return new SimpleMongoDbFactory(new MongoClient(host, port), database);
  }
  
  abstract public MongoTemplate getMongoTemplate() throws Exception;
  }
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>@Configuration
  @ConfigurationProperties(prefix = "primary.mongodb")
  public class CommonMongoConfig
          extends AbstractMongoConfig {
      @Primary
      @Override public @Bean(name = "primaryMongoTemplate")
      MongoTemplate getMongoTemplate() throws Exception {
          return new MongoTemplate(mongoDbFactory());
      }
  }
  </code></pre>
  
  <p>Second one:</p>
  
  <pre><code>@Configuration
  @ConfigurationProperties(prefix = "second.mongodb")
  public class SecondaryMongoConfig
          extends  AbstractMongoConfig {
      @Override public @Bean(name = "secondMongoTemplate")
      MongoTemplate getMongoTemplate() throws Exception {
          return new MongoTemplate(mongoDbFactory());
      }
  }
  </code></pre>
  
  <p>Then i changed application.yml to:</p>
  
  <pre><code>spring:
    profiles: default
  primary:
    mongodb:
      host: localhost
      port: 27017
      database: myDbName
  second:
    mongodb:
      host: localhost
      port: 27017
      database: myDbName2
  </code></pre>
  
  <p>When i run application, it says me </p>
  
  <pre><code>IllegalArgumentException:Could not resolve placeholder 'spring.data.mongodb.uri' in string value "${spring.data.mongodb.uri}"
  </code></pre>
  
  <p>of course, i deleted it, because i dont need it any more!<br>
  Why so?<br>
  Then i tried add it back.  </p>
  
  <pre><code>spring:
    profiles: default
    data:
      mongodb:
        uri: mongodb://localhost:27017/fake #notUsedActually
  vkad:
    mongodb:
      host: localhost
      port: 27017
      database: myDbName
  gap:
    mongodb:
      host: localhost
      port: 27017
      database: myDbName2
  </code></pre>
  
  <p>And it works! But dont used!<br>
  MongoTemplate beans creates right, depends on my prefix properties, its good.<br>
  But why i cant delete this for now? ${spring.data.mongodb.uri}<br>
  Mb im doing smth wrong ?  </p>
  
  <p>Stacktrace with commented property data.mongodb.uri.</p>
  
  <pre><code>2016-10-24 13:06:29.372  WARN 15016 --- [  restartedMain] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'databaseConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.mongodb.uri' in string value "${spring.data.mongodb.uri}"
  2016-10-24 13:06:29.377  INFO 15016 --- [  restartedMain] utoConfigurationReportLoggingInitializer : 
  
  Error starting ApplicationContext. To display the auto-configuration report enable debug logging (start with --debug)
  Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.mongodb.uri' in string value "${spring.data.mongodb.uri}"
      at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
      at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
      at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:219) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
      at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:193) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
      at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:813) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1039) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
      ... 21 common frames omitted
  </code></pre>