gpt4 book ai didi

java - Spring Bean : Doesn't seems to be wired up

转载 作者:行者123 更新时间:2023-11-30 04:44:46 25 4
gpt4 key购买 nike

我的wireup.xml看起来像

<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-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<util:properties id="mongoProperties" location="file:///storage//local.properties" />

<bean id="mongoService" class="com.business.persist.MongoService" init-method="init"></bean>
</beans>

storage//local.properties

### === MongoDB interaction === ###
host="127.0.0.1"
port=27017
database=contract

Bean 类 MongoService

@Service
public class MongoService {

@Value("#{mongoProperties['host']}")
private String host;

@Value("#{mongoProperties['port']}")
private int port;

@Value("#{mongoProperties['database']}")
private String database;

private Mongo mongo;

private static final Logger LOGGER = LoggerFactory.getLogger(MongoService.class);

public MongoService() {}

public void init() throws UnknownHostException {
LOGGER.info("host=" + host + ", port=" + port + ", database=" + database);
mongo = new Mongo(host, port);
}

public void putDocument(@Nonnull final DBObject document) {
LOGGER.info("inserting document - " + document.toString());
mongo.getDB(database).getCollection(getCollectionName(document)).insert(document, WriteConcern.SAFE);
}

public void putDocuments(@Nonnull final List<DBObject> documents) {
for (final DBObject document : documents) {
putDocument(document);
}
}

我在MongoServiceTest中测试这个类为

public class MongoServiceTest {
@Autowired
private MongoService mongoService;


@Test
public void testMongoService() {
final DBObject document = DBContract.getUniqueQuery("001");
document.put(DBContract.R_VARIABLES, "values");
document.put(DBContract.P_VARIABLES, "values");

mongoService.putDocument(document);
}

当我运行这个时,我遇到了NullpointerException

10:24:23.956 [main] INFO  c.s.sparrow.business.util.MongoRule -  Setting up Mongo Database
10:24:23.963 [main] INFO c.s.sparrow.business.util.MongoRule - Shutting down the Mongo Database

java.lang.NullPointerException
at com.business.persist.MongoServiceTest.testMongoService(MongoServiceTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
at org.junit.rules.RunRules.evaluate(RunRules.java:18)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

看起来接线实际上并没有创建一个 bean 并调用 init 方法

问题

我该如何解决这个问题并使其正常工作?

谢谢

最佳答案

使用@ContextConfiguration

@ContextConfiguration
public class MongoServiceTest {
@Resource
private MongoService mongoService;

完整的测试类应该如下所示

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public final class MongoServiceTest {

@Resource
private MongoServiceservice;

@Test
public void testServiceName() {//...
}

关于java - Spring Bean : Doesn't seems to be wired up,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11298323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com