Spring中bean的生命周期

关于bean的生命周期,其实过程非常多,总结起来就是在构造方法前面有其他方法可以执行,在属性设置前面,后面都也可以插入具体的方法执行,最后在关闭掉ApplicationContext的实现类时,也会有方法执行。先给出一个简易版的,仅在一个bean内需要调用的顺序

public class Person implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean {
   private String name;
   private String address;
   private int phone;
   private BeanFactory beanFactory;
   private String beanName;
   public Person() {
       System.out.println("【构造方法】");
   }
   public String getName() {
       return name;
   }

   public void setName(String name) {
       System.out.println("【注入属性】注入name属性");
       this.name = name;
   }

   public String getAddress(){
       return address;
   }
   public void setAddress(String address) {
       System.out.println("【注入属性】注入address属性");
       this.address = address;
   }

   public int getPhone(){
       return phone;
   }
   public void setPhone(int phone) {
       System.out.println("【注入属性】注入phone属性"+phone);
       this.phone = phone;
   }
   public String toString() {
       return "Person [address=" + address + ", name=" + name + ", phone="
               + phone + "]";
   }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory("+beanFactory.getClass()+")");
        this.beanFactory = beanFactory;
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()");
        this.beanName = name;
    }


    @Override
    public void destroy() throws Exception {
        System.out.println("【DiposibleBean接口】调用DiposibleBean.destory()");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");
    }

    public void myInit() {
        System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
    }

    public void myDestory() {
        System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
    }
}

接下来配置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" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">


    <bean id="person" class="com.zubh.test.framework.spring.beanlifetime.Person" init-method="myInit"
          destroy-method="myDestory" scope="singleton" p:name="张三" p:address="广州"
          p:phone="15900000" />

</beans>

测试类

public class BeanLifeCycle {
    public static void main(String[] args) {
        System.out.println("--------------------------------------------");
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        System.out.println("--------------------------------------------");
        Person person = (Person)context.getBean("person",Person.class);
        System.out.println(person);
        System.out.println("--------------------------------------------");
        ((ClassPathXmlApplicationContext)context).registerShutdownHook();
    }
}

执行结果

【构造方法】
【注入属性】注入address属性
【注入属性】注入name属性
【注入属性】注入phone属性15900000
【BeanNameAware接口】调用BeanNameAware.setBeanName()
【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory(class org.springframework.beans.factory.support.DefaultListableBeanFactory)
【InitializingBean接口】调用InitializingBean.afterPropertiesSet()
【init-method】调用<bean>的init-method属性指定的初始化方法
--------------------------------------------
Person [address=广州, name=张三, phone=15900000]
--------------------------------------------
【DiposibleBean接口】调用DiposibleBean.destory()
【destroy-method】调用<bean>的destroy-method属性指定的初始化方法

总结下,bean的生命周期

1、执行构造方法

2、设置属性值

3、如果实现beanNameAware接口,执行setBeanName方法

4、如果实现了BeanFactoryAware接口,执行setBeanFactory方法

5、如果实现了InitializingBean接口,执行afterPropertiesSet接口

6、最后调用设置的init方法

---------------------------------

关闭ApplicationContext时,如果bean实现了DiposibleBean接口,调用destory方法

最后执行设置的desotry方法

总结:网上关于bean生命周期中每个步骤调用过程非常详细,其实这个大家平时开发时候用的到不多,主要是面试,但是这个过程太tm长了,我真是背不会,如果面试的时候,说个大概就行,谁天天背作文一样,背这些东西。

如果想了解详细过程可以看我下面的参考文章。

参考:Spring Bean的生命周期(非常详细)

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值