title: Spring4 doesn't load injected bean mongoTemplate successfully
body: <p>I got a project to use spring framework and mongodb, but now I got a problem to load mongoTemplate successfully.</p>
  
  <p>here is my pom.xml:</p>
  
  <pre><code>&lt;properties&gt;
  ...
  &lt;org.springframework.version&gt;4.0.5.RELEASE&lt;/org.springframework.version&gt;
  &lt;jdk.version&gt;1.7&lt;/jdk.version&gt;
  &lt;spring.mongodb.version&gt;1.5.1.RELEASE&lt;/spring.mongodb.version&gt;
  &lt;mongo.java.driver&gt;2.9.3&lt;/mongo.java.driver&gt;
  ...
  &lt;/properties&gt;
  
  &lt;!-- mongo --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
      &lt;artifactId&gt;mongo-java-driver&lt;/artifactId&gt;
      &lt;version&gt;${mongo.java.driver}&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
      &lt;artifactId&gt;spring-data-mongodb&lt;/artifactId&gt;
      &lt;version&gt;${spring.mongodb.version}&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;cglib&lt;/groupId&gt;
      &lt;artifactId&gt;cglib&lt;/artifactId&gt;
      &lt;version&gt;2.2.2&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>here is spring-mongo-config.xml</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:mongo="http://www.springframework.org/schema/data/mongo"
     xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd"&gt;
  
  &lt;!-- Default bean name is 'mongo' --&gt;
  &lt;mongo:mongo host="localhost" port="27017"&gt;
      &lt;!-- OPTIONAL: configure &lt;mongo:options /&gt; --&gt;
  &lt;/mongo:mongo&gt;
  
  &lt;mongo:db-factory dbname="test" mongo-ref="mongo"/&gt;
  
  &lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"&gt;
      &lt;constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/&gt;
  &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>After configuration done, I wrote a junit testcase to test it upfront, however it always fails. here is the testcase,</p>
  
  <pre><code>public class MongoRepositoryTest extends TestCase {
  @Autowired
  private ConfigurableApplicationContext context;
  
  /**
   * setup
   */
  @Override
  protected void setUp() throws Exception {
      try {
          this.context = new GenericXmlApplicationContext("classpath*:/WEB_INF/spring-mongo-config.xml");
      }
      catch(Exception e){
          System.out.println(e.getMessage());
          throw e;
      }       
  }
  
  public void testMongoConfig(){
      System.out.println(GitGerritLogsLegacyRepositoryImpl.class);
      System.out.println(context.getBeanDefinitionNames().length + "===");
      MongoTemplate mongoTemplate = (MongoTemplate) context.getBean("mongoTemplate");
  }
  }
  </code></pre>
  
  <p>The error message goes like below,</p>
  
  <pre><code>org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' is defined
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
  at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
  at test.java.com.datahub.repository.MongoRepositoryTest.testMongoConfig(MongoRepositoryTest.java:39)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at junit.framework.TestCase.runTest(TestCase.java:154)
  at junit.framework.TestCase.runBare(TestCase.java:127)
  at junit.framework.TestResult$1.protect(TestResult.java:106)
  at junit.framework.TestResult.runProtected(TestResult.java:124)
  at junit.framework.TestResult.run(TestResult.java:109)
  </code></pre>
  
  <p>It seems <strong>MongoTemplate mongoTemplate = (MongoTemplate) context.getBean("mongoTemplate");</strong> always runs into error. Am I wrong somewhere? </p>
  
  <p>Thanks.</p>