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
|
@Test public 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
|
@Test public 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 ...
随机推荐
- HTTPS复习
Https Https其实是两个协议,即HTTP协议和SSL协议,但是由于HTTP协议应用广泛,而且需要为其提供数据隐私保护,所以将HTTP协议与SSL协议结合.HTTP属于应用层,在远古时代,它只需 ...
- NIO 01 (转)
本文转自:http://www.cnblogs.com/littlehann/p/3720396.html 目录 1. NIO.NIO.2简介 2. NIO中的关键技术 1. NIO.NIO.2简介 ...
- Core ML 入门
1.下载Xcode 9 2.下载模型,https://developer.apple.com/machine-learning/ 3.开动.. 4.待续 模拟器66的
- JS知识点简单总结
JS(JavaScript) 是一种解释性语言 是弱/动态 最开始用于表单验证的 JS的五个部分: 1.核心语言定义 2.原生对象和内置对象 5boolean 3.BOM 4.DOM 5.事件处理模型 ...
- xxxx interview
1. 什么数据库,做了什么优化? 回答mysql innodb与myisam区别,根据业务选择引擎.事务,行级锁,全文检索,二级索引. 存储:myisam数据和索引分开存储.innodb数据和索引存储 ...
- saltstack之nginx、php的配置
saltstack为nginx提供状态配置 1.创建nginx配置需要的目录 mkdir /srv/salt/prod/nginx mkdir /srv/salt/prod/nginx/files 2 ...
- 20145216 史婧瑶《Java程序设计》第6周学习总结
20145216 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 输入/输出 10.1 InputStream与OutputStream 如果要将数据从来源中取出,可以使用输 ...
- Mysql binlog设置
一:Binlog文件信息 1. binlog基本定义:二进制日志,也成为二进制日志,记录对数据发生或潜在发生更改的SQL语句,并以二进制的形式保存在磁盘中: 2. 作用:MySQL的作用类似于Ora ...
- random模块中最常用的几个函数
转自:http://www.cnblogs.com/yd1227/archive/2011/03/18/1988015.html 随机整数:>>> import random> ...
- 回文树 Palindromic Tree
回文树 Palindromic Tree 嗯..回文树是个什么东西呢. 回文树(或者说是回文自动机)每个节点代表一个本质不同的回文串. 首先它类似字典树,每个节点有SIGMA个儿子,表示对应的字母. ...