• 为什么要了解Spring中 bean的生命周期?

有时候我们需要自定义bean的创建过程,因此了解Spring中 bean的生命周期非常重要。

  • 二话不说先上图:

  • 在谈具体流程之前先看看Spring官方文档中在介绍原型作用域时的一段话:In contrast to the other scopes, Spring does not manage the complete lifecycle of a prototype bean. The container instantiates, configures, and otherwise assembles a prototype object and hands it to the client, with no further record of that prototype instance. Thus, although initialization lifecycle callback methods are called on all objects regardless of scope, in the case of prototypes, configured destruction lifecycle callbacks are not called. The client code must clean up prototype-scoped objects and release expensive resources that the prototype beans hold. To get the Spring container to release resources held by prototype-scoped beans, try using a custom bean post-processor, which holds a reference to beans that need to be cleaned up.

也就是说Spring并不会管理原型作用域对象的完整生命周期,对象在被Spring处理到上图中的“bean可以使用了”之后就脱离了spring监管,不会调用销毁方法(如果配置了),除非自定义BeanPostProcessor增加想要的销毁逻辑。

  • 接下来介绍上图中的每个步骤:
  1. Spring对bean进行实例化。
  2. Spring将值及bean的引用注入到当前bean对应的属性中。
  3. 如果bean实现了BeanNameAware接口,那么Spring将bean的ID传给setBeanName()方法。
  4. 如果bean实现了BeanFactoryAware接口,那么Spring将调用setBeanFactory()方法,同时传入BeanFactory容器实例。
  5. 如果bean实现了ApplicationContextAware接口,那么Spring将调用setApplicationContext()方法,同时传入bean所在应用上下文的引用。
  6. 如果bean实现了BeanPostProcessor接口,那么Spring将调用他们的postProcessBeforeInitialization()方法。
  7. 如果bean实现了InitializingBean接口,那么Spring将调用他们的afterPropertiesSet()方法。类似地,如果<bean>中配置了init-method属性(即指定了自定义的初始化方法,该方法可以抛异常但是不能有参数),则还会接着调用该方法。
  8. 如果bean实现了BeanPostProcessor接口,那么Spring将调用他们的postProcessAfterInitialization()方法。
  9. bean准备就绪,可以使用了,他们将一直驻留与应用上下文,直到应用上下文被销毁。
  10. 如果bean实现了DisposableBean接口,那么Spring将调用他的destroy()方法。类似地,如果<bean>中配置了destroy-method属性(即指定了自定义的销毁方法,和步骤7中的init-method方法一样,该方法可以抛异常但是不能有参数),则还会接着调用该方法。
 package com.spring.beans.cycle;

 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.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; public class LolService
implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, ApplicationContextAware { // 实现DisposableBean接口的方法
@Override
public void destroy() throws Exception {
System.out.println("执行DisposableBean接口的destroy方法");
} // 实现InitializingBean接口的方法
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("执行InitializingBean接口的afterPropertiesSet方法");
} // 通过配置文件中<bean>的init-method属性指定的初始化方法
public void init() {
System.out.println("init...");
} // 通过配置文件中<bean>的destroy-method属性指定的销毁方法
public void destroy0() {
System.out.println("destroy0...");
} @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("执行ApplicationContextAware接口的setApplicationContext()方法: " + applicationContext);
} @Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("执行BeanFactoryAware接口的setBeanFactory()方法: " + beanFactory);
} @Override
public void setBeanName(String name) {
System.out.println("执行BeanNameAware接口的setBeanName()方法: " + name);
} }

示例bean:LolService

package com.spring.beans.cycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization: " + bean + ", " + beanName);
return bean;
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization: " + bean + ", " + beanName);
return bean;
} }

BeanPostProcessor实现类

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="lolService" class="com.spring.beans.cycle.LolService" init-method="init" destroy-method="destroy0"></bean> <bean class="com.spring.beans.cycle.MyBeanPostProcessor"></bean>
</beans>

配置文件

package com.spring.beans.cycle;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans-cycle.xml");
LolService service = (LolService) context.getBean("lolService");
context.close();
} }

测试类

