我有一个项目使用spring框架和mongodb,但是现在我成功加载mongoTemplate时遇到了问题。
这是我的 pom.xml:
<properties>
...
<org.springframework.version>4.0.5.RELEASE</org.springframework.version>
<jdk.version>1.7</jdk.version>
<spring.mongodb.version>1.5.1.RELEASE</spring.mongodb.version>
<mongo.java.driver>2.9.3</mongo.java.driver>
...
</properties>
<!-- mongo -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>${mongo.java.driver}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>${spring.mongodb.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
这是spring-mongo-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- Default bean name is 'mongo' -->
<mongo:mongo host="localhost" port="27017">
<!-- OPTIONAL: configure <mongo:options /> -->
</mongo:mongo>
<mongo:db-factory dbname="test" mongo-ref="mongo"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
</beans>
配置完成后,我写了一个 junit 测试用例来预先测试它,但是它总是失败。这是测试用例,
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");
}
}
错误消息如下所示,
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)
看来MongoTemplate mongoTemplate = (MongoTemplate) context.getBean("mongoTemplate"); 总是会出错。我在某个地方错了吗?
谢谢。