学习 Spring (四) Bean 的生命周期
Spring入门篇 学习笔记
定义 --> 初始化 --> 使用 --> 销毁
初始化
实现 org.springframework.beans.factory.InitializingBean 接口,覆盖 afterPropertiesSet 方法
public class ExampleBean implements InitializingBean{
public void afterPropertiesSet() throws Exception{
// do some initialization work
} }
配置 init-method:
<bean id="exampleInitBean" class="example.ExampleBean" init-method="start"/>
public class ExampleBean{
public void start(){
// do some initialization work
} }
销毁
实现 org.springframework.beans.factory.DisposableBean 接口,覆盖 destory 方法
public class ExampleBean implements DisposableBean{
public void destory() throws Exception{
// do some destruction work
} }
配置 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>
多种方式同时使用执行顺序
- afterPropertiesSet()
- init-method / default-init-method
- destory()
- 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 的生命周期的更多相关文章
- Spring官网阅读(十)Spring中Bean的生命周期(下)
文章目录 生命周期概念补充 实例化 createBean流程分析 doCreateBean流程分析 第一步:factoryBeanInstanceCache什么时候不为空? 第二步:创建对象(crea ...
- 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章
前言 当你准备去复习Spring中Bean的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...
- 一次性讲清楚spring中bean的生命周期之二:FactoryBean的前世今生
前言 在<spring中FactoryBean是什么bean>一文中,带着小伙伴学习了spring中的FactoryBean,了解了到了FactoryBean其实是一种生产Bean的bea ...
- JAVA面试题:Spring中bean的生命周期
Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...
- 深入理解Spring中bean的生命周期
[Spring中bean的生命周期] bean的生命周期 1.以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期: (1).生命周期图: (2).具体事例 ...
- Spring中Bean的生命周期及其扩展点
原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6106456.html Spring中Bean的管理是其最基本的功能,根据下面的图来了解Spr ...
- 简:Spring中Bean的生命周期及代码示例
(重要:spring bean的生命周期. spring的bean周期,装配.看过spring 源码吗?(把容器启动过程说了一遍,xml解析,bean装载,bean缓存等)) 完整的生命周期概述(牢记 ...
- 面试Spring之bean的生命周期
找工作的时候有些人会被问道Spring中Bean的生命周期,其实也就是考察一下对Spring是否熟悉,工作中很少用到其中的内容,那我们简单看一下. 在说明前可以思考一下Servlet的生命周期:实例化 ...
- 通过BeanPostProcessor理解Spring中Bean的生命周期
通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理 Spring源码解析(十一)Spring扩展接口InstantiationAwareBeanPostProces ...
随机推荐
- MySql 建表出现的问题:[ERR] 1064 - You have an error in your SQL syntax; check the manual.......
使用 MySql 建表出现的问题 在使用 Navicat Premium 运行 sql 语句进行建表时,MySQL 报错如下: 建表语句: DROP DATABASE IF EXISTS javawe ...
- HRBUST - 2347 - 递归画图 - vj大一上寒假训练2.11
其他题可由本题变形得到. 思路:利用坐标dfs搜索. 注意:1,初始化.2,坐标实时更新(x,y) 代码: #include<iostream> #include<cstdio> ...
- 性能调优7:多表连接 - join
在产品环境中,往往存在着大量的表连接情景,不管是inner join.outer join.cross join和full join(逻辑连接符号),在内部都会转化为物理连接(Physical Joi ...
- 扒一拔:Java 中的泛型(一)
目录 1 泛型 1.1 为什么需要泛型 1.2 类型参数命名规约 2 泛型的简单实用 2.1 最基本最常用 2.2 简单泛型类 2.2.1 非泛型类 2.2.2 泛型类的定义 2.2.3 泛型类的使用 ...
- .netcore 堆栈调用方法小记
背景 上午临近午饭时,公司同事反馈验证码被攻击灌水.我们匆忙查询验证码明细,对已频繁出现的IP插入黑名单,但IP仍然隔断时间频繁变动,不得已之下只能先封禁对应公司id的验证码发送功能.年初时候,专门对 ...
- 【LeetCode-数组篇】 1 Two Sum
1 前言 之所以开始刷 LeetCode 上的算法题,一是快面临秋招,第二点是因为提升自己的编程能力,坚持两个月,希望博友们监督. 这个系列打算用 C# 和 Java 编程,为什么用两门语言,因为经历 ...
- Microsoft Tech Summit 2018 课程简述:利用 Windows 新特性开发出更好的手绘视频应用
概述 Microsoft Tech Summit 2018 微软技术暨生态大会将于10月24日至27日在上海世博中心举行,这也会是国内举办的最后一届 Tech Summit,2019 年开始会以 Mi ...
- 【开源】Westore Cloud 发布- 没后端没SQL没DBA,只需 javascript 开发云端小程序
Westore Cloud - 隐形云,NoBackEnd,NoSql,HiddenDB 好的设计便是感觉不到设计的存在 开发小程序,但是:没有后端!没有运维!没有 DBA!没有域名!没有证书!没有钱 ...
- java----牛客练习
1. 形式参数就是函数定义时设定的参数.例如函数头 int min(int x,int y,int z) 中 x,y,z 就是形参.实际参数是调用函数时所使用的实际的参数. 真正被传递的是实参 ...
- Makes And The Product CodeForces - 817B (思维+构造)
B. Makes And The Product time limit per test 2 seconds memory limit per test 256 megabytes input sta ...