Spring入门篇 学习笔记

定义 --> 初始化 --> 使用 --> 销毁

初始化

  1. 实现 org.springframework.beans.factory.InitializingBean 接口,覆盖 afterPropertiesSet 方法

    public class ExampleBean implements InitializingBean{
    public void afterPropertiesSet() throws Exception{
    // do some initialization work
    } }
  2. 配置 init-method:

    <bean id="exampleInitBean" class="example.ExampleBean" init-method="start"/>
    public class ExampleBean{
    public void start(){
    // do some initialization work
    } }

销毁

  1. 实现 org.springframework.beans.factory.DisposableBean 接口,覆盖 destory 方法

    public class ExampleBean implements DisposableBean{
    public void destory() throws Exception{
    // do some destruction work
    } }
  2. 配置 destory-method

    <bean id="exampleInitBean" class="example.ExampleBean" destory-method="stop"/>
    public class ExampleBean{
    public void stop(){
    // do some destruction work
    } }

配置全局默认初始化、销毁方法

<?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"
default-init-method="init" default-destory-method="destory"> </beans>

多种方式同时使用执行顺序

  1. afterPropertiesSet()
  2. init-method / default-init-method
  3. destory()
  4. destory-method / default-destory-method
  • 如果同时配置了 init-method 和 default-init-method 或 destory-method 和 default-destory-method,default-init-method 或 default-destory-method 不执行
  • default-init-method 和 default-destory-method 可以不在 Bean 中声明

源码:learning-spring

学习 Spring (四) Bean 的生命周期的更多相关文章

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

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

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

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

  3. 一次性讲清楚spring中bean的生命周期之二:FactoryBean的前世今生

    前言 在<spring中FactoryBean是什么bean>一文中,带着小伙伴学习了spring中的FactoryBean,了解了到了FactoryBean其实是一种生产Bean的bea ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 洛谷 P2802 回家

    题目链接 https://www.luogu.org/problemnew/show/P2802 题目描述 小H在一个划分成了n*m个方格的长方形封锁线上. 每次他能向上下左右四个方向移动一格(当然小 ...

  2. ffmpeg源码安装

    官网下载地址 http://www.ffmpeg.org/download.html https://sourceforge.net/projects/opencore-amr/ 参考资料:官网及以下 ...

  3. Sql Server利用游标批量清空数据表

    先吐槽一下,由于公司要为新客户部署一个全新的系统,然而公司并没有空库,所以只能把正在线上运行的数据库给备份,然后清空相关数据 下面分享一下我在做清空数据库时写的一个批量清空数据表的方法 思路:查询出该 ...

  4. 晓晨高效IP提取工具 附源码

    在网上找的几个代理ip网站,抓取下来的.解析网页用的是HtmlAgilityPack,没有用正则.自己重写了ListView使他动态加载的时候不闪烁.效果图 下载地址:http://files.cnb ...

  5. 朱晔的互联网架构实践心得S1E7:三十种架构设计模式(上)

    朱晔的互联网架构实践心得S1E7:三十种架构设计模式(上) [下载本文PDF进行阅读] 设计模式是前人通过大量的实践总结出来的一些经验总结和最佳实践.在经过多年的软件开发实践之后,回过头来去看23种设 ...

  6. C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 严格的用户账户审核功能

    整个集团有几万个用户,一个个用户添加是不现实的,只有每个公司的系统管理员添加.或者用户申请帐户,然后有相应的管理员审核,才会更准确一些. 每个公司.分公司.部门的账户情况只有所在公司的管理员是最清楚的 ...

  7. docker配置阿里云镜像加速

    一.登录阿里云控制台,并打开镜像加速器页面,复制加速器地址 二.修改daemon配置文件/etc/docker/daemon.json ,将复制的地址按照如下格式写入文件,若存在多行,使用逗号分隔. ...

  8. hibernate异常找不到get方法org.hibernate.PropertyNotFoundException: Could not find a getter for did in class com.javakc.hibernate.manytomany.entity.CourseEntity

    属性的get方法没找到,可能是CourseEntity类中对应属性没有get方法,如果有就看CourseEntity.hbm.xml属性名称,应该是写错了不和CourseEntity类中属性名相同,修 ...

  9. JS闭包以及作用域初探

    以前看到的一个问题,很有意思: for (var i = 0; i < 5; i++) { setTimeout(function () { console.log(i); },500); } ...

  10. 03-Linux的shell命令 .doc

    快捷键 基本操作和命令 Cd转换文件夹 以/开头的是绝对路径 没有/相对路径 ../代表上一级目录 Tab补充 Ctrl+R 查找历史输入过的命令 箭头上也代表能够查询以往输入的命令 Ctrl+C 终 ...