Bean Life Cycle of Spring 5 Source Analysis

Posted by dmikester1 on Sat, 27 Jul 2019 16:48:18 +0200

life cycle

Create - > initialize - > Destroy

1. Instantiating objects

2. setter injection, which executes Bean's attribute dependency injection

3. BeanNameAware's setBeanName(), if implemented, executes its setBeanName method

4.BeanFactoryAware's setBeanFactory(), if implemented, executes its setBeanFactory method

5. BeanPostProcessor's processBeforeInitialization() method executes the instance's processBeforeInitialization() method before the Bean is initialized if there is an associated processor.

6. InitializingBean's afterPropertiesSet() method is executed if the interface is implemented. Define init-method in Bean definition file

7.BeanPostProcessors'processAfterInitialization(), and if there are associated processor s, the instance's processAfterInitialization() method is executed before the Bean is initialized.

8. destory() of DisposeablebBean. When the container closes, if the Bean implements this interface, its destory() method is executed.

9. destroy-method is defined in the Bean definition file. When the container is closed, the method defined by "destory-method" can be used in the Bean definition file.

 

Note:

1. The singleton is initialized by default when the container is loved

2. Multiple instances are initialized every time Bean objects are retrieved

 

 

 

bean initialization means that all set methods in the object have been created and executed. Specify Method Execution

@ bean (initMethod, destory) specifies initialization and destruction methods and creates both methods in beans

Is init executed before or after the construction method? No parametric structure! Objects are created before initialization! So execute the parametric constructor first!

 

The process added to the above:

Bean creation (execution constructor) > initialization (custom init method) > destruction

Call the close() method to destroy the singleton object

Note: IOC containers use Map to combine storage objects and clear() to clear objects

See the source code:

Enter View:

Continue clicking to see:

Look at the first one:

Continuous follow-up is the clear ing method for collections

 

We can initialize by implementing some classes!

 

How to develop and use:

Method 1: Specify init-method and destory-method by @Bean

Method 2: Initializing Bean (Defining Initializing Logic) and Disposable Bean (Defining Destruction Logic)

Method 3: Use JSR250(Java specification, not Spring): @PostConstructo: Create and assign beans to execute the initialization method. @ PreDestory: Notify us to clean up the containers before they destroy the beans

 

Method 2:

Bean:

@Component
public class LifeBean implements InitializingBean, DisposableBean {

    //Constructor
    public LifeBean() {
        System.out.println("LifeBean Constructor");
    }

    /** Method of Interfacing InitializingBean
     *  //Interpretation objects have the process of creating affirmations and assigning attributes! The method is executed only after the assignment of the object is completed, i.e. (afterPropertiesSet) Chinese: set method is executed when all the methods are finished.
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        //Equivalent to @Bean(init =" ")
        //Interpretation objects have the process of creating affirmations and assigning attributes! The method is not executed until the object assignment is complete
        System.out.println("LifeBean ********> [InitializingBean.afterPropertiesSet] ");
    }

    /**
     * Method of Interfacing Disposable Bean
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        System.out.println("LifeBean ********>[DisposableBean.destroy]");
    }
}

Configuration and Sweeping

@Configuration
@ComponentScan("com.toov5.config.beanTest.entity")
public class MyConfig {

}

Start the test:

public class test {
    public test(){

    }
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext1 = new AnnotationConfigApplicationContext("com.toov5.config");
        applicationContext1.close();
//        System.out.println(applicationContext1);
    }
}

 

Method 3

Annotations replace interfaces:

Bean:

@Component
public class LifeBean {

    //Constructor
    public LifeBean() {
        System.out.println("LifeBean Constructor");
    }
    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        //Equivalent to @Bean(init =" ")
        //Interpretation objects have the process of creating affirmations and assigning attributes! The method is not executed until the object assignment is complete
        System.out.println("LifeBean ********> [InitializingBean.afterPropertiesSet] ");
    }

   @PreDestroy
    public void destroy() throws Exception {
        System.out.println("LifeBean ********>[DisposableBean.destroy]");
    }
}

The effect is the same:

 

  

Post Processor in Spring Bean Life Cycle: BeanPostProcessor Interface

In filters, you cannot use annotations to get Bean objects. How to do this: Get the context ApplicationContext separately

Post Processor to Enhance Bean Initialization

@Component
public class MyApplicationContext implements ApplicationContextAware {
    //Often when developing, it is used as a whole.
    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        LifeBean lifeBean = applicationContext.getBean("lifeBean", LifeBean.class);
        System.out.println("result----------"+lifeBean.toString());
    }
}

After operation: The result is no problem.

Topics: MySQL Spring Attribute Java