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 ...
随机推荐
- pom.xml文件报错:web.xml is missing and <failOnMissingWebXml> is set to true
这个错误原因是因为项目无法加载到web.xml,所以需要配置web项目的目录.具体解决,配置步骤如下: 1.右键项目属性,配置项目目录 /src/main/webapp,如果没有,新增一条 2.配置 ...
- nuget发布自已的程序集
1.nuget注册并获取apikey 2.下载nuget.exe 3.设置apikey nuget setApiKey <apikey> 4.开发程序集 5.进入.csproj目录生成描述 ...
- JAVA面试题整理(3)-Spring
1.BeanFactory 和 FactoryBean? 2.Spring IOC 的理解,其初始化过程? 3.BeanFactory 和 ApplicationContext? 4.Spring B ...
- REST API Design
- saltstack之nginx、php的配置
saltstack为nginx提供状态配置 1.创建nginx配置需要的目录 mkdir /srv/salt/prod/nginx mkdir /srv/salt/prod/nginx/files 2 ...
- 解读:计数器Counter
Counters: 44 File System Counters FILE: Number of bytes read=655771325 FILE: Number of bytes written ...
- [小问题笔记(七)] JS和 jQuery常用语句笔记(隐藏/显示/禁用标签 日期操作 跳转等)
隐藏/显示标签 $("#div1").css("display", "none");$("#div2").css(&qu ...
- sa learning
后缀数组之前一直在给队友搞,但是这个类太大了,预感到青岛八成会有,于是自己也学习一下,记录一下做题的历程 所用的模板暂时来自于队友的倍增nlogn da算法 int t1[maxn] , t2[max ...
- background-size css background-images
在设计网页时,经常会用到背景图片来达到视觉效果. 一般情况下用repeat的方式是最适全不过了,不过有时间是采用整图来充当背景,那么这个时候就会有多种可能性的存在了. 整图来做背景一般是采用no-re ...
- iView--3
项目基本结构 简单介绍目录 build目录是一些webpack的文件,配置参数什么的,一般不用config是vue项目的基本配置文件node_modules是项目中安装的依赖模块src源码文件夹,基本 ...