2016-02-18 72 views
0

錯誤:無法在春天創造mongotemplate

19-Feb-2016 00:00:16.731 SEVERE [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.data.mongodb.core.MongoTemplate com.test.app.service.PersonService.mongoTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.core.convert.support.ConversionServiceFactory.createDefaultConversionService()Lorg/springframework/core/convert/support/GenericConversionService;

<beans xmlns="http://www.springframework.org/schema/beans" 
 
\t xmlns:context="http://www.springframework.org/schema/context" 
 
\t 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" 
 
\t 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/> 
 
\t <bean 
 
\t \t class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
 
\t \t <property name="prefix"> 
 
\t \t \t <value>/WEB-INF/</value> 
 
\t \t </property> 
 
\t \t <property name="suffix"> 
 
\t \t \t <value>.jsp</value> 
 
\t \t </property> 
 
\t </bean> 
 
<!-- Factory bean that creates the Mongo instance --> 
 
\t <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean"> 
 
\t \t <property name="host" value="localhost" /> 
 
\t </bean> 
 
\t 
 
\t 
 
\t <!-- MongoTemplate for connecting and querying the documents in the database --> 
 
\t <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
 
\t \t <constructor-arg name="mongo" ref="mongo" /> 
 
\t \t <constructor-arg name="databaseName" value="test" /> 
 
\t </bean> 
 

 
\t <!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes --> 
 
\t <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 
 
\t 
 
\t <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" /> 
 
\t 
 
\t 
 
</beans>

我看到關於此錯誤的許多職位,但沒有答案,解決這個問題。

回答

0

嗨,也許這是有用的它幾乎相同的東西只有註釋驅動。在供應商我只是鑄造mongoTemplate(你可能不希望這樣)。所以如果你使用spring配置,一切都應該正常工作

關於你在XML中的版本,嘗試用SimpleMongoDbFactory和MongoClient以及數據庫名稱創建mongo工廠,然後把你的MongoTempleate bean放到你的工廠,就是這樣。

Spring配置

@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; 
    } 

} 

之後,我ccreated供應商(谷歌番石榴在這種CAS),這可能不是你的情況

public class MongoOperationSupplier implements Supplier<MongoOperations>{ 
    @Override 
    public MongoOperations get() { 
     ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); 
     MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate"); 
     return mongoOperation; 
    } 
} 

的anotated

@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(......)); 

希望它有幫助

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> 

它是固定的。

+0

這似乎適用於我 - 版本匹配似乎很愚蠢,但它的工作原理。令人沮喪 - 但謝謝! – Alin