Spring中bean的生命周期



根据上面的图片流程进行代码验证了一下:

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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

      <bean id="personServer" init-method="myInit"  destroy-method="myDestroy" scope="prototype"  class="com.zsy.person.PersonServer">
        <property name="name" value="大锤" />
        <property  name="age" value="22"></property>        
      </bean>
      
<!--注入前置和后置处类-->
      <bean id="myBeanPostProcessor"  class="com.zsy.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/zsy/person/beans.xml"));
        ApplicationContext context = new ClassPathXmlApplicationContext("com/zsy/person/beans.xml");
        PersonServer person = (PersonServer)context.getBean("personServer");
        person.output();
    }
    
}


//打印出的结果
Person初始化
name开始赋值:大锤
bean 的 id 开始赋值:personServer
beanFactory 的id 开始赋值:org.springframework.beans.factory.support.DefaultListableBeanFactory@b307f0: defining beans [personServer,myBeanPostProcessor]; root of factory hierarchy
applicationContext :org.springframework.context.support.ClassPathXmlApplicationContext@ece65: startup date [Sun Nov 01 20:58:56 CST 2015]; root of context hierarchy
postProcessBeforeInitialization:before
InitializingBean=======================================
myInit()的方法
postProcessAfterInitialization:after
大锤=================22



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值