gpt4 book ai didi

java - Spring bean创建错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:31:37 25 4
gpt4 key购买 nike

所以我一直在关注 Spring 文档,特别是这一部分,

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-collaborators依赖注入(inject),但每当我的代码运行时,我都会收到有关 bean 创建的错误。

这是我尝试基于 ExampleBean 示例的部分代码,

public class TimeFeedHandler implements MessageListener {

String msgID = null;

TimeBayeuxService timeBayeuxService;

public void setTimeBayeuxService(TimeBayeuxService timeBayeuxService) {
this.timeBayeuxService = timeBayeuxService;
}

我的 Spring XML 文件如下所示,

<!-- A POJO that implements the JMS message listener -->

<bean id="timeFeedHandler" class="com.example.streaming.time.TimeFeedHandler" >
<property name="timeBayeuxService" ref="timeBayeuxService"> </property>
</bean>

我得到的错误是,

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'timeFeedHandler' defined in URL....

你知道我做错了什么或者为什么做错了吗?

编辑,这里是 TimeBayeuxService bean,

<!-- Time BayeuxServices -->

<bean id="timeBayeuxService" class="com.example.streaming.time.TimeBayeuxService" lazy-init="true">
<constructor-arg><ref bean="common.bayeux" /></constructor-arg>
<property name="timeBean" ref="time.time" />
</bean>

这里有更多错误。 STS 的完整错误日志太长了。我觉得我没有正确引用 TimeBayeuxService bean,但从逻辑上讲,我似乎看不出我做错了什么。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'timeFeedHandler' defined in URL [file:/Users/nullpoint/applicationContext.xml]: Cannot resolve reference to bean 'timeBayeuxService' while setting bean property 'timeFeedHandler'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'timeBayeuxService' defined in URL [file:/Users/nullpoint/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.example.streaming.time.TimeBayeuxService]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)

这是 TimeBayeuxService 类的一部分

public class TimeBayeuxService {

private Bayeux bayeux;
private static StreamingTimeLogGatherer logGatherer;
String testMsgTS = "This is a test message from the original service";

public TimeBayeuxService(Bayeux bayeux) extends SomeBayeuxService{
super(bayeux, TimeBayeuxService.class.getName());

this.bayeux = bayeux;
final Bayeux fbayeux = bayeux;

this.logGatherer = logGatherer;
LogServlet.addLogGatherer(logGatherer);

startThread(fbayeux, testMsgTS, true);

}

最佳答案

timeFeedHandler 依赖于 timeBayeuxService。由于NullPointerException,它无法初始化timeBayeuxService,因此spring无法注入(inject)bean。

根据错误信息,在com.example.streaming.time.TimeBayeuxService的构造函数中发生了NullPointerException。您如何创建common.bayeux bean?请粘贴 TimeBayeuxService 类的构造函数代码。是一个接口(interface)吗?然后你应该把实现类放在bean定义中。

NPE 可能出现的位置。如果没有完整的堆栈跟踪,我必须猜测以下代码行:

  1. 它可能发生在父类(super class)的构造函数内。
  2. 这两个方法:LogServlet.addLogGatherer(logGatherer); startThread(fbayeux, testMsgTS, true);

关于java - Spring bean创建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7367816/

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