这篇博文是spring生命周期的详解,目前限于作者自身的水平对于一些内容可能只知其然不知其所以然,所以博文中如果出现错误欢迎各位指出,同时我也会逐步提升自己的水平,争取能够多发布一些能让大家获益的博文。

  活不多少,先贴代码。

  1,类文件

 person类用于演示bean生命周期的基础类。 

package com.spring.beanlife.beans;

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.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; public class Person implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean {
private String name; public Person(){
System.out.println("无参数构造器");
} public Person(String name){
System.out.println("有参数构造器");
name = this.name;
} public String getName() {
return name;
} public void setName(String name) {
System.out.println("setName(String name) method run!!!!!");
this.name = name;
} public void sayHi() {
System.out.println("hi " + name);
} @Override
/**
*bean名称关注接口可以通过该方法获取到bean的id,xml中配置的bean的id
*/
public void setBeanName(String arg0) {
System.out.println("bean---------> " + arg0);
} @Override
/**
* bean工厂关注接口通过该方法可以获取到beanfactory
*/
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println(arg0); } @Override
/**
* ApplicationContext关注接口通过该方法可以获取到ApplicationContext
*/
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
System.out.println(arg0);
} @Override
/**
* 调用afterPropertiesSet
*/
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet"); } /**
*@调用自定义的init方法
*/
public void init(){
System.out.println("调用自定义的init方法");
} /**
*@调用自定义的destory方法
*/
public void destory(){
System.out.println("调用自定义的destory方法");
}
}
MyBeanPostProcessor实现了BeanPostProcessor后置处理器的类
package com.spring.beanlife.beans;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { @Override
public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
System.out.println("后置处理器的after方法");
return arg0;
} @Override
public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
System.out.println("后置处理器的before方法");
return arg0;
} }

app测试生命周期的main函数方法

package com.spring.beanlife.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource; import com.spring.beanlife.beans.Person; public class App { public static void main(String[] args) {
/*@通过核心容器的方式
*
*/
/*ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/spring/beanlife/beans.xml");
Person person = (Person) applicationContext.getBean("person");
person.sayHi();*/
BeanFactory factory = new XmlBeanFactory(
new ClassPathResource("com/spring/beanlife/beans.xml"));
Person person = (Person) factory.getBean("person");
person.sayHi();
} }

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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="person" init-method="init" destroy-method="destory" class="com.spring.beanlife.beans.Person">
<property name="name" value="liukun" />
</bean>
<bean id="myBeanPostProcessor" class="com.spring.beanlife.beans.MyBeanPostProcessor" />
</beans>

2,bean生命周期步骤(以AppliccationContext容器演示)

  1,实例化(当我们的程序加载beans.xml文件),把我们的bean(前提是scope=singlton)实例化到内存

  2,调用set方法设置属性

  3,如果你实现了bean名字关注接口(BeanNameAware)则可以通过setBeanName获取id号

  4,如果你实现了bean工厂关注接口(BeanFactoryAware)则可以通过setBeanFactory获取BeanFactory

  5,如果你实现了ApplicationContextAware关注接口(ApplicationContextAware)则可以通过setApplicationContext获取ApplicationContext

  6,如果bean和一个后置处理器关联(BeanPostProcessor),则会调用postProcessBeforeInitialization(Before)方法

  7,如果你实现了InitializingBean关注接口(InitializingBean)则可以调用afterPropertiesSe

  8,如果自定义了初始化方法则调用自定义的初始化方法(注自定义初始化方法在bean中通过init-method方法配置)

  9,如果bean和一个后置处理器关联(BeanPostProcessor),则会调用postProcessAfterInitialization(After)方法

   10,bean可以使用了

   11,容器关闭

   12,如果实现DisposableBean接口则可以调用其destory()方法,也可以定制自定义的销毁方法(注自定义初始化方法在bean中通过init-method方法配置)

  ps:开发中常见操作步骤1>2>6>10>9>11

  执行操作结果

  

利用BeanFactory操作执行的结果

结论:BeanFactory和ApplicationContext的生命周期不一样,BeanFactory生命周期较为简单,没有使用到后置处理器的相关功能,以及没有获取核心容器相关方法。

