spring in action 学习笔记四:bean的生命周期
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 对应阶段说明 onLOad(Object query) 1.1 页面加载时触发, 一个页面只会调用一次, 可以在 onLoad的参数中获取打开当前页面路径中的参数 1.2 参数 ...
- 学习 Spring (四) Bean 的生命周期
Spring入门篇 学习笔记 定义 --> 初始化 --> 使用 --> 销毁 初始化 实现 org.springframework.beans.factory.Initializi ...
- MyEclipse Spring 学习总结二 Bean的生命周期
文件结构可以参考上一节 Bean的生命周期有方法有:init-method,destroy-method ApplicationContext.xml 文件配置如下: <?xml version ...
- Spring 使用介绍(十三)—— Bean的生命周期
一.概述 Spring Bean的完整生命周期从创建Spring容器开始,直到最终Spring容器销毁Bean,生命周期时序图如下: 二.生命周期接口分类 Bean的生命周期经历了多个接口方法的调用, ...
- java Spring系列之 配置文件的操作 +Bean的生命周期+不同数据类型的注入简析+注入的原理详解+配置文件中不同标签体的使用方式
Spring系列之 配置文件的操作 写在文章前面: 本文带大家掌握Spring配置文件的基础操作以及带领大家理清依赖注入的概念,本文涉及内容广泛,如果各位读者耐心看完,应该会对自身有一个提升 Spri ...
- spring in action 学习笔记十四:用纯注解的方式实现spring mvc
在讲用纯注解的方式实现springmvc之前先介绍一个类:AbstractAnnotationDispatcherServletInitializer.这个类的作用是:任何一个类继承AbstractA ...
- spring in action学习笔记十五:配置DispatcherServlet和ContextLoaderListener的几种方式。
在spring in action中论述了:DispatcherServlet和ContextLoaderListener的关系,简言之就是DispatcherServlet是用于加载web层的组件的 ...
- spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入
一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...
- Spring in Action 学习笔记一
Spring 核心 Spring的主要特性仅仅是 依赖注入DI和面向切面编程AOP JavaBean 1996.12 Javav 规范针对Java定义了软件组件模型,是简单的J ...
随机推荐
- 判断StringBuilder 是否为空
if("".equals(stringbuilder.toString())) do..
- 7-1 python 操作redis
1.安装并导入redis模块 # pip install redis 安装redis模块 import redis # 导入redis模块 2.连接一个或多个redis,指定数据库名,并指定返回字符串 ...
- ES6笔记01-声明变量
ES6只有六种声明变量的方法:var命令和function命令,let和const命令,import命令和class命令.所以,ES6一共有6种声明变量的方法. const声明一个只读的常量.一旦声明 ...
- C# Newtonsoft.Json 解析多嵌套json 进行反序列化
[ { ", "time": "2016-09-09 12:23:33", ", "freeShipping": tru ...
- redis源代码结构解析
看了黄建宏老师的<Redis设计与实现>,对redis的部分实现有了一个简明的认识: 之前面试的时候被问到了这部分的内容,没有关注,好在还有时间,就把Redis的源码看了一遍. Redis ...
- liteos学习文档liteos.github.io
https://liteos.github.io该主页是华为liteos物联网操作系统的文档,里面有一章是“内核指南”,讲的是rtos的最主要的功能.可以当作liteos的入门了解,如果用rtos的使 ...
- POJ 2891 中国剩余定理(不互素)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 17877 ...
- 笔记-falsk-入门-1
笔记-falsk-入门-1 1. 前言 有几个概念需要解释下,WSGI,JINJA2,WERKZEUG Flask是典型的微框架,作为Web框架来说,它仅保留了核心功能:请求响应处理和模板渲 ...
- scrapy如何实现分布式爬虫
使用scrapy爬虫的时候,记录一下如何分布式爬虫问题: 关键在于多台主机协作的关键:共享爬虫队列 主机:维护爬取队列从机:负责数据抓取,数据处理,数据存储 队列如何维护:Redis队列Redis 非 ...
- videomon 环境搭建
1.安装ACE-5.8.0.tar.bz2 tar -zxvf ACE-.tar.bz2 cd ACE_wrappers/mkdir buildcd build../configuremake &am ...