When my spring web app shuts down, is there an event I can wireup to somehow that I can perform some cleanup code to empty out some pools etc.
You Could use the following
destroy-method
as @amir75 recommends@PreDestroy annotation
Implement DisposableBean and override destroy method.
All the deatails about these can be found at Disposable Callbacks.
Spring beans have a 'destroy-method' attribute, which will be invoked when you 'close' your context.
<bean id="bean1"
destroy-method="stop"
class="com.example.Bean" />
In order to close it, you'd call the close() method:
(or just shut down the container if appropriate)
Hope that helps..
Based on the JSR-250 specification the best practice to use in modern spring application is the @PreDestroy
annotation since using this approach will decouple your beans from Spring.
The non-Spring way to handle this is to write a class that implements ServletContextListener
and do your cleanup in its contextDestroyed
method. You'd add your class as a context listener in web.xml.
@PreDestroy
is the right way. –
Eyelash According to the Spring Boot logback example project, you should close the context to clean up the logging system: https://github.com/spring-projects/spring-boot/commit/10402a651f1ee51704b58985c7ef33619df2c110
Example:
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleLogbackApplication.class, args).close();
}
© 2022 - 2024 — McMap. All rights reserved.