这是 XML 文件bean-jojo.xml
:
<?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="MICHEAL JACKSON" />
</bean>
<bean id="brun-p" class="com.justPractise.ex01.HALBrunPojo">
<constructor-arg value="234" />
</bean>
</beans>
我已经在以下 bean 上注册,我正在尝试构造函数 DI:
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;
}
}
正如在上述 bean 声明的 XML 中所见,我已经指定了构造函数参数,现在这是使用的以下代码,它具有 main 方法:
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();
}
}
}
当我尝试从命令行运行上述类时,输出是
PARAM-VALUE 0
这意味着HALBrunPojo
是null
。调试后我发现如果我注释了LifeCycleBeanP2
in的 bean 声明bean-jojo.xml
,像这样:
<!--
<bean id="beanLifeCycleTest" class="com.jal.LifeCycleBeanP2">
<property name="justAParam" value="MICHEAL JACKSON" />
</bean>
-->
然后我运行程序,我得到想要的输出,即
PARAM-VALUE 234
为什么会这样?,这是 bean 的代码LifeCycleBeanP2.java
:
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 ");
}
}
我究竟做错了什么?