当前位置: 首页 > 知识库问答 >
问题:

在spring中创建mongoTemplate bean时获取java.lang.NosuchFielderRror:实例

罗业
2023-03-14

创建mongoTemplate bean时出现以下错误。

我认为jar版本是冲突的。

org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.NoSuchFieldError: INSTANCE
        org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:164)
        org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:125)
        org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:270)
        org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1114)
        org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1017)
        org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
        org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
        org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
        org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
        org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
        org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
        org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
        org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
        org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
        org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
        org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
        org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
        org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
        org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
        org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
        javax.servlet.GenericServlet.init(GenericServlet.java:158)
        org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
        org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
        org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
        org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2500)
        org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2489)
        java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.lang.Thread.run(Thread.java:745)
    
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.websystique.springmvc</groupId>
    <artifactId>JioAsp</artifactId>
    <packaging>war</packaging>
    <version>1.0.0</version>
    <name>JioAsp Maven Webapp</name>

    <properties>
        <springframework.version>4.0.3.RELEASE</springframework.version>
        <jackson.library>2.7.5</jackson.library>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</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-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.library}</version>
        </dependency>
            <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>1.5.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-commons</artifactId>
    <version>1.12.6.RELEASE</version>
        </dependency>
        <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-commons-core</artifactId>
    <version>1.4.1.RELEASE</version>
</dependency>
        <dependency>
 <groupId>cglib</groupId>
 <artifactId>cglib</artifactId>
 <version>2.2</version>
 </dependency>

        <!-- dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>${jackson.library}</version>
        </dependency-->
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <warSourceDirectory>src/main/webapp</warSourceDirectory>
                        <warName>JioAsp</warName>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                        <outputDirectory>D:\apache-tomcat-8.0.33\webapps\</outputDirectory>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <finalName>JioAsp</finalName>
    </build>
</project>
<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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/data/mongo
">
<!-- http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
 --><context:component-scan base-package="com.websystique.springmvc.controller" />
<context:component-scan base-package="com.websystique.springmvc.service" />
     <!-- Configuration defining views files -->


 <!-- <mongo:mongo host="localhost" port="27017" id="mongo" />
    <mongo:db-factory dbname="aadhaar" mongo-ref="mongo" id="mongoDbFactory" />

     <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean> 
 -->
    <!-- <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo" />
        <constructor-arg name="databaseName" value="ganesh" />
    </bean> -->

    <!-- <bean id="personDAO" class="com.springjdbc.mongojdbctemplateclass.MongoDBJDBCTemplate">
        <constructor-arg name="mongoOps" ref="mongoTemplate"/>
    </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="aadhaar" />
</bean> 
</beans>

共有1个答案

任长卿
2023-03-14

Spring data mongodb 1.5.0使用mongo java驱动程序2.13.3,而您用mongo java驱动程序3.4.1重写它。在pom.xml中找到已解决的依赖项并删除冲突,这将解决问题。

 类似资料:
  • 我有一个表,其中包含。 我还有一个硬编码表,其中我已经为每个国家/地区创建了一个区域,国家/地区是主键。 我想加入

  • 我无法在/users/{uname}/token上获取令牌- 错误:提供给sendToDevice()的注册令牌必须是非空字符串或非空数组。在FirebaseMessagingeror。FirebaseError[作为构造函数](/srv/node_modules/firebase admin/lib/utils/error.js:42:28)位于FirebaseMessagingError。在新

  • 作为大多数Spring Boot新用户,我对@Autow的问题:D 我已经在这里阅读了大量有关此注释的主题,但仍然无法找到解决问题的正确方法。 假设我们有这个Spring Boot层次结构: 类,我们希望在每次调用时实例化它: Out get controller,它会在每个请求中创建新的SomeRepo对象: 最后,使用@Autowired创建TestWire实例的类: 假设我们多次向“/”发出

  • 我制作了一个应用程序,它将获取数据库的列名,并在运行时基于数据库的列名创建一个jlabel和jtextfield。 下面是代码片段:我在这里使用Netbeans。。。 这将是示例输出。 引用JTextFields的行。。 但是,我无法获取在jtextfields中输入的值。我试过使用下面的代码。但运气不好。感谢您的帮助。

  • 当我试图创建枚举时,我的EclipseIDE不断给出错误:java类型。伊奥。无法解析ObjectInputStream。它是从required间接引用的。类文件。

  • 请帮我解决这个问题。