spring Bean的完整生命周期
spring 容器中的bean的完整生命周期一共分为十一步完成。
1.bean对象的实例化
2.封装属性,也就是设置properties中的属性值
3.如果bean实现了BeanNameAware,则执行setBeanName方法,也就是bean中的id值
4.如果实现BeanFactoryAware或者ApplicationContextAware ,需要设置setBeanFactory或者上下文对象setApplicationContext
5.如果存在类实现BeanPostProcessor后处理bean,执行postProcessBeforeInitialization,可以在初始化之前执行一些方法
6.如果bean实现了InitializingBean,则执行afterPropertiesSet,执行属性设置之后的操作
7.调用<bean init-method="">执行指定的初始化方法
8.如果存在类实现BeanPostProcessor则执行postProcessAfterInitialization,执行初始化之后的操作
9.执行自身的业务方法
10.如果bean实现了DisposableBean,则执行spring的的销毁方法
11.调用<bean destory-method="">执行自定义的销毁方法。
第五步和第八步可以结合aop,在初始化执行之前或者执行之后执行一些操作。
以上就是springbean的完整生命周期.
代码如下:
public class Man implements BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean {
private String name;
public Man() {
System.out.println("第一步:实例化类");
}
public void setName(String name) {
System.out.println("第二步:设置属性");
this.name = name;
}
@Override
public void setBeanName(String s) {
System.out.println("第三步:设置bean的名称也就是spring容器中的名称,也就是id值" + name);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("第四步:了解工厂信息ApplicationContext");
}
//第五步执行初始化之前执行的方法
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("第六步:属性设置后执行的方法");
}
public void setup() {
System.out.println("第七步:执行自己配置的初始化方法");
}
//第八步执行初始化之后执行的方法
public void run() {
System.out.println("第九步:执行自身的业务方法");
}
@Override
public void destroy() throws Exception {
System.out.println("第十步:执行spring的销毁方法");
}
public void destory() {
System.out.println("第十一步:执行自己配置的销毁方法");
}
}
指定的类实现了BeanPostProcessor
public class MyBeanPostProcess implements BeanPostProcessor {
//后处理bean,最重要的两步
@Override
public Object postProcessBeforeInitialization(Object bean, String s) throws BeansException {
System.out.println("第五步:初始化之前执行的方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String s) throws BeansException {
System.out.println("第八步:执行初始化之后的方法");
return bean;
}
}
applicationContext.xml中的配置
<bean id="man" class="com.itheima.ioc.springBeanlife.Man" init-method="setup" destroy-method="destory">
<property name="name" value="张三"/>
</bean>
<bean class="com.itheima.ioc.springBeanlife.MyBeanPostProcess"/>
测试
@Test
public void beanlifeTest(){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Man man=(Man)context.getBean("man");
man.run();
context.close();
}
spring Bean的完整生命周期的更多相关文章
- 一张图搞懂Spring bean的完整生命周期
一张图搞懂Spring bean的生命周期,从Spring容器启动到容器销毁bean的全过程,包括下面一系列的流程,了解这些流程对我们想在其中任何一个环节怎么操作bean的生成及修饰是非常有帮助的. ...
- spring bean 容器的生命周期是什么样的?
spring bean 容器的生命周期流程如下: 1.Spring 容器根据配置中的 bean 定义中实例化 bean. 2.Spring 使用依赖注入填充所有属性,如 bean 中所定义的配置. 3 ...
- Spring Bean各阶段生命周期的介绍
一.xml方式配置bean 二.Aware接口 2.1 BeanNameAware 2.2 BeanFactoryAware 2.3 ApplicationContextAware 2.4 Aware ...
- [spring] -- bean作用域跟生命周期篇
作用域 singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的. prototype : 每次请求都会创建一个新的 bean 实例. request : 每一次HT ...
- Spring注解开发系列Ⅲ --- 生命周期
Bean的生命周期 Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解. 首先看下生命周期图: 再谈生命周期之前有一点需要先明确: S ...
- Spring 了解Bean的一生(生命周期)
转载 https://blog.csdn.net/w_linux/article/details/80086950 该篇博客就来了解IoC容器下Bean的一生吧,也可以理解为bean的生命周期. ## ...
- day38 09-Spring类的完整生命周期及后处理Bean
可以配置Bean的这个类的初始化和销毁的方法. 如何销毁这个bean?销毁必须得手动地关闭掉容器才行.而且销毁必须是在scope="singleton"下才有效.因为如果你scop ...
- Spring中与bean有关的生命周期
前言 记得以前的时候,每次提起Spring中的bean相关的生命周期时,内心都无比的恐惧,因为好像有很多,自己又理不清楚,然后看网上的帖子,好像都是那么一套,什么beanFactory啊,aware接 ...
- 浅尝Spring注解开发_Bean生命周期及执行过程
Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含Bean生命周期.自定义初始化方法.Debug BeanPostProcessor执行过程及在Spring底层中的应 ...
随机推荐
- APACHE SPARK 2.0 API IMPROVEMENTS: RDD, DATAFRAME, DATASET AND SQL
What’s New, What’s Changed and How to get Started. Are you ready for Apache Spark 2.0? If you are ju ...
- Iris jwt 使用
jwt分为三个部分: 1.header,用来存储算法和token类型等信息 2.payload, 一些简单的信息 3.签名,来验证token是否合法 iris-jwt 这是初始化jwt中间 ...
- Android开发欢迎页点击跳过倒计时进入主页
没点击跳过自然进入主页,点击跳过之后立即进入主页 1.欢迎页布局activity_sp.xml放一张背景图(图片随你便啦)再放一个盛放倒计时的TextView <?xml versi ...
- go语言之行--golang操作redis、mysql大全
一.redis 简介 redis(REmote DIctionary Server)是一个由Salvatore Sanfilippo写key-value存储系统,它由C语言编写.遵守BSD协议.支持网 ...
- mysql c connector 多条sql语句执行示例
// 假设参数 sql已经包含多条sql语句.如 sql = "insert into table1(...) values(...); update table2 set a=1;& ...
- HDU-1695 莫比乌斯反演
这里学习一下莫比乌斯反演 翻看了很多书,发现莫比乌斯反演,准确来说不是一种固有的公式,而是一种法则. 我们定义F(n),为f(d)的和函数,而定义f(n)为某儿算术函数. 反演公式1:反演n的因子时 ...
- 1 vmware 如何联网,以及行命令令初步
VMware安装Linux(我安装的是ubuntu),没法上网,上网教程见:https://blog.csdn.net/qq_28090573/article/details/78730552 安装完 ...
- c语言之控制语句:循环
#include<stdio.h> int main(void) { long num; long sum = 0L; int status; printf("Please en ...
- docker(一) Centos7下安装docker
docker(一) Centos7下安装dockerdocker(二) windows10下安装dockerdocker(三) 镜像和容器常用命令 docker(四) 使用Dockerfile构建镜像 ...
- Shiro限制登录尝试次数
/** * 认证信息.(身份验证) : Authentication 是用来验证用户身份 * * @param token * @return * @throws AuthenticationExce ...