The life cycle of Spring annotation-driven development

review

Component registration for Spring annotation-driven development

Introduction

Bean life cycle: bean creation-initialization-destruction

The Spring container manages the cycle for us, but we can also customize the cycle, and the beans in the container call our custom methods when they reach the corresponding stage.

3 initialization methods and 1 interception method before and after initialization

1. Specify custom initialization methods and custom destruction methods through @Bean

① Create a Car class

public class Car {

    public Car() {
        System.out.println("Car对象被创建...");
    }

    /**
     * 自定义一个初始化对象方法
     */
    public void init(){
        System.out.println("Car对象被初始化...");
    }

    /**
     * 自定义一个销毁对象方法
     */
    public void destroy(){
        System.out.println("Car对象被销毁...");
    }
}

② Create a configuration class MyConfigOfLifeCycle

@Configuration
public class MyConfigOfLifeCycle {

    @Bean(initMethod = "init", destroyMethod = "destroy")//指定初始化方法和销毁方法
    public Car car(){
        return new Car();
    }
}

③ Test

@Test
public void test01(){
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfigOfLifeCycle.class);
    System.out.println("IOC容器被创建...");  //此时会创建并初始化所有的单例对象

    applicationContext.close();
}

④ The results are as follows

ps: If it is multi-instance, the object will be created and initialized when called, and the container no longer manages these multi-instance objects, so the object cannot be destroyed when the container is closed

⑤ Modified to multiple cases

@Configuration
public class MyConfigOfLifeCycle {

    @Scope("prototype")
    @Bean(initMethod = "init", destroyMethod = "destroy")//指定初始化方法和销毁方法
    public Car car(){
        return new Car();
    }
}

⑥ Retest 

@Test
public void test01(){
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfigOfLifeCycle.class);
    System.out.println("IOC容器被创建...");  //此时会创建并初始化所有的单例对象

    applicationContext.getBean("car");
    applicationContext.close();
}

⑦ The results are as follows

 

2. Realize the initialization logic by letting the bean implement the interface InitializingBean , and similarly, realize the destruction logic by letting the bean implement the interface DisposableBean

① Create a Cat class and use component annotations

@Component
public class Cat implements InitializingBean, DisposableBean {

    public Cat() {
        System.out.println("Cat对象的无参构造器被调用...");
    }

    /**
     * 销毁方法, 在容器关闭时被调用
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        System.out.println("Cat对象被销毁...");
    }

    /**
     * 初始化方法, 在对象创建并设置完所有属性后被调用
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Cat对象被初始化...");
    }
}

② Use component scan annotations to inject components into the container

@ComponentScan("com.spring.annotation.bean")
@Configuration
public class MyConfigOfLifeCycle {...}

③ Test, the results are as follows

 

3. Use the annotation @PostConstruct and the annotation @PreDestroy defined by the JSR250 specification

@PostConstruct

Call the method under the annotation after the bean is created and the attribute assignment is completed

@PreDestroy

Notify the method under the annotation before the bean is destroyed

Create a Dog object and mark the corresponding annotation

@Component
public class Dog {

    public Dog() {
        System.out.println("Dog对象的无参构造方法被调用...");
    }

    @PostConstruct
    public void init(){
        System.out.println("Dog对象被初始化...");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("Dog对象被销毁...");
    }
}

The test results are as follows

4. Bean post processor, BeanPostProcessor interface, used for interception operations before and after initialization

Create an implementation class MyBeanPostProcessor that implements the BeanPostProcessor interface

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    /**
     * @param bean  刚创建还未初始化的实例
     * @param beanName  bean的名称
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName+"初始化之前==>"+bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName+"初始化之后==>"+bean);
        return bean;
    }
}

The test results are as follows

The Spring annotation-driven life cycle part is finished, and the automatic assembly part will be written next.

Guess you like

Origin blog.csdn.net/ip_JL/article/details/85450316