在spring容器中,原有错误的配置:
<mongo:db-factory id="mongoDbFactory" host="${mongo.host}" port="${mongo.port}" dbname="${mongo.dbname}" /> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> </bean> <bean id="mongoManager" class="com.gemantic.analyse.mongodb.util.MongoManager"> <property name="mongoTemplate" ref="mongoTemplate" /> </bean>
该错误,主要是因为在生成mongoTemplate的bean实例过程中,构造函数的定义出现了问题。错误如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoTemplate' defined in URL [file:/D:/eclipse-workspace/gemantic-3.7/analyse-mongodb-service/target/classes/META-INF/mongo/applicationContext-server.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.data.mongodb.MongoDbFactory]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:716)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:196)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1002)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:906)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:484)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.apache.tuscany.sca.implementation.spring.SpringImplementationProvider.start(SpringImplementationProvider.java:110)
at org.apache.tuscany.sca.core.assembly.CompositeActivatorImpl.start(CompositeActivatorImpl.java:644)
at org.apache.tuscany.sca.core.assembly.CompositeActivatorImpl.start(CompositeActivatorImpl.java:560)
at org.apache.tuscany.sca.node.impl.NodeImpl.start(NodeImpl.java:668)
at com.gemantic.analyse.mongodb.server.Server.start(Server.java:30)
at com.gemantic.analyse.mongodb.server.Server.main(Server.java:57)
错误说明,在创建mongoTemplate的bean实例时,构造函数的类型定义不匹配造成的。从spring-db-mongodb的api中对构造函数的定义是这样的:
Constructors
Constructor and Description
MongoTemplate(MongoDbFactory mongoDbFactory)
Constructor used for a basic template configuration
MongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter)
Constructor used for a basic template configuration.
MongoTemplate(Mongo mongo, String databaseName)
Constructor used for a basic template configuration
MongoTemplate(Mongo mongo, String databaseName, org.springframework.data.authentication.UserCredentials userCredentials)
Constructor used for a template configuration with user credentials in the form of UserCredentials
若采用MongoTemplate(MongoDbFactory mongoDbFactory)的构造函数,容器的配置写法是这样的:
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg index="0" ref="mongoDbFactory" /> </bean>
这样就对了!