spring生命周期流程图

在这里插入图片描述Spring作为当前Java最流行、最强大的轻量级框架,受到了程序员的热烈欢迎。准确的了解Spring Bean的生命周期是非常必要的。我们通常使用ApplicationContext作为Spring容器。这里,我们讲的也是 ApplicationContext中Bean的生命周期。而实际上BeanFactory也是差不多的,只不过处理器需要手动注册。
转载请注明地址 http://www.cnblogs.com/zrtqsk/p/3735273.html,谢谢。一、生命周期流程图:  Spring Bean的完整生命周期从创建Spring容器开始,直到最终Spring容器销毁Bean,这其中包含了一系列关键点。 若容器注册了以上各种接口,程序那么将会按照以上的流程进行。下面将仔细讲解各接口作用。 二、各种接口方法分类Bean的完整生命周期经历了各种方法调用,这些方法可以划分为以下几类:1、Bean自身的方法  :  这个包括了Bean本身调用的方法和通过配置文件中的init-method和destroy-method指定的方法2、Bean级生命周期接口方法  :  这个包括了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean这些接口的方法3、容器级生命周期接口方法  :  这个包括了InstantiationAwareBeanPostProcessor 和 BeanPostProcessor 这两个接口实现,一般称它们的实现类为“后处理器”。4、工厂后处理器接口方法  :  这个包括了AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer等等非常有用的工厂后处理器  接口的方法。工厂后处理器也是容器级的。在应用上下文装配配置文件之后立即调用。  三、演示我们用一个简单的Spring Bean来演示一下Spring Bean的生命周期。1、首先是一个简单的Spring Bean,调用Bean自身的方法和Bean级生命周期接口方法,为了方便演示,它实现了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean这4个接口,同时有2个方法,对应配置文件中的init-method和destroy-method。如下: 复制代码复制代码 1 package springBeanTest; 2 3 import org.springframework.beans.BeansException; 4 import org.springframework.beans.factory.BeanFactory; 5 import org.springframework.beans.factory.BeanFactoryAware; 6 import org.springframework.beans.factory.BeanNameAware; 7 import org.springframework.beans.factory.DisposableBean; 8 import org.springframework.beans.factory.InitializingBean; 9 10 /**11 * @author qsk12 */13 public class Person implements BeanFactoryAware, BeanNameAware,14 InitializingBean, DisposableBean {15 16 private String name;17 private String address;18 private int phone;19 20 private BeanFactory beanFactory;21 private String beanName;22 23 public Person() {24 System.out.println("【构造器】调用Person的构造器实例化");25 }26 27 public String getName() {28 return name;29 }30 31 public void setName(String name) {32 System.out.println("【注入属性】注入属性name");33 this.name = name;34 }35 36 public String getAddress() {37 return address;38 }39 40 public void setAddress(String address) {41 System.out.println("【注入属性】注入属性address");42 this.address = address;43 }44 45 public int getPhone() {46 return phone;47 }48 49 public void setPhone(int phone) {50 System.out.println("【注入属性】注入属性phone");51 this.phone = phone;52 }53 54 @Override55 public String toString() {56 return “Person [address=” + address + “, name=” + name + “, phone=“57 + phone + “]”;58 }59 60 // 这是BeanFactoryAware接口方法61 @Override62 public void setBeanFactory(BeanFactory arg0) throws BeansException {63 System.out64 .println(”【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()”);65 this.beanFactory = arg0;66 }67 68 // 这是BeanNameAware接口方法69 @Override70 public void setBeanName(String arg0) {71 System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()");72 this.beanName = arg0;73 }74 75 // 这是InitializingBean接口方法76 @Override77 public void afterPropertiesSet() throws Exception {78 System.out79 .println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");80 }81 82 // 这是DiposibleBean接口方法83 @Override84 public void destroy() throws Exception {85 System.out.println("【DiposibleBean接口】调用DiposibleBean.destory()");86 }87 88 // 通过的init-method属性指定的初始化方法89 public void myInit() {90 System.out.println("【init-method】调用的init-method属性指定的初始化方法");91 }92 93 // 通过的destroy-method属性指定的初始化方法94 public void myDestory() {95 System.out.println("【destroy-method】调用的destroy-method属性指定的初始化方法");96 }97 }复制代码复制代码 2、接下来是演示BeanPostProcessor接口的方法,如下:复制代码复制代码 1 package springBeanTest; 2 3 import org.springframework.beans.BeansException; 4 import org.springframework.beans.factory.config.BeanPostProcessor; 5 6 public class MyBeanPostProcessor implements BeanPostProcessor { 7 8 public MyBeanPostProcessor() { 9 super();10 System.out.println(“这是BeanPostProcessor实现类构造器!!”);11 // TODO Auto-generated constructor stub12 }13 14 @Override15 public Object postProcessAfterInitialization(Object arg0, String arg1)16 throws BeansException {17 System.out18 .println(“BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改!”);19 return arg0;20 }21 22 @Override23 public Object postProcessBeforeInitialization(Object arg0, String arg1)24 throws BeansException {25 System.out26 .println(“BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改!”);27 return arg0;28 }29

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值