2017-08-15 60 views
2

bean的初始化/清除有多種不同的方式可以完成。還有其他事件機制。除了所有這些,爲什麼Spring有啓動上下文的概念,我認爲這個概念調用了bean的相應啓動和停止方法。爲什麼在春季啓動和停止bean的概念

爲了驗證這一點,我有一段代碼,看起來像這樣 -

public class Car implements InitializingBean, DisposableBean, SmartLifecycle { 

    private Engine engine; 

    private volatile boolean isRunning = false; 


    @Override 
    public void afterPropertiesSet() throws Exception { 
     logger.debug("Car -- afterPropertiesSet"); 
    } 

    @Override 
    public void destroy() throws Exception { 
     logger.debug("Car -- destroy"); 
    } 

    @PostConstruct 
    public void postConstruction() { 
     logger.debug("Car -- postConstruct"); 
    } 

    @PreDestroy 
    public void preDestruction() { 
     logger.debug("Car -- preDestroy"); 
    } 


    @Override 
    public void stop() { 
     //Note that this stop notification is not guaranteed to come before destruction: On regular shutdown, 
     //Lifecycle beans will first receive a stop notification before the general destruction callbacks are being propagated; 
     //however, on hot refresh during a context's lifetime or on aborted refresh attempts, only destroy methods will be called. 
     logger.debug("Car -- stop"); 
     isRunning = false; 
    } 

    @Override 
    public boolean isRunning() { 
     //Check whether this component is currently running. 
     //In the case of a container, this will return true only if all components that apply are currently running. 
     logger.debug("Car -- isRunning"); 
     return isRunning; 
    } 

    @Override 
    public int getPhase() { 
     //Return the phase value of this object. 
     logger.debug("Car -- getPhase"); 
     return 10; 
    } 

    @Override 
    public boolean isAutoStartup() { 
     //Returns true if this Lifecycle component should get started automatically by the container 
     //A value of false indicates that the component is intended to be started through an explicit start() call instead, 
     //analogous to a automatic Lifecycle. 
     logger.debug("Car -- isAutoStartup"); 
     return false; 
    } 

    @Override 
    public void stop(Runnable callback) { 
     logger.debug("Car -- stop - async"); 
     isRunning = false; 
     try { 

      //Sleeping for 10 seconds so that all threads 
      //get enough time to do their cleanup 
      TimeUnit.SECONDS.sleep(10); 
      logger.debug("Wait over"); 
      //Shudown complete. Regular shutdown will continue. 
      callback.run(); 
     } catch (final InterruptedException e) { 
      //Looks like we got exception while shutting down, 
      //log it or do something with it 
     } 
    } 

    @Override 
    public void start() { 
     //Start this component. 
     //Should not throw an exception if the component is already running. 
     logger.debug("Car -- start"); 
     isRunning = true; 
    } 
} 

首先,我對isAutoStartUp返回真值那麼我嘗試返回false。下面的圖片是2次運行的日誌文件的比較。 Difference of log files 無論autostartup是true/false,代碼運行良好。

+0

*該代碼可以正常運行,但是一些更復雜的bean可能需要在這些生命週期回調中執行某些操作,並且如果嘗試在啓動之前嘗試使用它們,可能無法正常工作(並且可能會留下泄漏的資源在沒有機會整齊關閉的情況下被移除)。 – Thilo

+0

所以問題是我們在這些方法中做了什麼 - 關於什麼地方的最佳實踐? – John

回答

0

這是爲什麼當你的豆與服務有關。您可能想知道它們何時啓動成功,並且在例行應用程序關閉時也有正確的關閉過程/清理/拆卸過程。