3

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>
4
  • Which object that is being dereferenced is null? Commented Sep 3, 2014 at 18:13
  • @RobertHarvey serviceBeanA is null Commented Sep 3, 2014 at 18:13
  • 2
    Did you call setServiceBeanA() with a valid ServiceBeanAImpl object parameter, before executing initializeBeanB()? 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. Commented Sep 3, 2014 at 18:15
  • @RobertHarvey.. thaks.. I've decided to use constructor-arg Commented Sep 3, 2014 at 18:30

1 Answer 1

15

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() {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.