Java 带弹簧的MongoDb工厂
我想将MongoDB与Spring一起使用。 我正试图在我的DAO主类中注入MongoDbFactory 我不想使用MongoTemplate,因为我需要使用MongoDB驱动程序 当我尝试运行jUnit测试时,为了测试我的DAO类,我在工厂中得到了一个NullPointerExection 我想这是我注射的问题 我的应用程序配置:Java 带弹簧的MongoDb工厂,java,spring,mongodb,spring-mvc,dependency-injection,Java,Spring,Mongodb,Spring Mvc,Dependency Injection,我想将MongoDB与Spring一起使用。 我正试图在我的DAO主类中注入MongoDbFactory 我不想使用MongoTemplate,因为我需要使用MongoDB驱动程序 当我尝试运行jUnit测试时,为了测试我的DAO类,我在工厂中得到了一个NullPointerExection 我想这是我注射的问题 我的应用程序配置: <bean id="mongoFactoryBean" class="org.springframework.data.mongodb.core.MongoF
<bean id="mongoFactoryBean" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="127.0.0.1" />
<property name="port" value="27017"/>
</bean>
<bean id="mongoDbFactory"
class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
<constructor-arg name="mongo" ref="mongoFactoryBean" />
<constructor-arg name="databaseName" value="agence_voyage" />
</bean>
<bean id="dao" class="dao.daoImpl.DaoImpl">
<property name="mongoFactory" ref="mongoDbFactory" />
</bean>
public class DaoImpl implements Dao {
private MongoDbFactory mongoFactory;
private DB db;
@Required
public void setMongoFactory(MongoDbFactory myMongoFactory){
this.mongoFactory= myMongoFactory;
}
//TODO development mongodb://localhost
public void connect() throws UnknownHostException {
try{
this.db = mongoFactory.getDb("agence_voyage"); //NullPointer here
}
catch(DataAccessException d){
System.out.println(d);
}
}
public int getVoyageCount(String collection) {
DBCollection col = db.getCollection(collection);
return (int) col.count();
}
}
public class TestDao {
@Test
public void test() {
Dao test = new DaoImpl();
try {
test.connect();
} catch (UnknownHostException e) {
System.out.println(e);
assertTrue(false);
}
assertTrue(test.getVoyageCount("voyage")== 1);
}
}
然后是我的小测试:
<bean id="mongoFactoryBean" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="127.0.0.1" />
<property name="port" value="27017"/>
</bean>
<bean id="mongoDbFactory"
class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
<constructor-arg name="mongo" ref="mongoFactoryBean" />
<constructor-arg name="databaseName" value="agence_voyage" />
</bean>
<bean id="dao" class="dao.daoImpl.DaoImpl">
<property name="mongoFactory" ref="mongoDbFactory" />
</bean>
public class DaoImpl implements Dao {
private MongoDbFactory mongoFactory;
private DB db;
@Required
public void setMongoFactory(MongoDbFactory myMongoFactory){
this.mongoFactory= myMongoFactory;
}
//TODO development mongodb://localhost
public void connect() throws UnknownHostException {
try{
this.db = mongoFactory.getDb("agence_voyage"); //NullPointer here
}
catch(DataAccessException d){
System.out.println(d);
}
}
public int getVoyageCount(String collection) {
DBCollection col = db.getCollection(collection);
return (int) col.count();
}
}
public class TestDao {
@Test
public void test() {
Dao test = new DaoImpl();
try {
test.connect();
} catch (UnknownHostException e) {
System.out.println(e);
assertTrue(false);
}
assertTrue(test.getVoyageCount("voyage")== 1);
}
}
你有什么解决办法吗?
我肯定这是个愚蠢的错误,但我没发现
提前谢谢 您的测试似乎没有以任何方式连接到Spring上下文。您实际上是自己创建了对象,而不是获得Spring管理的bean
Dao test = new DaoImpl();
为什么Spring会对这个对象做任何事情
将这些注释添加到类中
@ContextConfiguration(locations = "yourfile.xml")
@RunWith(SpringJUnit4ClassRunner.class)
并直接注入DaoImpl
bean
@Autowired
private Dao test;
然后在测试中使用它
阅读Spring文档中的。您的测试似乎没有以任何方式连接到Spring上下文。您实际上是自己创建了对象,而不是获得Spring管理的bean
Dao test = new DaoImpl();
为什么Spring会对这个对象做任何事情
将这些注释添加到类中
@ContextConfiguration(locations = "yourfile.xml")
@RunWith(SpringJUnit4ClassRunner.class)
并直接注入DaoImpl
bean
@Autowired
private Dao test;
然后在测试中使用它
请阅读Spring文档中的。好的,您知道我需要导入哪些依赖项吗,因为我在SpringJUnit4Clas上遇到错误。。。而且@ContextConfiguration似乎不存在。@user2323241它们是
spring测试的一部分。你可以得到它。好的,你知道我需要导入哪些依赖项吗,因为我在SpringJUnit4Clas上有错误。。。而且@ContextConfiguration似乎不存在。@user2323241它们是spring测试的一部分。你可以得到它。