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 ...
随机推荐
- Java OOP——第五章 异常
1. 尝试通过if-else来解决异常问题: Eg: public class Test2 { public static void main(String[] args) { ...
- dts--tests(二)
rxtx_callbacks.py """ DPDK Test suite. Test Rxtx_Callbacks. """ import ...
- python-无参函数
#!/usr/local/bin/python3 # -*- coding:utf-8 -*- ''' #-----------定义函数---------- def func1(): "te ...
- 硬件中断--DEBUG系列
问题描述: 在线调试时,全速运行,程序进入硬件中断,查看堆栈窗口,发现是从A函数进去的.但是A函数应该没有问题的: 再次重复,发现是从B函数进去的,但是B函数之前运行起来也没有问题的,而且没有传入参数 ...
- python向多个邮箱发邮件--注意接收是垃圾邮件
群发邮件注意:三处标红的地方 # -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText from emai ...
- Flume是什么
分布式流式实时收集日志文件系统,便于实时在线的流式计算,常配合 Storm 和 spark streming 使用. Flume is a distributed分布式的, reliable可靠的, ...
- Mysql处理海量数据时的一些优化查询速度方法【转】
最近一段时间由于工作需要,开始关注针对Mysql数据库的select查询语句的相关优化方法.由于在参与的实际项目中发现当mysql表的数据量达到百万级时,普通SQL查询效率呈直线下降,而且如果wher ...
- Android 用Chrome浏览器打开url 自定义样式
1.效果预览 1.1.真实效果就是从某一个APP,打开一个url,跳转到谷歌浏览器,返回之后,又回到之前的APP 1.2.说明一下条件 1.手机上必须要安装谷歌浏览器 2.手机上的默认浏览器 ...
- PHP代码审计2-常用超全局变量,常用命令注入,常用XSS漏洞审计,文件包含
超全局变量 $GLOBALS — 引用全局作用域中可用的全部变量$_SERVER — 服务器和执行环境信息$_GET — HTTP GET 变量$_POST — HTTP POST 变量$_FILES ...
- python ranndom模块及生成验证码
python的random模块用于生成随机数,下面介绍一下random模块的常用方法: 取随机小数: 数学计算 random.random() 用于生成一个0-1的随机浮点数 0<=n<1 ...