This is the third day of my participation in the August Text Challenge.More challenges in August
1. @PostConstruct
and@PreDestroy
@postConstruct and @PreDestroy are Java’s own annotations, as defined in the JSR-250 specification
The @postconstruct annotation is used to decorate a non-static void() method. Methods decorated with the @postconstruct annotation run when the server loads the Servlet and are executed by the server only once. Methods decorated with the @postconstruct annotation are usually executed after the constructor and before the init() method
The @PostConstruct annotation is used in the Spring framework, and its methods are executed in the following order during bean initialization:
Constructor → @autowired → @postconstruct
The @predestroy annotation is also provided by Java and is defined in the JSR-250 specification.
Methods decorated with the @predestroy annotation run when the server uninstalls the Servlet and are called only once by the server, similar to the destroy() method of the Servlet. Methods decorated with the @predestroy annotation are executed after destroy() and before the Servlet is completely uninstalled. The execution sequence is as follows:
Destroy () → @predestroy → Destroy () → bean destroy
Case study:
import lombok.Data;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* Description:
*
* @author jack
* @date2021/8/6 22:26 * /
@Data
public class Car {
private String name;
public Car(a) {
System.out.println("Car() constructor executed");
}
@PostConstruct
public void postConstruct(a) {
System.out.println("Performed the Car#postConstruct initialization method");
}
@PreDestroy
public void preDestroy(a) {
System.out.println("Car#preDestroy destruction method executed"); }}Copy the code
import com.example.beans.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/** * Description: * Configuration class **@author jack
* @date2021/8/6 "* /
@Configuration
public class MainConfig {
@Bean
public Car car(a) {
return newCar(); }}Copy the code
Start the class
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Description:
*
* @author jack
* @date2021/8/6 "* /
public class ApplicationTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext app = newAnnotationConfigApplicationContext(MainConfig.class); app.close(); }}Copy the code
The running results are as follows:
2. ImplementInitializingBean
和 DisposableBean
interface
InitializingBean interface, which provides post-property initialization methods for the bean. It includes only afterPropertiesSet methods. Classes that inherit this interface execute this method after the bean’s properties are initialized.
The DisposableBean interface is called before the end of the bean’s life cycle
DisposableBean
Precautions for using interfaces
The life cycle of a multi-instance bean is not managed by the Spring container, because methods DisposableBean interface is called by the Spring container, so it is of no significance for a multi-instance bean to implement The DisposableBean interface, because the corresponding methods will not be called at all. Of course, specifying the destroy method in the XML configuration file also makes no sense. Therefore, in the case of multi-instance beans, Spring does not automatically invoke the bean destruction method.
Case in singleton case:
import lombok.Data;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* Description:
*
* @author jack
* @date2021/8/6 22:26 * /
@Data
public class Car implements InitializingBean.DisposableBean {
private String name;
public Car(a) {
System.out.println("Car() constructor executed");
}
@PostConstruct
public void postConstruct(a) {
System.out.println("Performed the Car#postConstruct initialization method");
}
@PreDestroy
public void preDestroy(a) {
System.out.println("Car#preDestroy destruction method executed");
}
@Override
public void afterPropertiesSet(a) throws Exception {
System.out.println("Performed the Car#afterPropertiesSet initialization method");
}
@Override
public void destroy(a) throws Exception {
System.out.println("Execute the Car#destroy destroy method"); }}Copy the code
The test code remains unchanged, and the test results are as follows
Initialize the execution process
Constructor → @autowired → @postconstruct -> InitializingBean#afterPropertiesSet
Destruction execution process
@predestroy (commented method) -> DisposableBean#destroy (bean implementation method)
Cases in multiple cases:
import com.example.beans.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
/** * Description: * Configuration class **@author jack
* @date2021/8/6 "* /
@Configuration
public class MainConfig {
@Scope(scopeName = "prototype")
@Bean
public Car car(a) {
return newCar(); }}Copy the code
import com.example.beans.Car;
import com.example.config.MainConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Description:
*
* @author jack
* @date2021/8/6 "* /
public class ApplicationTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext app = newAnnotationConfigApplicationContext(MainConfig.class); Car car = app.getBean(Car.class); System.out.println(car); app.close(); }}Copy the code
The test results are as follows:
You can see that the bean’s destruction method is not called after the container’s destruction
Use 3.@Bean
Attributes of annotations set initialization and destruction methods
import lombok.Data;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* Description:
*
* @author jack
* @date2021/8/6 22:26 * /
@Data
public class Car implements InitializingBean.DisposableBean {
private String name;
public Car(a) {
System.out.println("Car() constructor executed");
}
@PostConstruct
public void postConstruct(a) {
System.out.println("Performed the Car#postConstruct initialization method");
}
@PreDestroy
public void preDestroy(a) {
System.out.println("Car#preDestroy destruction method executed");
}
@Override
public void afterPropertiesSet(a) throws Exception {
System.out.println("Performed the Car#afterPropertiesSet initialization method");
}
@Override
public void destroy(a) throws Exception {
System.out.println("Execute the Car#destroy destroy method");
}
public void beanInit(a) {
System.out.println("Performed the Car#beanInit initialization method");
}
public void beanDestroy(a) {
System.out.println("Car#beanDestroy destruction method executed"); }}Copy the code
import com.example.beans.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/** * Description: * Configuration class **@author jack
* @date2021/8/6 "* /
@Configuration
public class MainConfig {
@Bean(initMethod = "beanInit", destroyMethod = "beanDestroy")
public Car car(a) {
return newCar(); }}Copy the code
The test code remains unchanged, and the test results are as follows
conclusion
Initialize the execution process
Constructor → @autowired → @postconstruct -> InitializingBean#afterPropertiesSet -> InitMethod ()(bean initialization method)
Destruction execution process
#destroy (bean implementation method) -> destoryMethod()(bean destruction method)
Reference: liayun.blog.csdn.net/article/det…