Dec 23, 2019 6:38:02 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@29ca901e: startup date [Mon Dec 23 18:38:02 CST 2019]; root of context hierarchy
Dec 23, 2019 6:38:02 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans-cycle.xml]
执行BeanNameAware接口的setBeanName()方法: lolService
执行BeanFactoryAware接口的setBeanFactory()方法: org.springframework.beans.factory.support.DefaultListableBeanFactory@7e0b85f9: defining beans [lolService,com.spring.beans.cycle.MyBeanPostProcessor#0]; root of factory hierarchy
执行ApplicationContextAware接口的setApplicationContext()方法: org.springframework.context.support.ClassPathXmlApplicationContext@29ca901e: startup date [Mon Dec 23 18:38:02 CST 2019]; root of context hierarchy
postProcessBeforeInitialization: com.spring.beans.cycle.LolService@77847718, lolService
执行InitializingBean接口的afterPropertiesSet方法
init...
postProcessAfterInitialization: com.spring.beans.cycle.LolService@77847718, lolService
Dec 23, 2019 6:38:03 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@29ca901e: startup date [Mon Dec 23 18:38:02 CST 2019]; root of context hierarchy
执行DisposableBean接口的destroy方法
destroy0...

控制台

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. 通过BeanPostProcessor理解Spring中Bean的生命周期

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

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

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

  7. Spring中bean的生命周期!

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

  8. Spring官网阅读(十)Spring中Bean的生命周期(下)

    文章目录 生命周期概念补充 实例化 createBean流程分析 doCreateBean流程分析 第一步:factoryBeanInstanceCache什么时候不为空? 第二步:创建对象(crea ...

  9. 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章

    前言 当你准备去复习Spring中Bean的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...

随机推荐

  1. 七牛云图床及MPIC工具使用

    考虑到图片更容易被人接受,但是大量图片又会延迟博客加载速度.因此,个人感觉可以把静态文件资源托管在云端,这样加载的话就不至于太慢. 注册七牛云 实名验证通过 创建文件存储 内容管理-上传图片 下载Mp ...

  2. STL priority_queue 常见用法详解

    <算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...

  3. selenium的使用与chromedriver的下载配置

    Selenium是一个web自动化测试工具,最初是为网站自动化测试而开发的,Selenium可以直接运行在浏览器上,它支持所有主流的浏览器,可以接受指令,让浏览器自动加载页面,获得需要的数据,甚至页面 ...

  4. type(),dir(),getattr(),hasattr(), isinstance()用法

    1.type(变量)  --->输出变量的类型int.float.str or others: 2.dir()   ----> dir() 函数不带参数时,返回当前范围内的变量.方法和定义 ...

  5. Neo4j WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual

    you can add a line in /etc/default/neo4j: NEO4J_ULIMIT_NOFILE=60000 to set the ulimit setting (60000 ...

  6. .Net面试题二

    谈谈创建线程的方式 1.列举.Net页面之间传值的方式 2..Net中aspx页面从客户端浏览器开始请求到服务器返回响应所经历的过程 CLR主要运行过程 ASP.NET运行管道所有事件 3.如何理解委 ...

  7. JSON函数表1

    jsoncpp 主要包含三个class:Value.Reader.Writer.注意Json::Value 只能处理 ANSI 类型的字符串,如果 C++ 程序是用 Unicode 编码的,最好加一个 ...

  8. ArrayList,LinkedList,Vector区别.TreeSet,TreeSet,LinkedHashSet区别

    ArrayList: 基于数组的数据结构,地址连续,一旦数据保存好了,查询效率比较高,但是因为其地址连续,所以增删数据需要移动数据,影响速度 内部数组长度默认为10,当需要扩容时,数组长度按1.5倍增 ...

  9. Swagger学习(一、入门)

    简单 入门(效果) SwaggerConfig.class @Configuration //变成配置文件 @EnableSwagger2 //开启swagger2 public class Swag ...

  10. Myeclipse启动后tomcat空指针异常

    今天早上吃完早餐来公司上班,打开电脑,输入密码,123456.....嗯……,再打开myeclipse,duang...duang...duang....tomcat空指针异常,tmd我这暴脾气昨天还 ...