Spring Annotation Development-comprehensive analysis of the life cycle of common annotation usage methods

Source: Internet
Author: User

This article GitHub location: https://github.com/WillVi/Spring-Annotation/

Previous article: Spring Annotation Development-comprehensive analysis of common annotations using the component Register Bean lifecycle

? Create------> Initialize-------> Destroy. The container manages the bean's lifecycle uniformly, and we can also customize the initialization and destruction methods, and the container calls our custom initialization and destruction methods when the bean is in the current life cycle.

Process:
    1. Construction (object creation)

      • Single instance: Create an object when the container is started
      • Multi-instance: Create an object at each fetch
    2. Beanpostprocessor.postprocessbeforeinitialization

      ? PS: Traverse to get all the beanpostprocessor in the container, execute beforeinitialization, one but return null, jump out of the For loop, No subsequent beanpostprocessor.postprocessorsbeforeinitialization will be executed.

    3. Initialize: Object creation is complete, and assignment is good, call initialization method ...

    4. Beanpostprocessor.postprocessafterinitialization

    5. Destroyed:

      • Single instance: When the container is closed
      • Multi-instance: The container does not manage the bean, and the container does not call the Destroy method;
Beanpostprocessor principle

The following method executes in turn:

populateBean(beanName, mbd, instanceWrapper);Assigning attributes to a bean

? Initializebean

{

`applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);`初始化前执行`invokeInitMethods(beanName, wrappedBean, mbd);`执行自定义初始化`applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);`初始化后执行

}

The use of beanpostprocessor at the bottom of spring
1. bean赋值2. 注入其他组件3. @Autowired4. 生命周期注解功能5. @Async,xxx
Use:
  1. Specify initialization and destruction methods, specify Initmethod and Destroymethod by @bean;

    Cases:@Bean(initMethod="init",destroyMethod="destroy")

  2. Disposablebean (Defining the destruction logic) by having the bean implement Initializingbean (define initialization logic)

    package cn.willvi.bean;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;import org.springframework.stereotype.Component;@Componentpublic class Man implements InitializingBean,DisposableBean{ public Man() {     System.out.println("Man创建"); } public void destroy() {     System.out.println("Man摧毁"); } public void afterPropertiesSet() throws Exception {     System.out.println("Man初始化"); }}
  3. The
  4. can use JSR250;

    1. @PostConstruct: The Bean is created and the property assignment is complete; To execute the initialization method
    2. @ Predestroy: Notifies us of cleanup work before the container destroys the bean
      package Cn.willvi.bean;import Javax.annotation.postconstruct;import Javax.annotation.predestroy;import org.springframework.stereotype.Component; @Componentpublic class Woman {public Woman () {System.out.println ("Woman create" ); } @PreDestroy public void Destroy () {System.out.println ("Execute Before Woman Destroy"), @PostConstruct public void init () {Syste M.OUT.PRINTLN ("Woman Initialization"); }}  

    ?

  5. Beanpostprocessor "Interface": The post processor of the bean, and some processing before and after the bean initialization

    1. Postprocessbeforeinitialization: Working before initialization
    2. Postprocessafterinitialization: Working after initialization
    package cn.willvi.bean;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.stereotype.Component;@Componentpublic class ManBeanPostProcessor implements BeanPostProcessor{ public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {     if(arg1.equals("man")) {         System.out.println("----------------------BeanPostProcessor初始化后执行");     }     return arg0; } public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {     if(arg1.equals("man")) {         System.out.println("---------------------BeanPostProcessor初始化前");     }     return arg0; }}

Spring Annotation Development-comprehensive analysis of the life cycle of common annotation usage methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.