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 ...
随机推荐
- redis 慢查询日志
说明:针对慢查询日志,可以设置两个参数,一个是执行时长,单位为微秒,另一个是慢查询日志的长度.如果超过该长度,当一个新的命令被写入日志时,最老的一条会从命令日志队列中被移除. 编辑配置文件redis. ...
- ios 常见问题解决 以及小技巧
1.使用cocoaPods引用第三方类库,报错:file not found . 解决方案:设置 Project->Info->Configurations之后 clear ,然后再 ...
- JavaScript 事件处理详解
事件绑定与解绑: el.onEventName = function (){} document.getElementById("dom").onclick = function( ...
- 《Java入门第三季》第一章 异常与异常处理
Java异常简介 1.Java异常的体系结构.万恶之源Throwable以及它的两个大儿子Mr.Error(程序终结者)和Mr.Exception(有大量儿子,包括不受查的RuntimeExcepti ...
- Django----Request对象&Response对象
Django 使用Request 对象和Response 对象在系统间传递状态. HttpRequest 对象: Request.body:一个字节字符串,表示原始HTTP 请求的正文.它对于处理非H ...
- python2与python3语法区别之_重定向
python2与python3两版本的区别是众所周知的,今天主要记录python下版本2 与版本3的区别 python2 In [7]: logfile = open('/tmp/mylog.log' ...
- journalctl 工具使用
在Systemd出现之前,Linux系统及各应用的日志都是分别管理的,Systemd开始统一管理了所有Unit的启动日志,这样带来的好处就是可以只用一个 journalctl命令,查看所有内核和应用的 ...
- Nginx配置X-Forwarded-Proto
需求 最近公司在做全站https,架构上面有Nginx+tomcat Nginx+php,且nginx配置了ssl,tomcat和php项目使用https协议 但是,发送的是https url请求,p ...
- LeetCode——3Sum
1. Question Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? ...
- JDK 中的监控与故障处理工具-05 (jstack)
jstack : stack trace for java jstack 命令用于生成虚拟机当前时刻的线程快照 (threaddump).线程快照是当前虚拟机中每一条线程正在执行的方法堆栈的集合,生成 ...