Bean生命周期的几个知识点

Spring IOC 容器可以管理 Bean 的生命周期, Spring 允许在 Bean 生命周期的特定点执行定制的任务. 
Spring IOC 容器对 Bean 的生命周期进行管理的过程:
通过构造器或工厂方法创建 Bean 实例
为 Bean 的属性设置值和对其他 Bean 的引用
调用 Bean 的初始化方法
Bean 可以使用了
当容器关闭时, 调用 Bean 的销毁方法

在 Bean 的声明里设置 init-method 和 destroy-method 属性, 为 Bean 指定初始化和销毁方法.


附上个人demo

[html]  view plain  copy
 print ?
  1. <span style="font-size:24px;"><bean id"student" class="Spring19.Student" init-method="init" destroy-method="destory">  
  2.         <property name="name" value="Sam"></property>  
  3. </bean></span>  

[html]  view plain  copy
 print ?
  1. package Spring19;  
  2.   
  3. public class Student {  
  4.     private String name;  
  5.   
  6.     public String getName() {  
  7.         return name;  
  8.     }  
  9.   
  10.     public void setName(String name) {  
  11.         this.name = name;  
  12.     }  
  13.   
  14.     @Override  
  15.     public String toString() {  
  16.         return "Student [name=" + name + "]";  
  17.     }  
  18.     public void init(){  
  19.         System.out.println("Student init...");  
  20.     }  
  21.     public void destory(){  
  22.         System.out.println("Student destory...");  
  23.     }  
  24.     public Student() {  
  25.         System.out.println("Student construtor....");  
  26.     }  
  27.   
  28.     public Student(String name) {  
  29.         super();  
  30.         this.name = name;  
  31.         System.out.println("Student construtor....");  
  32.     }  
  33.       
  34. }  



Spring IOC 容器对 Bean 的生命周期进行管理的过程:
通过构造器或工厂方法创建 Bean 实例
为 Bean 的属性设置值和对其他 Bean 的引用
将 Bean 实例传递给 Bean 后置处理器的 postProcessBeforeInitialization 方法
调用 Bean 的初始化方法
将 Bean 实例传递给 Bean 后置处理器的 postProcessAfterInitialization方法
Bean 可以使用了
当容器关闭时, 调用 Bean 的销毁方法


以下附上接口实现

[html]  view plain  copy
 print ?
  1. package Spring19;  
  2.   
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.beans.factory.config.BeanPostProcessor;  
  5.   
  6. public class testBeanPostProcess implements BeanPostProcessor {  
  7.   
  8.     public Object postProcessAfterInitialization(Object arg0, String arg1)  
  9.             throws BeansException {  
  10.         System.out.println("postProcessAfterInitialization..and bean is "+arg0+ " bean name is "+ arg1 );  
  11.         return arg0;  
  12.     }  
  13.   
  14.     public Object postProcessBeforeInitialization(Object arg0, String arg1)  
  15.             throws BeansException {  
  16.         System.out.println("postProcessBeforeInitialization..and bean is "+arg0+ " bean name is "+ arg1 );        
  17.         return arg0;  
  18.     }  
  19.       
  20. }<strong style="color: rgb(0, 0, 153);">  
  21. </strong>  



<strong><span style="font-size:32px;color:#ff0000;"><bean class="Spring19.testBeanPostProcess"></bean></span></strong>


运行结果:

Student construtor....
postProcessBeforeInitialization..and bean is Student [name=Sam] bean name is student
Student init...
postProcessAfterInitialization..and bean is Student [name=Sam] bean name is student
Student [name=Sam]
Student destory...

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值