I'm pretty new to the whole idea of injecting beans, so speak slowly. :)
I've got a class that injects a bean, but when the property is accessed, the property is null and I get a null-pointer exception.
From /project-TRUNK/war-module/src/main/webapp/WEB-INF/spring-config/spring-bean.xml
<bean id="linkCheck"
class="com.dogwatch.util.LinkCheck">
<property name="linkDao" ref="jdbcLinkDao" />
</bean>
From /project-TRUNK/war-module/src/main/webapp/WEB-INF/spring-config/spring-dao.xml
<bean id="jdbcLinkDao" class="com.dogwatch.util.jdbcLinkDao">
<property name="dataSource" ref="dataSource" />
<property name="linkJdbcDataTypesSupport" ref="linkJdbcDataTypesSupport"/>
</bean>
The DAO bean is known to be good and works in several other classes.
package com.dogwatch.util;
public class 开发者_运维问答LinkCheck {
private LinkDAO linkDao;
public LinkDAO getLinkDao() {
return linkDao;
}
public void setLinkDao(LinkDAO linkDao) {
this. linkDao = linkDao;
}
}
I've been comparing it to other classes that use the same DAO bean and I can't find any differences.
I do see the bean getting defined in:
INFO [2010-01-15 01:10:05,838] [main] [XmlBeanDefinitionReader] [XmlBeanDefinitionReader.java:323] - Loading XML bean definitions from URL [file:war-module/src/main/webapp/WEB-INF/spring-config/spring-dao.xml] INFO [2010-01-15 01:10:05,858] [main] [XmlBeanDefinitionReader] [XmlBeanDefinitionReader.java:323] - Loading XML bean definitions from URL [file:war-module/src/main/webapp/WEB-INF/spring-config/spring-bean.xml] INFO [2010-01-15 01:10:06,597] [main] [DefaultListableBeanFactory] [DefaultListableBeanFactory.java:414] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3c01d5a0: defining beans [(snip) jdbcLinkDao, linkCheck(snip)businessLoggingAspect,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#1]; root of factory hierarchy
Does anyone have any suggestions on how to go about troubleshooting bean injection?
I'm expanding my comment from the OP's question since it appears to have helped...
We're still missing some information, such as how does the code that uses the linkCheck bean get it? Is it also injected? Do you retrieve it from the Spring context? I'm assuming you're not doing new LinkCheck()
, but since you're new to Spring, it's a sensible starting question.
Spring can only inject the beans if it controls the creation of the beans: if you do new MyClass()
then the Spring container doesn't know about the object and can't do the injection.
There is a potential problem in that the type of 'jdbcLinkDao' is 'jdbcLinkDao' but the argument type of the setter is LinkDAO. (If 'jdbcLinkDao' is a subtype of 'LinkDAO' that is probably OK ... but FIX THE CLASS NAME!)
If that's not the problem I suggest that you:
set the log level to DEBUG,
if that doesn't show anything, add a trace print to
LinkCheck.setLinkDAO()
to see what value is being set ... and when it is being set.
精彩评论