bean 的生命周期分为:一个是ApplicationContext的容器的bean的生命周期,另一个是BeanFactory容器的生命周期。

首先介绍一下:ApplicationContext的容器的bean的生命周期:

一共13步步骤如下:

Instaniate--->Populate properties--->BeanNameAware's  setBeanName--->BeanFactoryAware's setBeanFactory-->ApplicationContextAware's setApplicationContext-->pre-Initialization BeanPostProcessor --->InitializingBean's afterPropertiesSet--->call custom init-method-->post-Initialization BeanPostProcessor-->bean is ready to use-->container is shutdown-->DisposableBean's destory-->call custom destory-method.

上述文字的图如下所示:

其次介绍一下:BeanFactory的bean的生命周期,它的生命周期只是比ApplicationContext的bean的生命周期少了三步:一步是:ApplicationContextAware,另外两步是:

BeanPostProcessor的两步。如下图所示:

代码的目录结构如下:

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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentService" class="com.qls.beanlife2.StudentService" >
<property name="name" value="熊二"/>
</bean>
<bean id="myBeanPostProcessor" class="com.qls.beanlife2.MyBeanPostProcessor"/>
</beans>

StudentService的代码如下:

 package com.qls.beanlife2;

 import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; /**
* Created by ${秦林森} on 2017/6/6.
*/
public class StudentService implements BeanNameAware ,BeanFactoryAware,ApplicationContextAware,InitializingBean,DisposableBean{
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
System.out.println("2.Populate Properties");
} public StudentService() {
System.out.println("1.Instantiate");
} @Override
public void setBeanName(String name) {
System.out.println("3.BeanNameAware's setBeanName the bean name is :"+name);
} @Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("4.BeanFactoryAware's setBeanFactory the bean factory name is: "+beanFactory);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
System.out.println("5.ApplicationContextAware's setApplicationContext the applicationContext is: "+applicationContext);
} @Override
public void afterPropertiesSet() throws Exception {
System.out.println("7.InitializingBean's afterPropertiesSet ");
}
public void hello(){
System.out.println("hello");
} @Override
public void destroy() throws Exception {
System.out.println("destroy");
}
public void myDestroy(){
System.out.println("my destroy");
}
}

MyBeanPostProcessor的代码如下:

package com.qls.beanlife2;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; /**
* Created by ${秦林森} on 2017/6/6.
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("6.pre-initialization BeanPostProcessor");
return beanName;
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return beanName;
}
}

StudentTest的代码如下:

package com.qls.beanlife2;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource; /**
* Created by ${秦林森} on 2017/6/6.
*/
public class StudentTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/qls/beanlife2/beans.xml");//这个是测试ApplicationContext的bean的生命周期
/*
、//这个是测试BeanFactory的bean的生命周期。
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/qls/beanlife2/beans.xml"));
StudentService studentService= (StudentService) factory.getBean("studentService");
studentService.hello();*/
}
}

spring in action 学习笔记四:bean的生命周期的更多相关文章

  1. 微信小程序学习笔记四 页面的生命周期

    1. 生命周期 1.1 对应阶段说明 onLOad(Object query) 1.1 页面加载时触发, 一个页面只会调用一次, 可以在 onLoad的参数中获取打开当前页面路径中的参数 1.2 参数 ...

  2. 学习 Spring (四) Bean 的生命周期

    Spring入门篇 学习笔记 定义 --> 初始化 --> 使用 --> 销毁 初始化 实现 org.springframework.beans.factory.Initializi ...

  3. MyEclipse Spring 学习总结二 Bean的生命周期

    文件结构可以参考上一节 Bean的生命周期有方法有:init-method,destroy-method ApplicationContext.xml 文件配置如下: <?xml version ...

  4. Spring 使用介绍(十三)—— Bean的生命周期

    一.概述 Spring Bean的完整生命周期从创建Spring容器开始,直到最终Spring容器销毁Bean,生命周期时序图如下: 二.生命周期接口分类 Bean的生命周期经历了多个接口方法的调用, ...

  5. java Spring系列之 配置文件的操作 +Bean的生命周期+不同数据类型的注入简析+注入的原理详解+配置文件中不同标签体的使用方式

    Spring系列之 配置文件的操作 写在文章前面: 本文带大家掌握Spring配置文件的基础操作以及带领大家理清依赖注入的概念,本文涉及内容广泛,如果各位读者耐心看完,应该会对自身有一个提升 Spri ...

  6. spring in action 学习笔记十四:用纯注解的方式实现spring mvc

    在讲用纯注解的方式实现springmvc之前先介绍一个类:AbstractAnnotationDispatcherServletInitializer.这个类的作用是:任何一个类继承AbstractA ...

  7. spring in action学习笔记十五:配置DispatcherServlet和ContextLoaderListener的几种方式。

    在spring in action中论述了:DispatcherServlet和ContextLoaderListener的关系,简言之就是DispatcherServlet是用于加载web层的组件的 ...

  8. spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入

    一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...

  9. Spring in Action 学习笔记一

    Spring 核心       Spring的主要特性仅仅是 依赖注入DI和面向切面编程AOP       JavaBean 1996.12 Javav 规范针对Java定义了软件组件模型,是简单的J ...

随机推荐

  1. ofbiz研究

    近段时间,刚有有时间研究了下ofbiz ; 目前还是刚开始,后期会记录过程 有一起研究的没

  2. 【CodeBase】PHP打印所有用户自定义常量

    print_r(get_defined_constants(true)['user']);

  3. JavaScript - 库 jQuery

    测试 JavaScript 框架库 - jQuery 引用JQuery 如需测试JavaScript库,您需要在网页中引用它. 为了引用某个库,请使用<script>标签,其src属性设置 ...

  4. php-5.6.26源代码 - PHP文件汇编成opcode、执行

    文件 php-5.6.26/Zend/zend.c ZEND_API int zend_execute_scripts(int type TSRMLS_DC, zval **retval, int f ...

  5. delphi的消息对话框

    delphi的消息对话框,类似VFP中的WAIT和MESSAGEBOXdelphi的消息对话框,类似VFP中的WAIT和MESSAGEBOX1.最简单的是:showmessage() 它只有一个OK按 ...

  6. Mysql:case when then end 的用法

    0.创建一张数据表 表名为 test_when_case CREATE TABLE `test_when_case` ( `id` int(11) unsigned NOT NULL AUTO_INC ...

  7. 11.VUE学习之提交表单时拿到input里的值

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  8. python入门基本知识

    1. 什么是语言 语言是一个事物与另外一个事物沟通的介质. python则是人(程序员)与计算机沟通的介质. 2. 什么是编程 编程就是程序员将自己想要让计算机做的事情用编程语言翻译出来写到一系列的文 ...

  9. C++基础 const

    1. C中的const C中const变量只是只读变量,有自己存储空间.可能被存放在 栈.堆.数据段,所以可以修改. 2. C++中const 可能分配空间,也可能不分配空间. 当 const 为全局 ...

  10. 13 KNN背景分割器

    传统的前景背景分割方法有GrabCut,分水岭算法,当然也包括一些阈值分割的算法.但是这些算法在应用中往往显得鲁棒性较弱,达不到一个好的分割效果. 现代的背景分割算法融入了机器学习的一些方法来提高分类 ...