Spring Bean setting initialization method and destruction method

1. Preparation

        1. Import spring-context dependencies

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.22.RELEASE</version>
</dependency>

2. By specifying the initialization and destruction methods of the Bean

        1. Create an entity class Color

public class Color {

    public void init(){
        System.out.println("Color init");
    }

    public void destroy(){
        System.out.println("Color destroy");
    }

}

        2. Configure through beans.xml

<?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="color" class="Color" init-method="init" destroy-method="destroy" />

</beans>

        3. Configure through BeansConfig annotation

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeansConfig {

    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Color color(){
        return new Color();
    }

}

        4. Start the test

public static void main(String[] args) {
    testXml();
    testAnnotation();
}

public static void testXml(){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    context.close();
}

public static void testAnnotation(){
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
    context.close();
}

3. By implementing InitializingBean and DisposableBean

        1. Create an entity class Color

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Color implements InitializingBean , DisposableBean {
    
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Color init");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("Color destroy");
    }

}

        2. Configure through BeansConfig annotation

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeansConfig {

    @Bean
    public Color color(){
        return new Color();
    }

}

        3. Start the test

public static void main(String[] args) {
    testAnnotation();
}

public static void testAnnotation(){
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
    context.close();
}

Fourth, through the annotation @PostConstruct, @PreDestroy

        1. Create an entity class Color

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class Color {

    @PostConstruct
    public void init(){
        System.out.println("Color init");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("Color destroy");
    }

}

        2. Configure through BeansConfig annotation

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeansConfig {

    @Bean
    public Color color(){
        return new Color();
    }

}

        3. Start the test

public static void main(String[] args) {
    testAnnotation();
}

public static void testAnnotation(){
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
    context.close();
}

Five, by implementing BeanPostProcessor

        1. Create an entity class Color

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class Color {

    @PostConstruct
    public void init(){
        System.out.println("Color init");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("Color destroy");
    }

}

        2. Create MyBeanPostProcessor

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        // bean初始化之前调用,initMehtod之前执行
        System.out.println("postProcessBeforeInitialization : " + beanName + " :" + bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // bean初始化之后调用,initMehtod之后执行
        System.out.println("postProcessAfterInitialization : " + beanName + " :" + bean);
        return bean;
    }

}

        3. Configure through BeansConfig annotation

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeansConfig {

    @Bean
    public Color color(){
        return new Color();
    }

    @Bean
    public MyBeanPostProcessor myBeanPostProcessor(){
        return new MyBeanPostProcessor();
    }

}

        4. Start the test

public static void main(String[] args) {
    testAnnotation();
}

public static void testAnnotation(){
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfig.class);
    context.close();
}

6. Summary and analysis

        This chapter mainly learns, Spring Bean sets the initialization method and destruction method. "Reference Materials"

        

Guess you like

Origin blog.csdn.net/weixin_40968009/article/details/129802203