Spring源码分析-IOC之DisposableBean
2019-04-03 20:45
411 查看
上一篇我们讲到了InitializingBean,这一篇我们看下DisposableBean接口,它也只提供一个方法destroy()。如果实现了DisposebleBean接口,那么Spring将自动调用bean中的Destory方法进行销毁,下面我们看下DisposebleBean应用:
[code]package com.ck.bean; import java.io.Serializable; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class Person implements InitializingBean, DisposableBean, Serializable{ private static final long serialVersionUID = 1L; private Long id; private String name; private String sex; private Integer age; public Person() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public void afterPropertiesSet() throws Exception { System.out.println("Person InitializingBean afterPropertiesSet"); } //注意这个方法 @Override public void destroy() throws Exception { System.out.println("Person DisposableBean destroy"); } }
看下配置文件:
[code]<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <bean id="person" class="com.ck.bean.Person"/> </beans>
看下测试代码:
[code]package com.ck.ioc; import org.junit.Test; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class IocTest { @Test public void test() { ConfigurableApplicationContext ctx = new FileSystemXmlApplicationContext( "src/main/resources/applicationContext.xml"); //注意这个方法 ctx.close(); } }
测试结果:
[code]四月 03, 2019 8:19:03 下午 org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@3c679bde: startup date [Wed Apr 03 20:19:03 CST 2019]; root of context hierarchy 四月 03, 2019 8:19:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from file [X:\CK\stsws\springWeb\src\main\resources\applicationContext.xml] Person InitializingBean afterPropertiesSet 四月 03, 2019 8:19:04 下午 org.springframework.context.support.FileSystemXmlApplicationContext doClose 信息: Closing org.springframework.context.support.FileSystemXmlApplicationContext@3c679bde: startup date [Wed Apr 03 20:19:03 CST 2019]; root of context hierarchy Person DisposableBean destroy
我们看到destroy方法被执行.
小结:DisposableBean主要用于我们销毁bean的时候做一些逻辑处理.
相关文章推荐
- Spring IOC源码分析(七):IOC容器的设计实现与bean对象的创建流程
- 分析Spring IoC源码(二)BeanFactory初始化
- Spring Ioc 源码分析之Bean的加载和构造
- spring源码分析之bean之IOC
- 做一个合格的程序猿之浅析Spring IoC源码(四)分析BeanPostProcessor(1)
- 浅析Spring IoC源码(四)分析BeanPostProcessor(1)
- Spring IOC 容器源码分析 - 创建单例 bean 的过程
- Spring Ioc 源码分析(二)- bean的实例化
- Spring IOC容器bean初始化源码分析
- Spring IOC 容器源码分析 - 获取单例 bean
- Spring源码分析1--IoC容器载入Bean定义资源
- spring IOC源码分析(一)bean工厂的创建加载过程
- 【spring源码分析】BeanDefinitionRegistryPostProcessor接口可自定义bean加入IOC
- Spring Ioc创建之BeanFactory创建源码分析
- Spring IOC 容器源码分析 - 创建原始 bean 对象
- Spring对注解(Annotation)处理源码分析1——扫描和读取Bean定义
- Spring源码分析:Bean加载流程概览及配置文件读取
- Spring源码分析:Spring中的后置处理器BeanPostProcessor
- Spring源码 - 注册bean信息到ioc容器
- (十)Spring事务处理 - IoC容器的事务处理源码分析