【问题标题】:Constructor Dependency Injection using Spring framework, unable to retain the value使用Spring框架的构造函数依赖注入,无法保留值
【发布时间】:2012-05-10 13:56:07
【问题描述】:

这是bean-jojo.xml的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 中所示,我指定了构造函数参数, 现在这是下面使用的具有主要方法的代码:

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。调试后发现如果在bean-jojo.xml中注释了LifeCycleBeanP2的bean声明,像这样:

        <!--
        <bean id="beanLifeCycleTest" class="com.jal.LifeCycleBeanP2">
            <property name="justAParam" value="MICHEAL JACKSON" />
        </bean>     
        -->

然后我运行程序,我得到了想要的输出,即

PARAM-VALUE 234

为什么会这样?,这是豆子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 ");
    }
}

我做错了什么?

【问题讨论】:

    标签: java xml spring javabeans


    【解决方案1】:

    实现 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多