2.spring销毁bean的时候,如果bean实现了DisposableBean接口,会自动调用destroy()方法。
使用:
InitializingBean:afterPropertiesSet()--服务启动的时候会自动调用,启动webservice、定时任务等
DisposableBean:destroy()--销毁所有在afterPropertiesSet启动的任务
3.代码
public class TestSpringInit implements InitializingBean,DisposableBean{ /** *启动旁挂的webservice,定时任务... * (non-Javadoc) * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ @Override public void afterPropertiesSet() throws Exception { startAllSystemTasks(); } /** * 销毁afterPropertiesSet()方法中启动的任务 * (non-Javadoc) * @see org.springframework.beans.factory.DisposableBean#destroy() */ @Override public void destroy() throws Exception { stopAllSystemTasks(); } private void stopAllSystemTasks() throws Exception{ ... } private void startAllSystemTasks() throws Exception{ ... } }