java - @PreDestroy method of a Spring singleton bean not called -
i have spring bean defined in beans.xml
follows:
<context:annotation-config /> [...] <bean id="mybackend" class="mycompany.backendbean" scope="singleton" />
inside bean 2 methods, must executed @ start , before termination of web application:
public class backendbean implements ibackend { private static final logger logger = loggerfactory .getlogger(backendbean.class); @postconstruct public void init() { logger.debug("init"); } @predestroy public void destroy() { logger.debug("destroy"); } }
when run server (mvn jetty:run
), can see output of init
method in console, conclude init
method executed.
when press ctrl-c
, jetty starts shut down, don't see output of destroy
method.
what should change in order destroy
method executed, when application terminated?
for spring call @predestroy
callback method when application shuts down, have add shutdown hook , close application context in. attach hook jvm using runtime.getruntime().addshutdownhook(thread)
or jetty if provides such api. here how you'd jvm shutdown hook:
final applicationcontext appcontext = ... // create application context // using 1 of various application context classes runtime.getruntime().addshutdownhook(new thread() { public void run() { appcontext.close(); }});
Comments
Post a Comment