我正在尝试使用带弹簧的Mongo模板,但它提供 InvalidDataAccessApiUsageException
我的主要课程是
package com.spring.mongodb.main;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.spring.mongodb.model.Person;
public class SpringDataMongoDBMain {
public static final String DB_NAME = "mydb";
public static final String PERSON_COLLECTION = "mycol";
public static final String MONGO_HOST = "localhost";
public static final int MONGO_PORT = 27017;
public static void main(String[] args) {
try{
Mongo mongo = new MongoClient(MONGO_HOST, MONGO_PORT);
System.out.println("Connected to MongoDB");
MongoOperations mongoOps = new MongoTemplate(mongo, DB_NAME);
System.out.println("Connected to database");
Person p = new Person("100", "ABC", "GRG PQR");
mongoOps.createCollection(PERSON_COLLECTION);
mongoOps.insert(p, PERSON_COLLECTION);
}catch(Exception e){
e.printStackTrace();
}
}
}
我的模型类是:
package com.spring.mongodb.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "person")
public class Person {
@Id
private String id;
private String name;
private String address;
public Person() {
}
public Person(String i, String n, String a) {
this.id = i;
this.name = n;
this.address = a;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return id + "::" + name + "::" + address;
}
}
我的Spring.xml是
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd">
<mongo:mongo host="localhost" port="27017" id="mongo" />
<mongo:db-factory dbname="mydb" mongo-ref="mongo"
id="mongoDbFactory" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
<bean id="personDAO" class="com.journaldev.spring.mongodb.dao.PersonDAOImpl">
<constructor-arg name="mongoOps" ref="mongoTemplate" />
</bean>
</beans>
当我运行上面的代码时,会出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/dao/InvalidDataAccessApiUsageException
at com.spring.mongodb.main.SpringDataMongoDBMain.main(SpringDataMongoDBMain.java:21)
Caused by: java.lang.ClassNotFoundException: org.springframework.dao.InvalidDataAccessApiUsageException
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 1 more
我添加了以下罐子:
mongodb-driver-3.1.1.jar
mongodb-driver-core-3.1.1.jar
spring-core-4.2.3.RELEASE.jar
spring-data-mongodb-1.6.1.RELEASE.jar
spring-data-commons-core-1.4.1.RELEASE.jar
我尝试在MongoOperations对象创建时终止显示错误的代码并终止。我做错了什么?
答案 0 :(得分:0)
你错过了一些罐子(spring-tx,spring-beans,spring-context,spring-expression等等。)
看一下maven来管理你的依赖