这是教程系列的16个部分中的13个
教程内容:
- 弹簧框架介绍
- 春季面试题
- 春天的依赖注入(IOC)
- Eclipse中的竞彩篮球分析 Hello World示例
- 基于竞彩篮球分析 Java的配置
- 在春季通过setter方法进行依赖注入
- 在春天通过构造函数进行依赖注入
- 弹簧 Bean作用域示例
- 在春季初始化集合
- 春季自动接线
- 春天的传承
- 弹簧 ApplicationContext
- 春季生命周期回调
- 春季的BeanPostProcessors
- 弹簧中基于注释的配置
- 春季AOP教程
The 竞彩篮球分析 Framework provides several callback interfaces to change the behavior of your bean in the container; they include 在 itializingBean
和 一次性豆
.
弹簧 bean的生命周期很容易理解。实例化Bean时,可能需要执行一些初始化以使其进入可用状态。同样,当不再需要bean并将其从容器中删除时,可能需要进行一些清理。
初始化回调:
Implementing the 组织.springframework.beans.factory.InitializingBean
interface allows a bean to perform initialization work after all necessary properties on the bean have been set by the container. The 在 itializingBean
interface specifies exactly one method:
1 2 3 |
虚空 afterPropertiesSet() 抛出 例外; |
在 itializingBean
interface can be avoided and is actually discouraged since it unnecessarily couples the code to 竞彩篮球分析.You have to use afterPropertiesSet(),
you can not change name of method.There is alternative for this i.e. XML-based configuration metadata.This is done using the 'init-method'
attribute of tag.It provides flexibility of changing method name.
1 2 3 4 5 6 7 8 9 10 |
<豆 ID=“ countryBean” 类=“ org.arpit.javapostsforlearning.Country” 在里面-方法=“在里面”/> 上市 类 国家{ 上市 虚空 在里面() { //做一些初始化工作 } } |
…与...完全相同…
1 2 3 4 5 6 7 8 9 10 |
<豆 ID=“ countryBean” 类=“ org.arpit.javapostsforlearning.Country”/> 上市 类 国家 实施 在 itializingBean { 上市 虚空 afterPropertiesSet() { //做一些初始化工作 } } |
…但不会将代码耦合到竞彩篮球分析。
销毁回调:
组织.springframework.beans.factory.DisposableBean
interface allows a bean to get a callback when the container containing it is destroyed. The 一次性豆
interface specifies a single method:
1 2 3 |
虚空 破坏() 抛出 例外; |
一次性豆
interface can be avoided and is actually discouraged since it unnecessarily couples the code to 竞彩篮球分析.You have to use 破坏(),
you can not change name of method.There is alternative for this i.e. XML-based configuration metadata.This is done using the 'destroy-method'
attribute of tag.It provides flexibility of changing method name.
1 2 3 4 5 6 7 8 9 10 |
<豆 ID=“ countryBean” 类=“ org.arpit.javapostsforlearning.Country” 在里面-方法=“破坏”/> 上市 类 国家{ 上市 虚空 破坏() { //做一些破坏工作(例如释放池连接) } } |
…与...完全相同…
1 2 3 4 5 6 7 8 9 10 |
<豆 ID=“ countryBean” 类=“ org.arpit.javapostsforlearning.Country”/> 上市 类 国家 实施 一次性豆{ 上市 虚空 破坏() { //做一些破坏工作(例如释放池连接) } } |
…但不会将代码耦合到竞彩篮球分析。
弹簧生命周期回调示例:
有关在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=“国家” 类=“ org.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=“国家” 类=“ org.arpit.javapostsforlearning.Country”> <属性 名称=“国家的名字” 值=“印度”/> </豆> </豆子> |
那’关于竞彩篮球分析生命周期回调的所有内容。