It's possible to intercept creation and distruction of a bean by implementing two interfaces:
public class Employee implements InitializingBean, DisposableBean { @Override public void destroy() throws Exception { // Something goes here } @Override public void afterPropertiesSet() throws Exception { // Something goes here } }
However to intercept the distruction, you also need to set an hook by calling registerShutdownHook() after the creation of the application context:
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); ctx.registerShutdownHook(); Employee employee = (Employee)ctx.getBean("employee");
There is also a way to set the callbacks in your xml configuration:
<bean id="employee" class="org.davis.Employee" init-method="doCreationOps" destroy-method="doDistructionOps"> </bean>
You can also add the two methods to all the beans in that way:
<beans default-init-method="doCreationOps" default-destroy-method="doDistructionOps">
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.