@PostConstruct, @PreDestroy and initMethod, destroyMethod, InitializingBean, DisposableBean concepts, differences and execution order

@PostConstruct, @PreDestroy and initMethod, destroyMethod, InitializingBean, DisposableBean concepts, differences and execution order

@PostConstruct

Definition related

  1. This annotation is added in the Java EE5 specification, not spring. Spring refers to following this specification and has a certain role in the Servlet life cycle. It is usually some initialization operations, but initialization may depend on other components injected. , so wait until all dependencies are loaded before executing
  2. @PostConstruct annotation is used for methods executed after dependency injection is complete
  3. Methods may be modified with final

usage

Declare a void method in a class, of course, it can be of other types, but it has no practical significance. Generally, it is a non-static void method.

 @PostConstruct
    public final  void initForPostConstruct(){
    
    
        System.out.println("initForPostConstruct 方法执行了!");
    }

@PreDestroy

Definition related

  1. This annotation is also added in the Java EE5 specification, not spring. Spring means that following this specification has a certain role in the Servlet life cycle. It is usually some operations before destruction.
  2. The @PreDestroy annotation is used as a callback notification on the method to indicate that the instance is being deleted by the container
  3. A method annotated with @PreDestroy is usually used to release resources it has been holding, or to do garbage collection
  4. Methods may be modified with final

usage

Like @PostConstruct, declare a void method in the class, of course, it can be of other types, but it has no practical significance. Generally, it is a non-static void method.

 @PreDestroy
    public void destroyForPreDestroy(){
    
    
        System.out.println("destroyForPreDestroy 方法执行了!");
    }

initMethod和destroyMethod

Definition related

After initializing an object (bean), initialize and load some data immediately, and do some resource release and garbage collection operations before destroying the object

usage

xml method, define the init and destroy methods in the class corresponding to the bean, and use the init-method and destroy-method association in xml

    public void initForInitMethod (){
    
    
        System.out.println("initForInitMethod 方法执行了!");
    }

    public void destroyFordestroyMethod(){
    
    
        System.out.println("destroyFordestroyMethod 方法执行了!");
    }
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans.xsd">  

<bean id="initOrDestroyTest" class="com.cyyit.bean.TestBean" init-method="init" destroy-method="destroy">
</bean>
</beans>

The javaConfig method, the effect is equivalent to the xml method

@Configuration
public class config {
    
    
    @Bean(initMethod = "initForInitMethod",destroyMethod ="destroyFordestroyMethod")
    public TestBean testBean(){
    
    
        return  new TestBean();
    }
}

InitializingBean

The IinitializingBean interface provides an initialization method for beans. It only includes the afterPropertiesSet method. All classes that inherit this interface will execute this method when initializing the bean.

DisposableBean

The DisposableBean interface provides a way for beans to destroy the Bean method. It only includes the destroy method. All classes that inherit this interface will execute this method when the bean is destroyed.

execution order

Test: Define a TestBean as follows

public class TestBean implements InitializingBean, DisposableBean {
    
    
    private String name;
    private Integer age;
    @Autowired
    private TestBean1 testBean1;

    @PostConstruct
    public final  void initForPostConstruct(){
    
    
        System.out.println("initForPostConstruct 方法执行了!");
    }

    @PreDestroy
    public void destroyForPreDestroy(){
    
    
        System.out.println("destroyForPreDestroy 方法执行了!");
    }

    public void initForInitMethod (){
    
    
        System.out.println("initForInitMethod 方法执行了!");
    }

    public void destroyFordestroyMethod(){
    
    
        System.out.println("destroyFordestroyMethod 方法执行了!");
    }

    @Override
    public void destroy() throws Exception {
    
    
        System.out.println("destroy 方法执行了!");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
    
    
        System.out.println("afterPropertiesSet 方法执行了!");
    }
}

Execution of the main function running results:
insert image description here
Conclusion: As can be seen from the above, in fact, the three methods are used to perform some related operations in the initialization and destruction phases in the life cycle of springBean. Configure as needed. In addition, @PostConstruct and @PreDestroy are Java's own annotations, not spring's. Spring just follows such a specification. The three execution sequences are:

Initialization: @PostConstruct>InitializingBean>initMethod

Destroy: @PreDestroy>DisposableBean>destroyMethod

Guess you like

Origin blog.csdn.net/qq_38338409/article/details/120605901