我有一个 bean mongoService 看起来像
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<context:property-placeholder location="file:///storage//local.properties"/>
<bean id="mongoService" class="com.business.persist.MongoService">
<constructor-arg value="${host}"/>
<constructor-arg value="${port}"/>
<constructor-arg value="${database}"/>
</bean>
</beans>
我需要将这个 bean 包含在一个单独的项目中,所以我为这个项目创建了一个 jar 并添加为 maven 依赖项,看起来像
<dependency> <groupId>com.project</groupId> <artifactId>business</artifactId> <version>master-SNAPSHOT</version> </dependency>
现在在我需要注入此字段的文件中,我执行以下操作
public class DocumentSaver implements IDocumentSaver { @Resource private MongoService mongoService; public boolean addDocument(Document doc) { // do other things // add document to mongo mongoService.putDocument(document); return true; } }
然后我按如下方式运行测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/com/wireup.xml")
public class DocumentSaverTest extends DocumentCase {
@Test
public void loadAndSave() {
DocumentSaver saver = new DocumentSaver();
Document doc = new Document();
// fill the doc
saver.addDocument(doc);
}
}
我看到NullPointerException
当我像这样运行它时saver.addDocument(doc);
请告诉我我做错了什么
谢谢