上一博客主要学习了下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. 01-从零玩转JavaWeb-面向过程与面向对象

    配套视频讲解:面向过程面向对象 一.面向过程 所有事情都按顺序一件一件来执行.   二.面向对象 面向对象是将功能通过对象也实现,将功能封装进对象之中 让对象去实现具体的细节   三.面向对象的目的 ...

  2. 《mysql必知必会》读书笔记--存储过程的使用

    以前对mysql的认识与应用只是停留在增删改查的阶段,最近正好在学习mysql相关内容,看了一本书叫做<MySQL必知必会>,看了之后对MySQL的高级用法有了一定的了解.以下内容只当读书 ...

  3. 如何在 Apple TV 上使用描述文件

    您可以使用 OS X 上的 Apple Configurator 在 Apple TV 上安装配置描述文件. 开始前,请在 Mac 上安装最新版本的 Apple Configurator(如果尚未安装 ...

  4. ubuntu 常用命令集

    一.安装的时候,让你输入代替root用户的名称与密码 使用sudo root切换root的时候会要求你输入密码,这时候你输入什么都不对的 要想使用的哈,需要给root设置密码,命令如下: sudo p ...

  5. Windows 安装 python2.7

    Windows 安装 python2.7 python2.7下载地址: https://www.python.org/downloads/release/python-2714/ 安装过程: 设置系统 ...

  6. KMP (next数组的性质及证明)

    性质:如果len%(len-next[len-1])==0,则字符串中必存在最小循环节,且循环次数即为len/(len-next[len-1]); 证明:在前len个字符组成的字符串,存在最小循环节k ...

  7. 磁盘管理 之 parted命令添加swap,文件系统

    第1章 磁盘管理 1.1 必须要了解的. 1.1.1 ps aux 命令中 RSS 与VSZ的含义 rss 进程占用的物理内存的大小 单位:kb : vsz 进程占用的虚拟的内存大小(物理内存+swa ...

  8. git使用教程之了解git

    1.理解git快照 快照的作用就相当于将旧文件所占的空间保留下来,并且保存一个引用,而新文件中会继续使用与旧文件内容相同部分的磁盘空间,不同部分则写入新的磁盘空间.git有一个HEAD指针,这个指针可 ...

  9. 记Javascript的编写方式的全新学习

    前言 这次有幸参与前端的工作,对于前端开发学习了不少新知识,在此记录一下相比之前,完全不同的Javascript编写方式. 原来的编写方式 之前也是写过Javascript,就是常见的.js 文件写函 ...

  10. js 函数声明和函数表达式

    在ECMAScript中,创建函数的最常用的两个方法是函数表达式和函数声明,因为ECMA规范只明确了一点:函数声明必须带有标示符(Identifier)(就是大家常说的函数名称),而函数表达式则可以省 ...