RKodakandla
RKodakandla

Reputation: 3484

Spring bean injection throwing null pointer exception

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>

Upvotes: 3

Views: 18329

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279890

Spring has to initialize your object before it performs its property setting (setter injection). Initialization means invoking the constructor. So Spring first invokes

public ServiceBeanBImpl(){
    initializeBeanB();
}

which invokes

initializeBeanB(){
    a = serviceBeanA.getA(); /// this is the line that throws Null pointer exception
    b = serviceBeanA.getB();
}

But property injection has not happened yet and serviceBeanA still hasn't been initialized (it's null).

Consider refactoring to use constructor injection

public ServiceBeanBImpl(ServiceBeanA serviceBeanA) {
    this.serviceBeanA = serviceBeanA;
    // initializeBeanB();
}

You could also take advantage of @PostConstruct and let Spring invoke that after all injections have occurred.

@PostConstruct
void initializeBeanB() {
    ...
}

Upvotes: 15

Related Questions