春季生命周期回调
这是教程系列的16个部分中的13个
教程内容:
- 弹簧框架介绍
- 春季面试题
- 春天的依赖注入(IOC)
- Eclipse中的Spring Hello World示例
- 基于Spring 爪哇的配置
- 在春季通过setter方法进行依赖注入
- 在春天通过构造函数进行依赖注入
- 弹簧 Bean作用域示例
- 在春季初始化集合
- 春季自动接线
- 春天的传承
- 弹簧 ApplicationContext
- 春季生命周期回调
- 春季的BeanPostProcessors
- 弹簧中基于注释的配置
- 春季AOP教程
The 弹簧 Framework provides several callback interfaces to change the behavior 的 your 豆 in the container; they include 在itializingBean
和 一次性豆
.
弹簧 豆的生命周期很容易理解。实例化Bean时,可能需要执行一些初始化以使其进入可用状态。同样,当不再需要bean并将其从容器中删除时,可能需要进行一些清理。
初始化回调:
Implementing the 组织.springframework.beans.factory.InitializingBean
interface allows a 豆 to perform 在里面ialization work after all necessary properties on the 豆 have been set by the container. The 在itializingBean
interface specifies exactly one 方法:
1 2 3 |
虚空 afterPropertiesSet() 抛出 例外; |
在itializingBean
interface can be avoided 和 is actually discouraged since it unnecessarily couples the code to 弹簧.You have to use afterPropertiesSet(),
you can not change 名称 的 方法.There is alternative for 这个 i.e. XML-based configuration metadata.This is done using the 'init-method'
attribute 的 tag.It provides flexibility 的 changing 方法 名称.
1 2 3 4 5 6 7 8 9 10 |
<豆 ID =“ 国家Bean” 类=“ 组织.arpit.javapostsforlearning.Country” 在里面-方法=“在里面”/> 上市 类 国家{ 上市 虚空 在里面() { //做一些初始化工作 } } |
…与...完全相同…
1 2 3 4 5 6 7 8 9 10 |
<豆 ID =“ 国家Bean” 类=“ 组织.arpit.javapostsforlearning.Country”/> 上市 类 国家 实施 在itializingBean { 上市 虚空 afterPropertiesSet() { //做一些初始化工作 } } |
…但不会将代码耦合到Spring。
销毁回调:
组织.springframework.beans.factory.DisposableBean
interface allows a 豆 to get a callback when the container containing it is 破坏ed. The 一次性豆
interface specifies a single 方法:
1 2 3 |
虚空 破坏() 抛出 例外; |
一次性豆
interface can be avoided 和 is actually discouraged since it unnecessarily couples the code to 弹簧.You have to use 破坏(),
you can not change 名称 的 方法.There is alternative for 这个 i.e. XML-based configuration metadata.This is done using the 'destroy-method'
attribute 的 tag.It provides flexibility 的 changing 方法 名称.
1 2 3 4 5 6 7 8 9 10 |
<豆 ID =“ 国家Bean” 类=“ 组织.arpit.javapostsforlearning.Country” 在里面-方法=“破坏”/> 上市 类 国家{ 上市 虚空 破坏() { //做一些破坏工作(例如释放池连接) } } |
…与...完全相同…
1 2 3 4 5 6 7 8 9 10 |
<豆 ID =“ 国家Bean” 类=“ 组织.arpit.javapostsforlearning.Country”/> 上市 类 国家 实施 一次性豆{ 上市 虚空 破坏() { //做一些破坏工作(例如释放池连接) } } |
…但不会将代码耦合到Spring。
弹簧生命周期回调示例:
有关在Eclipse IDE中配置spring的信息,请参阅 你好世界的例子
1.国家/地区
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
包 组织.Arpit.爪哇postsforlearning; 上市 类 国家 { 串 国家的名字 ; 上市 串 getCountryName() { 返回 国家的名字; } 上市 虚空 setCountryName(串 国家的名字) { 这个.国家的名字 = 国家的名字; } 上市 虚空 在里面() { 系统.出.打印(“国家的初始化块”); } 上市 虚空 破坏() { 系统.出.打印(“在破坏国家”); } } |
2.ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<?XML文件 版="1.0" 编码方式=“ UTF-8”?> <豆子 XML文件ns="http://www.springframework.org/schema/beans" XML文件ns:si="http://www.w3.org/2001/XMLSchema-instance" XML文件ns:哎呀="http://www.springframework.org/schema/aop" si:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <豆 ID =“国家” 类=“ 组织.arpit.javapostsforlearning.Country” 在里面-方法=“在里面” 破坏-方法=“破坏”> <属性 名称=“国家的名字” 值=“印度”/> </豆> </豆子> |
3.LifetimeCallbacksMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
包 组织.Arpit.爪哇postsforlearning; 进口 组织.弹簧框架.语境.支持.AbstractApplicationContext; 进口 组织.弹簧框架.语境.支持.ClassPathXmlApplicationContext; 上市 类 LifetimeCallbacksMain{ 上市 静态的 虚空 主要(串[] args) { AbstractApplicationContext appContext = 新 ClassPathXmlApplicationContext(“ ApplicationContext.xml”); 国家 国家Obj = (国家) appContext.getBean(“国家”); 系统.出.打印(“国家的名字: ”+国家Obj.getCountryName()); appContext.registerShutdownHook(); } } |
在这里您需要注册一个关闭钩子 registerShutdownHook() 在AbstractApplicationContext类上声明的方法。这将确保正常关机并调用相关的destroy方法。
4.运行
1 2 3 4 5 |
在 在里面 块 的 国家 国家 名称: 印度 在 破坏 块 的 国家 |
默认的初始化和销毁方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?XML文件 版="1.0" 编码方式=“ UTF-8”?> <豆子 XML文件ns="http://www.springframework.org/schema/beans" XML文件ns:si="http://www.w3.org/2001/XMLSchema-instance" XML文件ns:哎呀="http://www.springframework.org/schema/aop" si:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" 默认-在里面-方法=“在里面” 默认-破坏-方法=“破坏” > <豆 ID =“国家” 类=“ 组织.arpit.javapostsforlearning.Country”> <属性 名称=“国家的名字” 值=“印度”/> </豆> </豆子> |
那’关于Spring生命周期回调的所有内容。