Spring Bean Life Cycle - @PostConstruct and @PreDestroy
Abid Anjum
Senior Full Stack Developer Architect | Java Spring Boot Microservices | Angular & React | Mobile Apps Engineer Android IOS Flutter | Asp.net Core C# | BI Expert | GIS Expertise
In Spring framework, we can manage lifecycle of a bean by using method-level annotations?@PostConstruct?and?@PreDestroy.
The?@PostConstruct?annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.
The?@PreDestroy?annotation is used on methods as a callback notification to signal that the instance is in the process of being removed by the container.
The following examples demonstrate the use of?@PostConstruct?and?@PreDestroy?annotations.
Consider the?MailService?bean, whose?init()?and?destroy()?methods are annotated with?@PostConstruct?and?@PreDestroy?annotations respectively.
MailService.java
import java.util.HashMap
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Component;
/**
* @author anjum
*/
@Component
public class MailService {
private Map<String, String> map=null;
public MailService() {
map=new HashMap<>();
}
public void send(String mailTo){
//Code for sending mail
System.out.println("Inside send method - "+mailTo);
}
@PostConstruct
public void init() {
map.put("host", "mail.example.com");
map.put("port", "25");
map.put("from", "[email protected]");
System.out.println("Inside init method - "+map);
}
@PreDestroy
public void destroy() {
map.clear();
System.out.println("Inside destroy method - "+map);
}
};
Create?AppConfig?class and write the following code in it.
App.Config.java
package com.boraji.tutorial.spring
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author anjum
*/
@Configuration
@ComponentScan(basePackages = "com.anjum.tutorial.spring")
public class AppConfig {
};
Note -?@ComponentScan?annotation scans all beans, whose class is annotated by the?@Component?annotation in a package, specified by?basePackages?attribute.
Create main class and run application.
MainApp.java
import org.springframework.context.annotation.AnnotationConfigApplicationContext
/**
* @author anjum
*/
public class MainApp {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
// Send mail 1
MailService mailService1 = context.getBean(MailService.class);
mailService1.send("[email protected]");
// Send mail 2
MailService mailService2 = context.getBean(MailService.class);
mailService2.send("[email protected]");
context.close();
}
};
Output
Inside init method - {port=25, host=mail.example.com, [email protected]}
Inside send method - [email protected]
Inside send method - [email protected]
Inside destroy method - {}
As you can see, the?init()?and?destroy()?methods of?MailService?bean are called only once, when the scope of bean is singleton (default scope).
In case of prototype scope, the destroy method of the?MailService?bean will not work. You can try this by downloading the sources from the below download link.
Java Developer | Spring Boot | Microservices | AWS | Docker | Overall Experience 2 years
1 年thank you!