Spring中bean的生命周期

来源:互联网 发布:网达软件 编辑:程序博客网 时间:2024/05/02 00:03



根据上面的图片流程进行代码验证了一下:applicationContext.xml(我取的另一个名字:beans.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">      <bean id="personServer" init-method="myInit"  destroy-method="myDestroy" scope="prototype"  class="com.iflytek.person.PersonServer">        <property name="name" value="大锤" />        <property  name="age" value="22"></property>              </bean>      <!--注入前置和后置处类-->      <bean id="myBeanPostProcessor"  class="com.iflytek.person.MyBeanPostProcessor"/>           </beans>Server类:public class PersonServer implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean{    private String name;        private int age;        //业务逻辑方法    public void output()    {        System.out.println(name + "=================" + age);    }        //无参构造函数    public PersonServer()    {        System.out.println("PersonServer初始化");    }        //有参构造函数    public PersonServer(int age, String name)    {        this.name = name;        this.age = age;            }        public String getName()    {        return name;    }        //打印出set值    public void setName(String name)    {        System.out.println("name开始赋值:" + name);        this.name = name;    }        public int getAge()    {        return age;    }        public void setAge(int age)    {        this.age = age;    }        //获取bean的id    @Override    public void setBeanName(String arg0)    {                System.out.println("bean 的 id 开始赋值:" + arg0);    }        //获取BeanFactory的对象    @Override    public void setBeanFactory(BeanFactory arg0)        throws BeansException    {        System.out.println("beanFactory 的id 开始赋值:" + arg0);            }        //获取容器对象    @Override    public void setApplicationContext(ApplicationContext applicationContext)        throws BeansException    {                System.err.println("applicationContext :" + applicationContext);    }        //在执行postProcessAfterInitialization的方法之前调用    @Override    public void afterPropertiesSet()        throws Exception    {                System.out.println("InitializingBean=======================================");            }        //在postProcessAfterInitialization和postProcessBeforeInitialization之间调用    public void myInit()    {        System.out.println("myInit()的方法");    }        //自定义销毁方法    public void myDestroy()    {        System.out.println("销毁方法");    }    }前置处理和后置处理类:public class MyBeanPostProcessor implements BeanPostProcessor{        @Override    public Object postProcessAfterInitialization(Object arg0, String arg1)        throws BeansException    {        System.out.println("postProcessAfterInitialization:after");        return arg0;    }        @Override    public Object postProcessBeforeInitialization(Object arg0, String arg1)        throws BeansException    {        System.out.println("postProcessBeforeInitialization:before");        //System.out.println(arg0 + "时间:" + new Date() + "   类名:" + arg0.getClass().getName());        // TODO Auto-generated method stub        return arg0;    }    }//Main方法public class TestApp{        /**     * main:(这里用一句话描述这个方法的作用). <br/>     * @author syzhao     * @param args     * @since JDK 1.6     */    public static void main(String[] args)    {        //BeanFactory context = new XmlBeanFactory(new ClassPathResource("com/iflytek/person/beans.xml"));        ApplicationContext context = new ClassPathXmlApplicationContext("com/iflytek/person/beans.xml");        PersonServer person = (PersonServer)context.getBean("personServer");        person.output();    }    }//打印出的结果Person初始化name开始赋值:大锤bean 的 id 开始赋值:personServerbeanFactory 的id 开始赋值:org.springframework.beans.factory.support.DefaultListableBeanFactory@b307f0: defining beans [personServer,myBeanPostProcessor]; root of factory hierarchyapplicationContext :org.springframework.context.support.ClassPathXmlApplicationContext@ece65: startup date [Sun Nov 01 20:58:56 CST 2015]; root of context hierarchypostProcessBeforeInitialization:beforeInitializingBean=======================================myInit()的方法postProcessAfterInitialization:after大锤=================22



0 0
原创粉丝点击