Spring之bean的生命周期的更多相关文章

  1. JAVA面试题:Spring中bean的生命周期

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  2. 深入理解Spring中bean的生命周期

    [Spring中bean的生命周期] bean的生命周期 1.以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期: (1).生命周期图: (2).具体事例 ...

  3. Spring中Bean的生命周期及其扩展点

    原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6106456.html Spring中Bean的管理是其最基本的功能,根据下面的图来了解Spr ...

  4. 简:Spring中Bean的生命周期及代码示例

    (重要:spring bean的生命周期. spring的bean周期,装配.看过spring 源码吗?(把容器启动过程说了一遍,xml解析,bean装载,bean缓存等)) 完整的生命周期概述(牢记 ...

  5. 面试Spring之bean的生命周期

    找工作的时候有些人会被问道Spring中Bean的生命周期,其实也就是考察一下对Spring是否熟悉,工作中很少用到其中的内容,那我们简单看一下. 在说明前可以思考一下Servlet的生命周期:实例化 ...

  6. 通过BeanPostProcessor理解Spring中Bean的生命周期

    通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理 Spring源码解析(十一)Spring扩展接口InstantiationAwareBeanPostProces ...

  7. 一分钟掌握Spring中bean的生命周期!

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean 的别名只能维持 ...

  8. Spring中bean的生命周期!

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  9. 深究Spring中Bean的生命周期

    前言 这其实是一道面试题,是我在面试百度的时候被问到的,当时没有答出来(因为自己真的很菜),后来在网上寻找答案,看到也是一头雾水,直到看到了<Spring in action>这本书,书上 ...

  10. Spring中 bean的生命周期

    为什么要了解Spring中 bean的生命周期? 有时候我们需要自定义bean的创建过程,因此了解Spring中 bean的生命周期非常重要. 二话不说先上图: 在谈具体流程之前先看看Spring官方 ...

随机推荐

  1. java实现每个单词首字母大写

    /** * 每个单词第一个字母大写 * @param str * @return */ public static String toUpperFirstCode(String str) { Stri ...

  2. Linux 下如何修改用户名(同时修改用户组名和家目录)

    有时候,由于某些原因,我们可能会需要重命名用户名.我们可以很容易地修改用户名以及对应的家目录和 UID.-- Shusain 本文导航◈ 修改用户名12%◈ 修改家目录43%◈ 更改用户 UID52% ...

  3. DAX/PowerBI系列 - 建模视图可以多个分页

    PowerBI 十一月的更新终于有了一个解决密集恐惧症患者的方法,建模视图每个tab专注于一个领域,更加简洁. ps: Microstrategy早就有了.

  4. 【Linux】Centos partition

    http://www.cnblogs.com/yogurtwu/p/9494108.html https://www.cnblogs.com/zhangkaimin/p/6251448.html wh ...

  5. pytorch打印模型结构图

    import torchsummary from torchvision.models.resnet import * net = resnet18().cuda() print(net) 打印出来的 ...

  6. Fiddler抓包【1】_介绍及界面概述

    一.   主要抓包工具介绍与对比 1.Wireshark :通用抓包工具,抓取信息量庞大,需要过滤才能得到有用信息,只抓HTTP请求有点大财小用. 2.Firebug.HttpWatch等Web调试工 ...

  7. 从网卡发送数据再谈TCP/IP协议—网络传输速度计算-网卡构造

    在<在深谈TCP/IP三步握手&四步挥手原理及衍生问题—长文解剖IP>里面提到 单个TCP包每次打包1448字节的数据进行发送(以太网Ethernet最大的数据帧是1518字节,以 ...

  8. day17 python递归案例(二分查找,三级菜单)

    递归函数与三级菜单 menu = { '北京': { '海淀': { '五道口': { 'soho': {}, '网易': {}, 'google': {} }, '中关村': { '爱奇艺': {} ...

  9. Excel坐标自动在AutoCad绘图_2

    众所周知,Excel对数据处理的功能非常强大,它可以进行数据处理.统计分析已经辅助决策的操作,该软件已经渗透到各个领域.作为一个测绘人,GISer, 也经常利用excel完成一些测量表格的自动化计算, ...

  10. 2018-2019-2 网络对抗技术 20165305 Exp4 恶意代码分析

    Exp4 恶意代码分析 1.实践目标 1.1是监控你自己系统的运行状态,看有没有可疑的程序在运行. 1.2是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysi ...