1.實現DisposableBean介面的destroy
2.在bean類的方法上增加@PreDestroy方法,那麼這個方法會在DisposableBean.destory方法前觸發
3.實現SmartLifecycle介面的stop方法
package com.wyf.service;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.Lifecycle;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
@Component
public class UserService implements DisposableBean, SmartLifecycle {
boolean isRunning = false;
@Override
public void destroy() throws Exception {
System.out.println(this.getClass().getSimpleName()+" is destroying.....");
}
@PreDestroy
public void preDestory(){
System.out.println(this.getClass().getSimpleName()+" is pre destory....");
}
@Override
public void start() {
System.out.println(this.getClass().getSimpleName()+" is start..");
isRunning=true;
}
@Override
public void stop() {
System.out.println(this.getClass().getSimpleName()+" is stop...");
isRunning=false;
}
@Override
public boolean isRunning() {
return isRunning;
}
}
那麼這個時候我們去啟動一個spring容器
package com.wyf;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
}
}
這個時候其實銷燬方法是不會執行的,我們可以通過,呼叫close方法觸發或者呼叫registerShutdownHook註冊一個勾點來在容器關閉時觸發銷燬方法
package com.wyf;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//新增一個關閉勾點,用於觸發物件銷燬操作
context.registerShutdownHook();
context.close();
}
}
實際上我們去檢視原始碼會發現本質上這兩種方式都是去呼叫了同一個方法org.springframework.context.support.AbstractApplicationContext#doClose
@Override
public void registerShutdownHook() {
if (this.shutdownHook == null) {
// No shutdown hook registered yet.
this.shutdownHook = new Thread(SHUTDOWN_HOOK_THREAD_NAME) {
@Override
public void run() {
synchronized (startupShutdownMonitor) {
doClose();
}
}
};
Runtime.getRuntime().addShutdownHook(this.shutdownHook);
}
}
registerShutdownHook
方法其實是建立了一個jvm shutdownhook(關閉勾點),這個勾點本質上是一個執行緒,他會在jvm關閉的時候啟動並執行執行緒實現的方法。而spring的關閉勾點實現則是執行了org.springframework.context.support.AbstractApplicationContext#doClose
這個方法去執行一些spring的銷燬方法
@Override
public void close() {
synchronized (this.startupShutdownMonitor) {
doClose();
// If we registered a JVM shutdown hook, we don't need it anymore now:
// We've already explicitly closed the context.
if (this.shutdownHook != null) {
try {
Runtime.getRuntime().removeShutdownHook(this.shutdownHook);
}
catch (IllegalStateException ex) {
// ignore - VM is already shutting down
}
}
}
}
而close方法則是執行直接執行了doClose
方法,並且在執行之後會判斷是否註冊了關閉勾點,如果註冊了則登出掉這個勾點,因為已經執行過doClose了,不應該再執行一次
@Override
public void close() {
synchronized (this.startupShutdownMonitor) {
doClose();
// If we registered a JVM shutdown hook, we don't need it anymore now:
// We've already explicitly closed the context.
if (this.shutdownHook != null) {
try {
Runtime.getRuntime().removeShutdownHook(this.shutdownHook);
}
catch (IllegalStateException ex) {
// ignore - VM is already shutting down
}
}
}
}
@SuppressWarnings("deprecation")
protected void doClose() {
// Check whether an actual close attempt is necessary...
//判斷是否有必要執行關閉操作
//如果容器正在執行中,並且以CAS的方式設定關閉標識成功,則執行後續關閉操作,當然這個標識僅僅是標識,並沒有真正修改容器的狀態
if (this.active.get() && this.closed.compareAndSet(false, true)) {
if (logger.isDebugEnabled()) {
logger.debug("Closing " + this);
}
if (!NativeDetector.inNativeImage()) {
LiveBeansView.unregisterApplicationContext(this);
}
try {
// Publish shutdown event.
//釋出容器關閉事件,通知所有監聽器
publishEvent(new ContextClosedEvent(this));
}
catch (Throwable ex) {
logger.warn("Exception thrown from ApplicationListener handling ContextClosedEvent", ex);
}
// Stop all Lifecycle beans, to avoid delays during individual destruction.
//如果存在bean實現的Lifecycle介面,則執行onClose(),lifecycleProcessor會對所有Lifecycle進行分組然後分批執行stop方法
if (this.lifecycleProcessor != null) {
try {
this.lifecycleProcessor.onClose();
}
catch (Throwable ex) {
logger.warn("Exception thrown from LifecycleProcessor on context close", ex);
}
}
// Destroy all cached singletons in the context's BeanFactory.
//銷燬所有快取的單例bean
destroyBeans();
// Close the state of this context itself.
//關閉bean工廠
closeBeanFactory();
// Let subclasses do some final clean-up if they wish...
//為子類預留的方法允許子類去自定義一些銷燬操作
onClose();
// Reset local application listeners to pre-refresh state.
//將本地應用程式偵聽器重置為預重新整理狀態。
if (this.earlyApplicationListeners != null) {
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}
// Switch to inactive.
//設定上下文到狀態為關閉狀態
this.active.set(false);
}
}
org.springframework.beans.factory.DisposableBean#destroy
方法的bean的destroy方法,以及其帶有@PreDestroy註解的方法。tips:其實Lifecycle不算是bean銷燬時的操作,而是bean銷燬前操作,這個是bean生命週期管理實現的介面,相當於spring除了自己去對bean的生命週期管理之外,還允許你通過這個介面來在bean的不同生命週期階段去執行各種邏輯,我個人理解和另外兩種方法的本質上是差不多的,只是誰先執行誰後執行的問題,Lifecycle只不過是把這些能力整合在一個介面裡面方便管理和使用。
本文只是簡單看了一下原始碼,很多細節沒有深究,必然會有不少錯誤的地方但是總體邏輯是沒問題的,如果您覺得有些地方不對,歡迎指教。