Listing 5-17. Configuring the ShutdownHookBean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="destructiveBean"
class="com.apress.prospring3.ch5.lifecycle.DestructiveBeanWithInterface">
<property name="filePath">
<value>d:/temp/test.txt</value>
</property>
</bean>
<bean id="shutdownHook"
class="com.apress.prospring3.ch5.interaction.ShutdownHookBean"/>
</beans>
Notice that no special configuration is required. Listing 5-18 shows a simple example application
that uses the ShutdownHookBean to manage the destruction of singleton beans.
Listing 5-18. Using ShutdownHookBean
package com.apress.prospring3.ch5.interaction;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.apress.prospring3.ch5.lifecycle.DestructiveBeanWithInterface;
public class ShutdownHookBeanExample {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("interaction/shutdownHook.xml");
ctx.refresh();
DestructiveBeanWithInterface bean =
(DestructiveBeanWithInterface) ctx.getBean("destructiveBean");
}
}
This code should seem quite familiar to you. When Spring bootstraps the ApplicationContext and
the destructiveBean defined in the configuration (since the bean implemented the
ApplicationContextAware interface), Spring passes the reference of the ApplicationContext to the
shutdownHook bean for registering the shutdown hook. Running this example yields the following output,
as expected:
Initializing Bean
Destroying Bean
As you can see, even though no calls to destroy() are in the main application, the ShutdownHookBean
is registered as a shutdown hook, and it calls destroy() just before the application shuts down.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home