In this post we’ll see where and how to use @PostConstruct and @PreDestroy annotations with in the Spring bean life cycle.
Spring @PostConstruct annotation
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.
Any method annotated with @PostConstruct is treated as an initialization callback which is hooked with container’s management of the bean lifecycle. Such method is called to perform any bean initialization once the bean is instantiated by the container.
Spring @PreDestroy annotation
The PreDestroy annotation is used on a methods that is called when bean instance is in the process of being removed by the container.
Any method annotated with @ PreDestroy is treated as a destruction callback which lets a bean get a callback when the container that contains it is destroyed. That gives a chance to perform some cleanup before the bean is destroyed.
Spring @PostConstruct and @PreDestroy annotations example
Note that for using @PostConstruct and @PreDestroy annotations javax annotation API is needed. We need to add dependency for this explicitly Java 9 onward.
<dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency>
Here is a class with methods annotated with @PostConstruct and @PreDestroy annotations.
import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.stereotype.Component; @Component public class MyBean { public void myMethod() { System.out.println("In MyMethod of MyBean class"); } @PostConstruct public void initMethod() { System.out.println("calling init method for post construct"); } @PreDestroy public void destroyMethod() { System.out.println("calling destroy method for pre destroy"); } }AppConfig class
@Configuration @ComponentScan("com.knpcode.springexample") public class AppConfig { }
Please change the base packages to scan as per your package structure.
Class with main method to run the example.
public class App { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); context.close(); } }
As you can see this class is not doing much just creating the context using the AppConfig as configuration and then closing the context.
When the context is created all the beans are instantiated so this is the point where method annotated with @PostConstruct should be called. Method annotated with @PreDestroy should be called when the context is closed.
Output19:20:08.649 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myBean' call init method for post construct call destroy method for pre destroy
That's all for the topic Spring @PostConstruct and @PreDestroy Annotation. If something is missing or you have something to share about the topic please write a comment.
You may also like
No comments:
Post a Comment