1

这是 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

这意味着HALBrunPojonull。调试后我发现如果我注释了LifeCycleBeanP2in的 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 ");
    }
}

我究竟做错了什么?

4

1 回答 1

0

实现 BeanPostProcessor 接口的 bean 是一个特殊的 bean。它传递了对上下文创建的所有其他 bean 的引用,它可以修改它们并在需要时返回不同的实例

BeanPostProcessor 返回的任何引用都将与 bean id 相关联。

在您的 LifeCycle 类中,您返回 null - 相反,您应该返回对传递给您的方法的对象的引用。如下更改这两种方法,它应该可以按预期工作。

   // method of BeanPostProcessor
    @Override
    public Object postProcessAfterInitialization(Object arg0, String arg1)
            throws BeansException {
        System.out.println(" POST PROCESS AFTER INITIALISATION ");
        return arg0;
    }

    // method of BeanPostProcessor
    @Override
    public Object postProcessBeforeInitialization(Object arg0, String arg1)
            throws BeansException {
        System.out.println(" POST PROCESS BEFORE INITIALISATION ");
        return arg0;
    }
于 2012-05-10T17:31:53.203 回答