MongoOperations保存方法,给出NoSuchMethodError异常

时间:2016-11-16 11:04:00

标签: java mongodb java-8 mongodb-query spring-data-mongodb

我有一个使用Spring Data MongoDB和MongoDB驱动程序的简单java应用程序。 我只是将一个新用户添加到MongoDB集合中。但是当MongoOperations的save方法执行时,它会产生NoSuchMethodError。

这是我的pom.xml的依赖部分:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.***</groupId>
  <artifactId>m1</artifactId>
  <packaging>war</packaging>
  <version>0</version>
  <name>M1 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
        <spring.version>4.1.5.RELEASE</spring.version>
        <springsecurity.version>3.2.0.RELEASE</springsecurity.version>
        <log4j.version>1.2.17</log4j.version>
        <commons.fileupload.version>1.3.1</commons.fileupload.version>
        <commons.io.version>2.4</commons.io.version>
        <springsocial.version>1.1.0.RELEASE</springsocial.version> 
    </properties>
  <dependencies>
    <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
        </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
            <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.6.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.2.6.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.0.1.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>4.2.6.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>4.2.6.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.6.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.2</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.messaging.saaj</groupId>
            <artifactId>saaj-impl</artifactId>
            <version>1.3.21</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- EXCEL -->

        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp</artifactId>
            <version>0.8.0.RELEASE</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.53</version>
        </dependency>
    <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>3.3</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20160212</version>
    </dependency>
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>jsr250-api</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.9.4.RELEASE</version>
    </dependency>



  </dependencies>
  <build>
    <finalName>m1</finalName>
  </build>
</project>

这是我的MongoConfig类

    import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import com.mongodb.MongoClient;

@Configuration
public class MongoConfig {

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

    public @Bean
    MongoTemplate mongoTemplate() throws Exception {

        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());

        return mongoTemplate;

    }

}

这是我的DBOperations类,我在执行save()方法时遇到错误:

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import com.bng.monitor.config.MongoConfig;
import com.bng.monitor.model.User;
//import org.springframework.context.support.GenericXmlApplicationContext;

public class DbOperations {

    public static void main(String[] args) {

    // For XML
    //ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");

    // For Annotation
    ApplicationContext ctx =
             new AnnotationConfigApplicationContext(MongoConfig.class);
    MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

    User user = new User("kv", "password123");

    // save
    mongoOperation.save(user);

    // now user object got the created id.
    System.out.println("1. user : " + user);

    // query to search user
    Query searchUserQuery = new Query(Criteria.where("username").is("kv"));

    // find the saved user again.
    User savedUser = mongoOperation.findOne(searchUserQuery, User.class);
    System.out.println("2. find - savedUser : " + savedUser);

    // update password
    mongoOperation.updateFirst(searchUserQuery,
                         Update.update("password", "new password"),User.class);

    // find the updated user object
    User updatedUser = mongoOperation.findOne(searchUserQuery, User.class);

    System.out.println("3. updatedUser : " + updatedUser);

    // delete
    mongoOperation.remove(searchUserQuery, User.class);

    // List, it should be empty now.
    List<User> listUser = mongoOperation.findAll(User.class);
    System.out.println("4. Number of user = " + listUser.size());

    }

}

这是我的模特课:

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "users")
public class User {

    @Id
    private String id;

    private String username;

    private String password;

    public User(String username, String password){
        super();
        this.username = username;
        this.password = password;
    }

    public void setId(String id){
        this.id = id;
    }

    public String getId(){
        return this.id;
    }

    public void setUsername(String username){
        this.username = username;
    }

    public String getUsername(){
        return this.username;
    }

    public void sePassword(String password){
        this.password = password;
    }

    public String gePassword(){
        return this.password;
    }
}

这是异常堆栈跟踪:

  

340 [main] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext - 刷新org.springframework.context.annotation.AnnotationConfigApplicationContext@380fb434:启动日期[Wed Nov 16 16:15:28 IST 2016];上下文层次结构的根   线程&#34; main&#34;中的例外情况java.lang.NoSuchMethodError:org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation(Ljava / lang / reflect / AnnotatedElement; Ljava / lang / Class;)Ljava / lang / annotation / Annotation;       在org.springframework.data.mapping.model.BasicPersistentEntity.findAnnotation(BasicPersistentEntity.java:371)       在org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity。(BasicMongoPersistentEntity.java:80)       在org.springframework.data.mongodb.core.mapping.MongoMappingContext.createPersistentEntity(MongoMappingContext.java:91)       在org.springframework.data.mongodb.core.mapping.MongoMappingContext.createPersistentEntity(MongoMappingContext.java:39)       在org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:309)       在org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:180)       在org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:140)       在org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:67)       在org.springframework.data.mongodb.core.MongoTemplate.determineCollectionName(MongoTemplate.java:2015)       在org.springframework.data.mongodb.core.MongoTemplate.determineEntityCollectionName(MongoTemplate.java:2002)       在org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:949)       在com.bng.monitor.app.DbOperations.main(DbOperations.java:32)

任何人都可以帮忙,我在做什么错?

1 个答案:

答案 0 :(得分:3)

您的问题与您使用spring-data-mongodb 1.9.4.RELEASE Spring 4.2.8.RELEASE时使用4.1.5.RELEASE Spring这样的事实有关如果版本为spring-data-mongodb,则只需将Spring的版本降级为1.8.x,因为它取决于4.1.x {{1}}(您的目标版本)。