SpringXML方式配置bean的生命周期lifecycle
在Spring中容器在初始化某个bean的时候会有相应的生命周期,类似于Servlet,有相应的init,destory等方法
例如:如下service
|
1
2
3
4
5
6
7
8
9
10
11
|
public class UserService { public void init(){ System.out.println("UserService init ....."); } public void execute(){ System.out.println("UserService execute..."); } public void distory(){ System.out.println("UserService distory..."); }} |
想让容器在初始化该bean之前调用init方法,容器销毁之后执行distory方法,可以这样配置
|
1
2
3
|
<bean id="userService" class="com.fz.service.UserService" init-method="init" destroy-method="distory"></bean> |
测试代码:
|
1
2
3
4
5
6
7
|
@Testpublic void getProperties(){ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService user = (UserService) ctx.getBean("userService"); System.out.println(user); ctx.destroy();} |
注意:这里是直接使用的applicationContext接口的实现类ClassPathXmlApplicationContext,因为下面要用到ctx.destroy()方法,这个方法
在ApplicationContext接口里是没有的。
打印结果:

假如现在我们再获取一次userService对象呢?
|
1
2
3
4
5
6
7
8
9
|
@Testpublic void getProperties(){ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService user = (UserService) ctx.getBean("userService"); UserService user1 = (UserService) ctx.getBean("userService"); System.out.println(user); System.out.println(user1); ctx.destroy();} |
此时,结果如下:

从上图可以看出,此时对象是获取了两次,并且这两个对象是同一个对象(因为默认的scope属性是singleton),但是init和distory都是只执行了一次。
假如现在我们再配置上scope=prototype之后呢?
|
1
2
3
|
<bean id="userService" class="com.fz.service.UserService" init-method="init" destroy-method="distory" scope="prototype"></bean> |
同样的测试代码,结果如下:

此时发现,现在是init了两次,也获取到两个不同的bean。但是distory却没有执行。
可以看出在配置了prototype之后,ClassPathXmlApplicationContext是监控不到bean的生存和销毁的。
总结:
1、在bean初始化之前执行某个方法:init-method="init"
2、在容器销毁之后执行某个方法: destroy-method="distory"
3、如果同一个bean获取多次,此时init-method和destroy-method都只执行一次(没有使用prototype情况下)
4、init-method、destroy-method不要和scope=prototype一起使用
5、init-method和destroy-method一般情况下我们开发人员很少使用,但是spring自己却需要使用。
比如:在Spring的连接池中就用到了destroy-method方法,不用该数据源的时候则把它关闭。
|
1
2
3
4
5
6
7
|
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><!-- results in a setDriverClassName(String) call --><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="username" value="root"/><property name="password" value="masterkaoli"/></bean> |
SpringXML方式配置bean的生命周期lifecycle的更多相关文章
- SpringXML方式配置bean的生存范围Scope
在一个bean的配置里面可以指定一个属性Scope,也就是bean的范围,bean的生命周期. Scope可取的值5种:singleton(默认).prototype.request.session. ...
- spring配置bean的生命周期
配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...
- SpringXML方式配置bean的集合注入:list,map,properties
新建一个bean,设置相应的集合属性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class Collecti ...
- SpringXML方式配置bean的自动装配autowire
Spring的自动装配,也就是定义bean的时候让spring自动帮你匹配到所需的bean,而不需要我们自己指定了. 例如: User实体类里面有一个属性role 1 2 3 4 5 6 7 publ ...
- SpringXML方式配置bean的懒加载lazy-init
lazy-init(懒加载),表示该bean在容器初始化的时候不进行初始化. 例如: <bean name="role1" class="com.fz.entity ...
- spring:bean的生命周期
1.spring中bean的生命周期 (1)概念 在spring框架中,所有的bean对象都有生命周期,就是指bean的创建.初始化.服务.销毁的一个过程. (2)bean的生命周期 bean的定义 ...
- IoC容器装配Bean(xml配置方式)(Bean的生命周期)
1.Spring管理Bean,实例化Bean对象 三种方式 第一种:使用类构造器实例化(默认无参数) package cn.itcast.spring.initbean; /** * 使用构造方法 实 ...
- Spring学习五----------Bean的配置之Bean的生命周期
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的生命周期 1.定义 2.初始化 3.使用 4.销毁 初始化和销毁的三种方式 1.实现org.springframework.beans.fa ...
- java Spring系列之 配置文件的操作 +Bean的生命周期+不同数据类型的注入简析+注入的原理详解+配置文件中不同标签体的使用方式
Spring系列之 配置文件的操作 写在文章前面: 本文带大家掌握Spring配置文件的基础操作以及带领大家理清依赖注入的概念,本文涉及内容广泛,如果各位读者耐心看完,应该会对自身有一个提升 Spri ...
随机推荐
- SpringMVC项目配置
一.创建一个maven项目 1.new一个maven项目,选择next,如图:
- Glide Picasso和Fresco的对比
比较Picasso.Glide 和 Fresco 三种图片加载库 比较 Picasso 与 Glide 总体来说二者极为相似,有着近乎相同的 API 的使用风格,但 Glide 在缓存策略和加载 gi ...
- JVM年轻代(转)
本文转自:http://my.oschina.net/xishuixixia/blog/133850 1.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代完 ...
- [2013-1-29] Air 安卓 天气预报源码
开源一个air android纯代码天气预报源码,未作优化~ ,仅供学习使用,勿作商用~ 花了两天时间,随便写了个天气预报 小应用 .纯代码,没有用组件,所以体积要小点.效率么...主要是没有去优 ...
- Java Calendar类总结
在实际项目当中,我们经常会涉及到对时间的处理,例如登陆网站,我们会看到网站首页显示XXX,欢迎您!今天是XXXX年....某些网站会记录下用户登陆的时间,比如银行的一些网站,对于这些经常需要处理的问题 ...
- innodb 行级锁
InnoDB行锁是通过给索引上的索引项加锁来实现的,这一点MySQL与Oracle不同,后者是通过在数据块中对相应数据行加锁来实现的.InnoDB这种行锁实现特点意味着:只有通过索引条件检索数据,In ...
- 20145314郑凯杰 《Java程序设计》实验四 实验报告
20145314郑凯杰 <Java程序设计>实验四 实验报告 实验要求 完成实验.撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用 ...
- linux 块设备-整理(一)
1. 基本概念: linux设备驱动开发详解(宋宝华): 字符设备与块设备 I/O 操作的不同如下. (1)块设备只能以块为单位接受输入和返回输出,而字符设备则以字节为单位. 大多数设备是字符设备,因 ...
- unicode下数据之间的转换
首先mfc下字符串只有两种数据:char(一个字节)和wchar_t(两个字节),很多其他数据类型如TCHAR,WCHAR等都是这个两个基本类型的宏定义,BYTE是uchar 1.对话框打印char* ...
- Show Desktop Pro FAQ
Q. Will the desktop background image be restored after quit? A: Yes. Right now, "Hide icons&quo ...