java - Why the concept of starting and stopping beans in spring -


there different ways in bean initializaiton/cleanup can done. there other event mechanisms. along why spring have concept of starting context believe invokes respective start , stop methods of beans.

to test this, have piece of code looks -

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 stop notification not guaranteed come before destruction: on regular shutdown,          //lifecycle beans first receive stop notification before general destruction callbacks being propagated;          //however, on hot refresh during context's lifetime or on aborted refresh attempts, destroy methods called.         logger.debug("car -- stop");         isrunning = false;     }      @override     public boolean isrunning() {         //check whether component running.         //in case of container, return true if components apply running.         logger.debug("car -- isrunning");         return isrunning;     }      @override     public int getphase() {         //return phase value of object.         logger.debug("car -- getphase");         return 10;     }      @override     public boolean isautostartup() {         //returns true if lifecycle component should started automatically container          //a value of false indicates component intended started through explicit start() call instead,          //analogous automatic lifecycle.         logger.debug("car -- isautostartup");         return false;     }      @override     public void stop(runnable callback) {         logger.debug("car -- stop -  async");         isrunning = false;          try {             //sleeping 10 seconds threads             //get enough time cleanup              timeunit.seconds.sleep(10);            logger.debug("wait over");            //shudown complete. regular shutdown continue.            callback.run();        } catch (final interruptedexception e) {            //looks got exception while shutting down,             //log or        }     }      @override     public void start() {         //start component.         //should not throw exception if component running.         logger.debug("car -- start");         isrunning = true;     } } 

first return true value isautostartup try returning false. image below comparison of log files of 2 runs. difference of log files , irrespective of whether autostartup true/false, code runs fine.

this when beans related services. may want know when have started successfully, , have correct shutdown procedure/ cleanup/ teardown on routine application shutdown.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -