2013-04-23 54 views
9

當我在Spring中編寫代碼時,我總是使用註釋,並且在涉及到基於xml的配置時總是卡住。現在,我陷入了一個可能非常簡單的問題。如果你能幫我解決這個問題,我會很感激。需要幫助在Spring/Maven上配置MongoDB

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in ServletContext resource [/WEB-INF/spring-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.IllegalAccessError: tried to access method org.springframework.core.GenericTypeResolver.getTypeVariableMap(Ljava/lang/Class;)Ljava/util/Map; from class org.springframework.data.util.ClassTypeInformation 
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:281) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:984) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:888) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:479) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423) 
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:272) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:196) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:166) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:722) 
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.IllegalAccessError: tried to access method org.springframework.core.GenericTypeResolver.getTypeVariableMap(Ljava/lang/Class;)Ljava/util/Map; from class org.springframework.data.util.ClassTypeInformation 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141) 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:107) 
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:273) 
    ... 24 more 
Caused by: java.lang.IllegalAccessError: tried to access method org.springframework.core.GenericTypeResolver.getTypeVariableMap(Ljava/lang/Class;)Ljava/util/Map; from class org.springframework.data.util.ClassTypeInformation 
    at org.springframework.data.util.ClassTypeInformation.<init>(ClassTypeInformation.java:96) 
    at org.springframework.data.util.ClassTypeInformation.<clinit>(ClassTypeInformation.java:42) 
    at org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper.<clinit>(DefaultMongoTypeMapper.java:45) 
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.<init>(MappingMongoConverter.java:107) 
    at org.springframework.data.mongodb.core.MongoTemplate.getDefaultMongoConverter(MongoTemplate.java:1691) 
    at org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:189) 
    at org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:175) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525) 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126) 
    ... 26 more 

爲spring-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    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/data/mongo 
     http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
    <context:annotation-config /> 

    <context:component-scan base-package="co.jobstix.controller"> 
     <context:include-filter type="annotation" 
      expression="org.springframework.stereotype.Controller" /> 
    </context:component-scan> 

    <context:component-scan base-package="co.jobstix.service"> 
     <context:include-filter type="annotation" 
      expression="org.springframework.stereotype.Service" /> 
    </context:component-scan> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/view/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 

    <mongo:mongo host="127.0.0.1" port="27017" /> 
    <mongo:db-factory dbname="jobstix" /> 

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
     <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> 
    </bean> 
</beans> 

SpringMongoConfig.java

@Configuration 
public class SpringMongoConfig { 

    public @Bean 
    MongoDbFactory mongoDbFactory() throws Exception { 
     return new SimpleMongoDbFactory(new MongoClient(), "yourdb"); 
    } 

    public @Bean 
    MongoTemplate mongoTemplate() throws Exception { 
     MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory()); 
     return mongoTemplate; 
    } 
} 

有一個偉大的一天!

+0

請提供錯誤日誌還。 – 2015-12-13 14:27:15

回答

8

編譯代碼(不涉及反射)中的IllegalAccessError通常意味着版本不匹配:客戶端代碼是針對成員具有更寬容的訪問修飾符的依賴版本編譯的。更新你的Maven依賴關係。

(因爲這是一個非常古老的疑問,得到了最近碰到:參與都已經過時,應該升級爲現代版本的版本,這應該可以解決這個問題。)

+0

,或者彈簧罐自身存在版本不匹配(彈簧庫的不同版本的混合)。沒有朋友,我們無法知道。 – eis 2015-12-12 22:58:58

+1

@eis我要說的是,*一些*依賴不匹配,更新一切都應該工作。 – chrylis 2015-12-13 00:14:07

3
!. Changes in Xml 

    spring-servlet.xml you can also change name with spring-servlet.xml to yourProjectName-servlet.xml 

     http://www.springframework.org/schema/data/mongo 
      http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd 

      change to 1.1 

        http://www.springframework.org/schema/data/mongo 
     http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd 



    <mongo:mongo host="localhost" port="27017"> 

    <mongo:options connections-per-host="100" 
    threads-allowed-to-block-for-connection-multiplier="5" 
      max-wait-time="120000000" 
      connect-timeout="10000000" 
      socket-keep-alive="true" 
      socket-timeout="15000000" 
      auto-connect-retry="true"/> 

    </mongo:mongo> 


    <mongo:db-factory dbname="jobstix" 
     mongo-ref="mongo" /> 
    <bean id="mongoTypeMapper" 
     class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper"> 
     <constructor-arg name="typeKey"> 
      <null /> 
     </constructor-arg> 
    </bean> 
    <bean id="mongoMappingContext" 
     class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" /> 
    <bean id="mongoConverter" 
     class="org.springframework.data.mongodb.core.convert.MappingMongoConverter"> 
     <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> 
     <constructor-arg name="mappingContext" ref="mongoMappingContext" /> 
     <property name="typeMapper" ref="mongoTypeMapper"></property> 
    </bean> 
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
     <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> 
     <constructor-arg name="mongoConverter" ref="mongoConverter" /> 
     <property name="writeResultChecking" value="EXCEPTION" /> 
    </bean> 

2. Put Four Jars OR Get From Maven 

/Test/WebContent/WEB-INF/lib/mongo-java-driver-2.10.1.jar 
/Test/WebContent/WEB-INF/lib/spring-data-commons-1.5.1.RELEASE.jar 
/Test/WebContent/WEB-INF/lib/spring-data-commons-core-1.3.1.RELEASE.jar 
/Test/WebContent/WEB-INF/lib/spring-data-mongodb-1.3.1.RELEASE.jar 

3. In Dao/Service/Controller you can call these connection to Autowired 

    @Autowired 
    private MongoTemplate mongoTemplate; 

    @Autowired 
    private MongoOperations mongoOperation;