In general, two lifecycle events are particularly relevant to a bean: post-initialization and pre-destruction. Both of these lifecycle events are only fired for beans that are singletons.
1. post-initialization event. 2 ways to implement it: method-based and interface-based.
1) method based. Specify the init method name in the configuration file (init-method="method name").
xml
- <bean id="simpleBean1"
- class="SimpleBean"
- init-method="init">
- <property name="age">
- <value>100value>
- property>
- bean>
2) interface based. Implement InitializingBean interface and define afterPropertiesSet() method.
java
- public class SimpleBeanWithInterface implements InitializingBean {
-
- public void afterPropertiesSet() throws Exception {
- System.out.println("Initializing bean");
- }
- }
2. pre-destruction event. 2 ways to implement it: method-based and interface-based.
1) method based. Specify the destroy mothod in the config file. (destroy-method="method name")
xml
- <bean id="destructiveBean"
- class="DestructiveBean"
- destroy-method="destroy">
- <property name="filePath">
- <value>d:/tmp/test.txtvalue>
- property>
- bean>
2) interface based. Implement DisposableBean interface and define destroy() method.
java
- public class DestructiveBeanWithInterface implements DisposableBean {
-
- public void destroy() {
-
- System.out.println("Destroying Bean");
- }
- }
Destruction callbacks are not fired automatically. You need to remember to call destroySingletons() before the application is closed. When the application runs as a servlet, you can simple call the method in the servlet's destroy() method. However, in a stand-alone application, you need to take advantage of the Java's shutdown hook, a thread executed just before the application shuts down.
java
- public class ShutdownHook implements Runnable {
-
- private ConfigurableListableBeanFactory factory;
-
- public ShutdownHook(ConfigurableListableBeanFactory factory) {
- this.factory = factory;
- }
-
- public void run() {
- System.out.println("Destroying Singletons");
- factory.destroySingletons();
- System.out.println("Singletons Destroyed");
- }
-
- }
Register this Thread as a shutdown hook using Runtime.addShutdownHook().
java
- public class ShutdownHookExample {
-
- public static void main(String[] args) {
- ConfigurableListableBeanFactory factory = new XmlBeanFactory(
- new FileSystemResource(
- "./disposeInterface.xml"));
-
- Runtime.getRuntime().addShutdownHook(
- new Thread(new ShutdownHook(factory)));
- DestructiveBeanWithInterface bean =
- (DestructiveBeanWithInterface)factory.getBean("destructiveBean");
- }
- }
分享到:
相关推荐
NULL 博文链接:https://cywhoyi.iteye.com/blog/1945152
Agile Product Lifecycle Management AdministratGuide
PLM资料。英文的Product Lifecycle Management产品生命周期的教材
Professional Application Lifecycle Management with Visual Studio 2013 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
HP Application Lifecycle Management QC 11安装配置要求,包括操作系统,应用程序服务器,数据库服务器,web服务器。
Pro Visual Studio Team System Application Lifecycle Management即Visual Studio Team System应用程序声明周期专业管理。
Beginning Application Lifecycle Management is a guide to an area of rapidly growing interest within the development community: managing the entire cycle of building software. ALM is an area that spans...
Product lifecycle management makes it possible to command the whole lifespan of a product and the information connected with it. Efficient product lifecycle management enables companies to compete ...
HP Application Lifecycle Management
hp非常有名的qc升级版ALM11.5 的中文版下载地址。。 直接将地址拷贝进迅雷即可下载
Professional Application Lifecycle Management with Visual Studio 2013 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 ...
Oracle Product Lifecycle Management Implementation Guide Release 11i Part No. B19350-02
信息生命周期管理架构描述如果对信息从产生到最终销毁进行管理,满足企业对信息管理的要求,帮助企业降低总体成本,提供资源利用率
人才生命周期管理 Talent Lifecycle Management_.pdf
Professional Application Lifecycle Management with Visual Studio 2013 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Troubleshooting note for SAP HANA platform lifecycle management tool hdblcm
WIN2003R2+SQL Server2005 SP4+HP Application Lifecycle Management配置详细过程,绝对安装成功。
- Bean lifecycle interfaces - Customizing beans using BeanPostProcessors and BeanFactoryPostProcessors - Bean definition inheritance - JSR 250's and 330's annotations for dependency injection - ...
Agile Application Lifecycle Management (ALM) is a comprehensive development lifecycle that encompasses essential Agile principles and guides all activities needed to deliver successful software or ...