上一博客主要学习了下bean的配置、注入、自定义属性编辑器,今天来熟悉bean的生命周期。在开发中生命周期是一个很常见的名词,基本每种编程语言都能找到与它关联的。关于bean的生命周期我在网上也找了好多,基本都差不多。这里我主要是想通过代码来验证,毕竟学的知识都是一样的,都是学的Java,最重要的是动手练习,这样印象更深。下面是它生命周期的描述,我们通过demo来进行验证。下图是它执行的顺序。

一、创建LiftCycle类实现BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean,ApplicationContextAware5个接口方法

package Cuiyw.Spring.Service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; public class LifeCycle implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean,ApplicationContextAware{ private String name;
public String getName() {
System.out.println("getName name="+name);
return name;
}
public void setName(String name) {
System.out.println("setName name="+name);
this.name = name;
}
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("InitializingBean.afterPropertiesSet()");
}
public void setBeanName(String arg0) {
// TODO Auto-generated method stub
System.out.println("BeanNameAware.setBeanName"); }
public void setBeanFactory(BeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub
System.out.println("BeanFactoryAware.setBeanFactory");
}
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("DisposableBean.destroy");
}
public void myInit() {
System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
} public void myDestory() {
System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
}
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
// TODO Auto-generated method stub
System.out.println("ApplicationContextAware.setApplicationContext");
} }

二、注册InstantiationAwareBeanPostProcessor接口

package Cuiyw.Spring.Service;

import java.beans.PropertyDescriptor;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor{ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInitialization");
return bean;
} public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInitialization");
return bean;
} public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation");
return true;
} public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation");
return null;
} public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("InstantiationAwareBeanPostProcessor.postProcessPropertyValues");
return pvs;
} }

三、注册BeanPostProcessor接口

其实InstantiationAwareBeanPostProcessor继承BeanPostProcessor,所以在上面我也实现了BeanPostProcessor接口的方法

package Cuiyw.Spring.Service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("BeanPostProcessor.postProcessAfterInitialization ");
return bean;
} public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("BeanPostProcessor.postProcessBeforeInitialization");
return bean;
} }

四、注册BeanFactoryPostProcessor接口

package Cuiyw.Spring.Service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub
System.out.println("BeanFactoryPostProcessor.postProcessBeanFactory");
}
}

五、在上下文中配置

这里还是在上一个博客demo的基础上进行修改,为了有其他干扰,我先把service去掉了。

<?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="beanPostProcessor" class="Cuiyw.Spring.Service.MyBeanPostProcessor"></bean>
<bean id="instantiationAwareBeanPostProcessor" class="Cuiyw.Spring.Service.MyInstantiationAwareBeanPostProcessor"></bean>
<bean id="beanFactoryPostProcessor" class="Cuiyw.Spring.Service.MyBeanFactoryPostProcessor"></bean>
<bean id="lifeCycle" class="Cuiyw.Spring.Service.LifeCycle" init-method="myInit" destroy-method="myDestory">
<property name="name" value="cuiyw1"></property>
</bean>
</beans>

六、在main中使用bean

package Cuiyw.SpringAop;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import Cuiyw.Spring.IService.IService;
import Cuiyw.Spring.Service.LifeCycle; public class App
{
public static void main( String[] args )
{
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"ApplicationContext.xml"});
BeanFactory factory=context;
LifeCycle lifeCycle=factory.getBean("lifeCycle",LifeCycle.class);
lifeCycle.setName("cuiyw2");
System.out.println("lifeCycle.name="+lifeCycle.getName());
((ClassPathXmlApplicationContext)factory).registerShutdownHook(); /*service=(IService)factory.getBean("ServiceA");
service.service("Cuiyw ServiceA");
service=(IService)factory.getBean("ServiceImpl");
service.service("Cuiyw ServiceImpl"); */
} }

七、输入打印结果

可以发现输出的顺序和上面图的顺序基本一致。

