title: Spring-data-mongodb db-factory id does not work
body: <p>I have a working copy of mongo-config.xml which uses the default id of db-factory:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:util="http://www.springframework.org/schema/util"
      xmlns:mongo="http://www.springframework.org/schema/data/mongo"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
          http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.4.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;
  
  &lt;mongo:mongo id="replicaSetMongo" replica-set="localhost:10901,localhost:10902"&gt;
  &lt;mongo:options connections-per-host="8"
  ssl="false" /&gt;
  &lt;/mongo:mongo&gt;
  
  &lt;mongo:db-factory
  mongo-ref="replicaSetMongo"
  dbname="logs"
  username="logs_owner"
  password="logs_owner" /&gt;
  
  &lt;mongo:mapping-converter&gt;
  &lt;mongo:custom-converters&gt;
  &lt;mongo:converter&gt;
  &lt;beanclass="com.utils.mongodb.dao.impl.SspLogsWriteConverter"/&gt;
  &lt;/mongo:converter&gt;
  &lt;/mongo:custom-converters&gt;
  &lt;/mongo:mapping-converter&gt;
  
  &lt;beanid="xmlLogTemplate"class="org.springframework.data.mongodb.core.MongoTemplate"&gt;
  &lt;constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/&gt;
  &lt;constructor-arg name="mongoConverter" ref="mappingConverter"/&gt;
  &lt;propertyname="writeConcern"&gt;
  &lt;util:constant static-field="com.mongodb.WriteConcern.UNACKNOWLEDGED"/&gt;
  &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>However we have requirement to connect to 2 different databases on the same mongodb replica-set. Hence I want to define 2 different mongo:db-factory beans. So I made the following change:
  (Only the changed beans are shown below)</p>
  
  <pre><code>&lt;mongo:db-factory id="xmlDbFactory" 
      mongo-ref="replicaSetMongo"
      dbname="logs"
      username="logs_owner"
      password="logs_owner" /&gt;
  
  &lt;beanid="xmlLogTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"&gt;
      &lt;constructor-arg name="mongoDbFactory" ref="xmlDbFactory"/&gt;
      &lt;constructor-arg name="mongoConverter" ref="mappingConverter"/&gt;
      &lt;property name="writeConcern"&gt;
      &lt;util:constant static-field="com.mongodb.WriteConcern.UNACKNOWLEDGED"/&gt;
      &lt;/property&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>As soon as I deploy this change, my JBoss starts throwing errors at startup as follows.
  Is this a bug in Spring, or am I doing something wrong? </p>
  
  <pre><code>15:33:37,507 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-1) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexCreationHelper': Cannot resolve reference to bean 'mongoDbFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoDbFactory' is defined
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:615) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:148) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1049) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) [spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) [spring-context-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
      at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
      at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
      at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
      at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_45]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_45]
      at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_45]
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoDbFactory' is defined
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:549) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1096) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
      ... 25 more
  </code></pre>