InitializingBeanとDisposableBean
1221 ワード
1.springがbeanを初期化するとき、beanがInitializingBeanインタフェースを実装すると、afterPropertiesSet()メソッドが自動的に呼び出されます.
2.springがbeanを破棄する場合、beanがDisposableBeanインタフェースを実装すると、destroy()メソッドが自動的に呼び出されます.
次の操作を行います.
InitializingBean:afterPropertiesSet()--サービス起動時に自動的に呼び出され、webservice、タイミングタスクなどを起動します
DisposableBean:destroy()--afterPropertiesSetで開始したすべてのタスクを破棄します.
3.コード
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{
...
}
}