Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

In Spring, the life cycle of a Bean goes through a long period of steps, which is a common question in interviews.

Let’s start with an overview of the Bean lifecycle:

  1. Creating a Bean instance
  2. Call the setter() method in the Bean to set the property value
  3. Check whether the Bean implements the Aware interface and, if so, invoke the corresponding interface method
  4. If the container has a BeanPostProcessor, call its postProcessAfterInitialization
  5. Check that the Bean implements an InitializingBean and, if so, call its afterPropertiesSet method
  6. Checks whether the init-method property of the Bean is specified and, if so, invokes its specified method
  7. If the container has a BeanPostProcessor, call its postProcessorAfterInitialization
  8. Check to see if the Bean has implemented DisposableBean, and if so, call its method
  9. Checks if the destroy-method attribute of the Bean is specified and, if so, calls its specified method

The life cycle of a Bean needs to go through the nine processes mentioned above, as shown in the figure:

Let’s test this with a specific program. First write a Bean:

public class User implements ApplicationContextAware.InitializingBean.DisposableBean {

    private String name;
    private Integer age;

    public User(a) {
        System.out.println("1-- "create User instance");
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("2-- "Set User name attribute");
    }

    public void setAge(Integer age) {
        this.age = age;
        System.out.println("2-- "Set User age attribute");
    }

    public void init(a) {
        System.out.println("6-- "Calls the method specified by the init-method attribute");
    }

    public void myDestroy(a) {
        System.out.println("9-- "calls the method specified by the destroy-method attribute.);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("3-- "Call methods corresponding to the Aware interface");
    }

    @Override
    public void afterPropertiesSet(a) throws Exception {
        System.out.println(Call the afterPropertiesSet method of the InitializingBean interface.);
    }

    @Override
    public void destroy(a) throws Exception {
        System.out.println(Call the Destroy method of the DisposableBean interface); }}Copy the code

First this Bean implements ApplicationContextAware, InitialzingBean, DisposableBean, and outputs the corresponding contents in each method, and then writes a BeanPostProcessor:

public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Seven," call MyBeanPostProcessor postProcessBeforeInitialization method");
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("4 -" call MyBeanPostProcessor postProcessAfterInitialization method");
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName); }}Copy the code

Finally, register them with the container:

@Configuration
public class MyBeanConfig {

    @Bean(initMethod = "init", destroyMethod = "myDestroy")
    public User user(a) {
        User user = new User();
        user.setName("zs");
        user.setAge(30);
        return user;
    }

    @Bean
    public BeanPostProcessor beanPostProcessor(a) {
        return newMyBeanPostProcessor(); }}Copy the code

Running results:

1-- "create User instance 2--" set User's name attribute 2-- "set User's age attribute 3--" call methods corresponding to the Aware interface 5-4 - "call MyBeanPostProcessor postProcessAfterInitialization methods. Call the afterPropertiesSet InitializingBean interface method 6 - "call the init method attribute specified method 7 -" call MyBeanPostProcessor postProcessBeforeInitialization method of 8 - "call DisposableBean interfaces destroy methods 9-- calls the method specified by the destroy-method attributeCopy the code