所以我一直在关注Spring文档,特别是这部分,
关于依赖注入的http://static.springsource.org/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-collaborators,但每当我的代码运行时,我都会收到有关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);
}
答案 0 :(得分:1)
timeFeedHandler
依赖timeBayeuxService
。由于NullPointerException,它无法初始化timeBayeuxService
因此spring无法注入bean。
根据错误消息,com.example.streaming.time.TimeBayeuxService
的构造函数中发生了NullPointerException。你是如何创建common.bayeux
bean的?请粘贴TimeBayeuxService
类的构造函数代码。它是一个界面吗?然后你应该把实现类放在bean定义中。
NPE的可能位置。如果没有完整的堆栈跟踪,我必须猜测以下几行代码:
LogServlet.addLogGatherer(logGatherer); startThread(fbayeux, testMsgTS, true);