错误:
19-Feb-2016 00:00:16.731 SEVERE [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException:创建名为'personService'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.springframework.data.mongodb.core.MongoTemplate com.test.app.service.PersonService.mongoTemplate;嵌套异常是org.springframework.beans.factory.BeanCreationException:在ServletContext资源[/WEB-INF/mvc-dispatcher-servlet.xml]中定义名为'mongoTemplate'的bean时出错:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[org.springframework.data.mongodb.core.MongoTemplate]:构造函数抛出异常;嵌套异常是java.lang.NoSuchMethodError:org.springframework.core.convert.support.ConversionServiceFactory.createDefaultConversionService()Lorg / springframework / core / convert / support / GenericConversionService;
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.2.xsd">
<context:component-scan base-package="com.test.app" />
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- Factory bean that creates the Mongo instance -->
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="localhost" />
</bean>
<!-- MongoTemplate for connecting and querying the documents in the database -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="test" />
</bean>
<!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="personController" class="com.test.app.controller.PersonController" />
<bean id="personService" class="com.test.app.service.PersonService" />
<bean id="person" class="com.test.app.model.Person" />
</beans>
我看到很多有关此错误的帖子,但没有解决此问题的答案。
答案 0 :(得分:4)
spring-core的版本需要与spring-data的版本匹配。 下面是我的部分pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- Spring data mongodb -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.8.2.RELEASE</version>
</dependency>
它已修复。
答案 1 :(得分:1)
只有注释驱动才几乎相同。在Supplier
我刚刚投射mongoTemplate
(您可能不希望如此)。所以如果使用spring配置,一切都应该可以正常工作。
关于您的XML版本,请尝试使用mongo factory
和SimpleMongoDbFactory
创建MongoClient
和数据库名称,然后将您的MongoTemplate
bean设为工厂,并且&# 39;是的。
Spring Config:
@Configuration
public class SpringConfig {
@Bean
public MongoDbFactory mongoDbFactory() throws UnknownHostException{
return new SimpleMongoDbFactory(new MongoClient(),"games");
}
@Bean
public MongoTemplate mongoTemplate() throws UnknownHostException{
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
return mongoTemplate;
}
}
之后我创建了supplier
(在这种情况下为Google Guava
),在您的情况下可能不需要
public class MongoOperationSupplier implements Supplier<MongoOperations>{
@Override
public MongoOperations get() {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
return mongoOperation;
}
}
带注释的课程:
@Document(collection="game_suspicious_event")
public class SuspiciousEvent {
@Id
private String id;
private String apiKey;
private String userUniqueId;
private Date date;
private String ip;
private String additionalInformation;
public SuspiciousEvent(){}
public SuspiciousEvent(String apiKey,String userUniqueId,Date date, String ip, String additionalInformation){
this.apiKey=apiKey;
this.userUniqueId=userUniqueId;
this.date=date;
this.ip=ip;
this.additionalInformation=additionalInformation;
}
public String getApiKey() {
return apiKey;
}
}
然后使用:
final MongoOperations mo = new MongoOperationSupplier().get();
mo.save(new SuspiciousEvent(......));
希望它有所帮助。