为什么在春天开始和停止豆的概念

时间:2017-08-15 07:03:57

标签: java spring spring-bean

可以通过不同的方式进行bean初始化/清理。还有其他事件机制。除此之外,为什么spring有启动上下文的概念,我相信它会调用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返回true值,然后尝试返回false。下图是2次运行的日志文件的比较。 Difference of log files 无论autostartup是真还是假,代码运行正常。

1 个答案:

答案 0 :(得分:0)

这适用于您的bean与服务相关的情况。您可能想知道它们何时成功启动,并且在常规应用程序关闭时也有正确的关闭程序/清理/拆卸。