Spring之bean二生命周期的更多相关文章

  1. 一次性讲清楚spring中bean的生命周期之二:FactoryBean的前世今生

    前言 在<spring中FactoryBean是什么bean>一文中,带着小伙伴学习了spring中的FactoryBean,了解了到了FactoryBean其实是一种生产Bean的bea ...

  2. 深究Spring中Bean的生命周期

    前言 这其实是一道面试题,是我在面试百度的时候被问到的,当时没有答出来(因为自己真的很菜),后来在网上寻找答案,看到也是一头雾水,直到看到了<Spring in action>这本书,书上 ...

  3. 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章

    前言 当你准备去复习Spring中Bean的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...

  4. 一次性讲清楚spring中bean的生命周期之三:bean是如何实例化的

    在前面的两篇博文<一次性讲清楚spring中bean的生命周期之一:getSingleton方法>和<一次性讲清楚spring中bean的生命周期之二:FactoryBean的前世今 ...

  5. JAVA面试题:Spring中bean的生命周期

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  6. 深入理解Spring中bean的生命周期

    [Spring中bean的生命周期] bean的生命周期 1.以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期: (1).生命周期图: (2).具体事例 ...

  7. Spring中Bean的生命周期及其扩展点

    原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6106456.html Spring中Bean的管理是其最基本的功能,根据下面的图来了解Spr ...

  8. 简:Spring中Bean的生命周期及代码示例

    (重要:spring bean的生命周期. spring的bean周期,装配.看过spring 源码吗?(把容器启动过程说了一遍,xml解析,bean装载,bean缓存等)) 完整的生命周期概述(牢记 ...

  9. 面试Spring之bean的生命周期

    找工作的时候有些人会被问道Spring中Bean的生命周期,其实也就是考察一下对Spring是否熟悉,工作中很少用到其中的内容,那我们简单看一下. 在说明前可以思考一下Servlet的生命周期:实例化 ...

随机推荐

  1. require.js实现js模块化编程(二):RequireJS Optimizer

    require.js实现js模块化编程(二):RequireJS Optimizer 这一节,我们主要学习一下require.js所提供的一个优化工具r.js的用法. 1.认识RequireJS Op ...

  2. Oracle存储过程跨用户执行查询报错

    在Oracle中,在USERA下编写一个存储过程,该存储过程中引用了另一个用户USERB下的表或视图对象.编译该存储过程,出现编译错误.报ORA-00942: table or view does n ...

  3. 版本控制之一:SVN服务器搭建与安装(转)

    Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下载地址: http:// ...

  4. session设置过期的方法(转载)

    这篇文章主要介绍了php中实现精确设置session过期时间的方法,需要的朋友可以参考下   大多数据情况下我们对于session过期时间使用的是默认设置的时间,而对于一些有特殊要求的情况下我们可以设 ...

  5. 2017 ICPC 广西邀请赛1005 CS Course

    CS Course Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. undefined 与null的区别与差异

    typeof null  ---> object 运行结果:

  7. Linux系列教程(十九)——Linux文件系统管理之手工分区

    上篇博客我们首先介绍了硬盘为什么要分区,以及Linux系统的几种分区类型,然后介绍了Linux系统几个常用的文件系统命令,最后讲解了挂载命令,并通过实例演示了如何挂载光盘和U盘. 本篇博客我们将介绍l ...

  8. java 冒泡排序与选择排序

    //冒泡排序 package test;public class Maopaosort {   public static void method(){  int[] arr=new int[10]; ...

  9. JSTL中foreach与fn表达式

    在jstl中的fn标签也是我们在网页设计中经常要用到的很关键的标签,在使用的时候要先加上头 <%@ taglib uri=" http://java.sun.com/jsp/jstl/ ...

  10. 前端面试题(3) cookie,sessionStorage和localStorage的区别

    cookie是网站为了标示用户身份存在用户本地终端上的数据(经过加密). cookie数据时钟在同源的http请求中携带(即使不需要),即会在浏览器和服务器之间传递. seeeionStorage和l ...