I'm trying to inject beanB into beanA, but for some reason, spring injection isn't working. When I try to use beanA in beanB, I'm getting null pointer exception.
Here is my configuration...
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/application-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans" .........>
<import resource="dao-beans.xml"/>
<import resource="service-beans.xml"/>
...................
...................
</beans>
service-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans".........>
<bean id="serviceBeanA" class="com.test.ServiceBeanAImpl"/>
<bean id="serviceBeanB" class="com.my.kodak.ServiceBeanBImpl">
<property name="serviceBeanA" ref="serviceBeanA"></property>
</bean>
</beans>
ServiceBeanBImpl.java
public class ServiceBeanBImpl{
private ServiceBeanAImpl serviceBeanA;
private String a;
private String b;
public ServiceBeanBImpl(){
initializeBeanB();
}
initializeBeanB(){
a = serviceBeanA.getA(); /// this is the line that throws Null pointer exception
b = serviceBeanA.getB();
}
public String getServiceBeanA(){
return this.serviceBeanA;
}
public String setServiceBeanA(ServiceBeanAImpl serviceBeanA){
this.serviceBeanA = serviceBeanA;
}
}
When spring tries to initialize ServiceBeanBImpl, it is failing on the call to serviceBeanA. I've set a debug point on setServiceBeanA() method and it never gets called. I've also tried different ref strategies, but none of them worked..
<bean id="serviceBeanB" class="com.my.kodak.ServiceBeanBImpl">
<property name="serviceBeanA">
<ref bean="serviceBeanA"/>
</property>
</bean>
<bean id="serviceBeanB" class="com.my.kodak.ServiceBeanBImpl">
<property name="serviceBeanA">
<ref local="serviceBeanA"/>
</property>
</bean>
setServiceBeanA()with a validServiceBeanAImplobject parameter, before executinginitializeBeanB()? You said you set a breakpoint that never gets hit. Your remedy is to call the method so that it has a valid object. Figure out why Spring isn't performing the injection. PROTIP: This should probably be constructor injection, not method injection.