创建mongoTemplate时如何解决此错误?

时间:2019-11-06 00:42:33

标签: spring mongodb spring-data spring-data-mongodb

我正在使用Spring 5.2.0.RELEASE,Spring Mongo 2.2.0.RELEASE和Mongo Java Driver 3.11.1。

有什么想法吗?

这是我的appContext-persistence.xml,其中包含mongo客户端,数据库工厂和模板。

模板是有问题的模板。

    <mongo:mongo-client replica-set="${mongo.replica.set}" host="${mongo.host}" port="${mongo.port}" id="mongoClient">
        <mongo:client-options connections-per-host="${mongo.local.connections.per.host}"
                              threads-allowed-to-block-for-connection-multiplier="4"
                              connect-timeout="${mongo.connection.timeout}" max-wait-time="${mongo.max.wait.time}"
                              socket-keep-alive="true" socket-timeout="1500"/>
    </mongo:mongo-client>

    <mongo:db-factory dbname="${mongo.dbname}" mongo-ref="mongoClient" id="mongoDbFactory"/>

    <mongo:template id="mongoTemplate" converter-ref="converter" db-factory-ref="mongoDbFactory" write-concern="JOURNAL_SAFE"/>

如果问题是某些版本不匹配,这里是xml顶部引用的xsds。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-2.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd

我也尝试过用这种方式创建Mongo模板:

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

这是部署时的错误。

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate': Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.beans.factory.BeanFactory.getBeanProvider(Ljava/lang/Class;)Lorg/springframework/beans/factory/ObjectProvider;
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1136)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
        ... 28 more
    Caused by: java.lang.NoSuchMethodError: org.springframework.beans.factory.BeanFactory.getBeanProvider(Ljava/lang/Class;)Lorg/springframework/beans/factory/ObjectProvider;
        at org.springframework.data.mapping.callback.EntityCallbackDiscoverer$CallbackRetriever.discoverEntityCallbacks(EntityCallbackDiscoverer.java:396)
        at org.springframework.data.mapping.callback.EntityCallbackDiscoverer.setBeanFactory(EntityCallbackDiscoverer.java:301)
        at org.springframework.data.mapping.callback.EntityCallbackDiscoverer.<init>(EntityCallbackDiscoverer.java:70)
        at org.springframework.data.mapping.callback.DefaultEntityCallbacks.<init>(DefaultEntityCallbacks.java:59)
        at org.springframework.data.mapping.callback.EntityCallbacks.create(EntityCallbacks.java:85)
        at org.springframework.data.mongodb.core.MongoTemplate.setApplicationContext(MongoTemplate.java:357)
        at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:121)
        at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:97)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1623)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        ... 37 more

以下是我正在使用的Spring和其他Spring模块的版本:

    <springframework.version>5.2.0.RELEASE</springframework.version>
    <spring-security.version>4.2.11.RELEASE</spring-security.version>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-messaging</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring-security.version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring-security.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${springframework.version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>org.apache.commons</groupId>
                <artifactId>com.springsource.org.apache.commons.logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${springframework.version}</version>
    </dependency>

0 个答案:

没有答案