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");
- }
- }
分享到:
相关推荐
藏区特产销售平台--论文.zip
文件放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载
该单片机项目可作为课程设计和期末大作业或者毕设,项目完整,有原理图和代码,需要的自行下载即可!
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件太大放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载
文件放服务器下载,请务必到电脑端资源详情查看然后下载