Spring Lifecycle @PostConstruct and @PreDestroy annotations
The @PostConstruct and @PreDestroy are standard Java JSR-250 lifecycle annotations. Spring supports these annotations using the CommonAnnotationBeanPostProcessor
. By annotation void no-argument signature methods with these annotations, your methods are eligible for initialization and destruction callback methods. These callback methods will be called only when the singleton bean is managed by the spring container.
@PostConstruct and @PreDestroy
You can annotate void no-argument signature methods with the @PostConstruct and @PreDestroy methods. When annotated spring will call the annotated method when the container is initializing the bean and before the bean is destroyed respectively.
package com.memorynotfound.spring.core.lifecycle;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Monitor {
private int timeout;
public Monitor(int timeout) {
this.timeout = timeout;
}
@PostConstruct
public void init() throws Exception {
System.out.println("- - - initializing monitor bean using @PostConstruct");
System.out.println("timeout: " + timeout);
}
@PreDestroy
public void destroy() throws Exception {
System.out.println("- - - destroying monitor bean using @PreDestroy");
}
}
Configuring Spring Container
We can enable the standard Java JSR-250 lifecycle annotations by adding the <context:annotation-config/> element.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean class="com.memorynotfound.spring.core.lifecycle.Monitor">
<constructor-arg name="timeout" value="1000"/>
</bean>
</beans>
Running the Application
We bootstrap the application using the ClassPathXmlApplicationContext
. We use the ConfigurableApplicationContext
interface to allow us to use the extra lifecycle registerShutdownHook()
method. This will register a shutdown hook, meaning: when the application is destroyed, the spring container will call all the destruction methods of all cached singleton beans and release all the resources.
package com.memorynotfound.spring.core.lifecycle;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Run {
public static void main(String... args) throws InterruptedException {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
context.registerShutdownHook();
}
}
Output
The properties of the singleton beans are available in the initialization method.
- - - initializing monitor bean using @PostConstruct
timeout: 1000
- - - destroying monitor bean using @PreDestroy