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");
- }
- }
分享到:
相关推荐
在Spring框架中,Bean生命周期控制是一项关键特性,它允许开发者对Bean从创建到销毁的整个过程进行精细管理。本文将深入探讨Spring Bean的生命周期,并通过实际示例来演示如何运用这些概念。 首先,Spring Bean的...
Professional Application Lifecycle Management with Visual Studio 2013 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
HP Application Lifecycle Management QC 11安装配置要求,包括操作系统,应用程序服务器,数据库服务器,web服务器。
**产品生命周期管理**(Product Lifecycle Management,简称PLM),是一种集成化的信息系统,旨在帮助制造型企业全面管理和优化从产品概念设计到报废回收整个生命周期中的所有数据、信息和流程。它不仅仅是对产品的...
根据给定的文件信息,我们可以深入探讨“Agile Product Lifecycle Management(PLM)”这一主题。Agile PLM是一种先进的产品生命周期管理解决方案,由Oracle公司提供,旨在帮助企业更有效地管理和优化从产品概念到...
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...
Spring Bean 是 Spring 框架的核心概念,它代表了应用程序中的一个对象,这个对象可以被 Spring 容器管理,包括创建、初始化、装配、销毁等生命周期过程。在 Spring 中,Bean 定义是由 `BeanDefinition` 接口来表示...
hp非常有名的qc升级版ALM11.5 的中文版下载地址。。 直接将地址拷贝进迅雷即可下载
信息生命周期管理(ILM)是一种综合性的管理框架,旨在应对信息爆炸性增长、成本控制、信息战略价值、信息流动性、信息可用性、法规遵循以及其他公司治理需求等多重挑战。ILM 不仅关注信息的存储和保护,还强调信息...
Professional Application Lifecycle Management with Visual Studio 2013 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 ...
### SAP HANA Platform Lifecycle Management Tool (hdblcm) 故障排除指南 #### 概述 SAP HANA 是一款高性能、内存中的数据平台,它为实时数据分析提供了强大的支持。为了帮助用户更好地管理SAP HANA环境,SAP提供...
### HP Application Lifecycle Management (ALM) 11.00 配置详细过程 #### HP ALM 概述 HP Application Lifecycle Management (ALM) 11.00 是一款全面的应用程序生命周期管理解决方案,旨在帮助IT团队有效地管理和...
Professional Application Lifecycle Management with Visual Studio 2013 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
在Java开发中,Spring框架是应用最广泛的IoC(Inversion of Control)和DI(Dependency Injection)容器之一。Bean在Spring框架中扮演着核心角色,它们代表了应用程序中的对象。本篇文章将深入探讨Spring Bean的生命...
- Bean lifecycle interfaces - Customizing beans using BeanPostProcessors and BeanFactoryPostProcessors - Bean definition inheritance - JSR 250's and 330's annotations for dependency injection - ...
标题中的"spring-lifecycle"暗示了这个项目专注于探索Spring框架中bean的生命周期管理。让我们深入了解一下Spring框架的生命周期及其相关的知识点。 在Spring中,bean的生命周期是指从创建到销毁的整个过程,它包括...
HP Application Lifecycle Management (ALM)是一个软件生命周期管理工具,它帮助IT团队在软件开发、测试、交付和维护过程中实现高质量和高效率的工作。ALM 11.0提供了增强的功能,以及对敏捷和传统迭代方法的支持。 ...
Oracle Product Lifecycle Management (PLM) 实施指南是针对企业管理和优化产品创新过程的重要参考资料,尤其在11i版本中,该指南提供了详细的实施步骤和最佳实践。本指南由Oracle公司编写,版权归属Oracle,旨在...