Constructor Dependency Injection using Spring framework, unable to retain the value

phiptubrift

New Member
This is the XML file \[code\]bean-jojo.xml\[/code\]:\[code\]<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="vTypeCar" class="com.justPractise.ex01.Car" /> <bean id="vTypeBike" class="com.justPractise.ex01.MotorCycle" /> <bean id="vService" class="com.justPractise.ex01.VehicalService"> <property name="vehical" ref="vTypeBike" /> </bean> <bean id="beanLifeCycleTest" class="com.jal.LifeCycleBeanP2"> <property name="justAParam" value="http://stackoverflow.com/questions/10535526/MICHEAL JACKSON" /> </bean> <bean id="brun-p" class="com.justPractise.ex01.HALBrunPojo"> <constructor-arg value="http://stackoverflow.com/questions/10535526/234" /> </bean> </beans>\[/code\]I've registered on the following bean, on which I am trying constructor DI:\[code\]package com.justPractise.ex01;public class HALBrunPojo { private Integer num; public HALBrunPojo(){ System.out.println(this.getClass().getName()+" INITIALISED "); } public HALBrunPojo(int num){ System.out.println(this.getClass().getName()+" PARAMETERISED..... INITIALISED "+num); this.num = num; } public Integer getNum() { return num; } public void setNum(Integer num) { this.num = num; }}\[/code\]As seen in the XML for the above bean declaration, I have specified the constructor-arg,now this is the following code used which has the main method:\[code\]package com.justPractise.ex01;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainPractise01 { /** * @param args */ public static void main(String[] args) { ApplicationContext ctx = null; HALBrunPojo obj = null; try{ ctx = new ClassPathXmlApplicationContext("bean-jojo.xml"); obj = (HALBrunPojo) ctx.getBean("brun-p"); System.out.println(" PARAM-VALUE "+(obj == null ? "0" : obj.getNum())); }catch(Exception e){ e.printStackTrace(); } }}\[/code\]When I try to run the above class from the command line, the output is\[code\]PARAM-VALUE 0\[/code\]That means \[code\]HALBrunPojo\[/code\] is \[code\]null\[/code\]. After debugging I found out that if I commented the bean declaration of \[code\]LifeCycleBeanP2\[/code\] in \[code\]bean-jojo.xml\[/code\], like this:\[code\] <!-- <bean id="beanLifeCycleTest" class="com.jal.LifeCycleBeanP2"> <property name="justAParam" value="http://stackoverflow.com/questions/10535526/MICHEAL JACKSON" /> </bean> -->\[/code\]and then I run the program, I get desired output, i.e \[code\]PARAM-VALUE 234\[/code\]Why so?, this is the code of the bean \[code\]LifeCycleBeanP2.java\[/code\]:\[code\]package com.jal;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.BeanFactoryAware;import org.springframework.beans.factory.BeanNameAware;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.factory.config.BeanPostProcessor;public class LifeCycleBeanP2 implements BeanNameAware, BeanFactoryAware, BeanPostProcessor, InitializingBean, DisposableBean{ String justAParam; public LifeCycleBeanP2(){ System.out.println(this.getClass().getName()+" INITIALISED...... "); } public String getJustAParam() { return justAParam; } public void setJustAParam(String justAParam) { System.out.println(" SETTER OF JustAParam ........ "); this.justAParam = justAParam; } // method of BeanNameAware @Override public void setBeanName(String arg0) { System.out.println(" IN SET BEAN-NAME "+arg0); } // method of BeanFactoryAware @Override public void setBeanFactory(BeanFactory arg0) throws BeansException { System.out.println(" IN SET BEAN-FACTORY, "+arg0.getClass().getName()); } // method of BeanPostProcessor @Override public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { System.out.println(" POST PROCESS AFTER INITIALISATION "); return null; } // method of BeanPostProcessor @Override public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { System.out.println(" POST PROCESS BEFORE INITIALISATION "); return null; } //method of InitializingBean @Override public void afterPropertiesSet() throws Exception { System.out.println(" AFTER PROPERTIES SET "); } //method of DisposableBean @Override public void destroy() throws Exception { System.out.println(" BEAN "+this.getClass().getName()+" QUALIFIED FOR GARBAGE COLLECTION "); }}\[/code\]What am I doing wrong?
 
Back
Top