Spring bean injection throwing null pointer exception -


i'm trying inject beanb beana, reason, spring injection isn't working. when try use beana in beanb, i'm getting null pointer exception.

here 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(){         = servicebeana.geta(); /// line throws null pointer exception         b = servicebeana.getb();     }      public string getservicebeana(){         return this.servicebeana;     }      public string setservicebeana(servicebeanaimpl servicebeana){         this.servicebeana = servicebeana;     }  } 

when spring tries initialize servicebeanbimpl, failing on call servicebeana. i've set debug point on setservicebeana() method , never gets called. i've tried different ref strategies, 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> 

spring has initialize object before performs property setting (setter injection). initialization means invoking constructor. spring first invokes

public servicebeanbimpl(){     initializebeanb(); } 

which invokes

initializebeanb(){     = servicebeana.geta(); /// line throws null pointer exception     b = servicebeana.getb(); } 

but property injection has not happened yet , servicebeana still hasn't been initialized (it's null).

consider refactoring use constructor injection

public servicebeanbimpl(servicebeana servicebeana) {     this.servicebeana = servicebeana;     // initializebeanb(); } 

you take advantage of @postconstruct , let spring invoke after injections have occurred.

@postconstruct void initializebeanb() {     ... } 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -