Spring Bean设置初始化方法和销毁方法

一、准备工作

        1、导入spring-context依赖

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

二、通过指定Bean的初始化和销毁方法

        1、创建实体类Color

public class Color {

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

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

}

        2、通过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、通过BeansConfig注解配置

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、开始测试

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();
}

三、通过实现InitializingBean、DisposableBean

        1、创建实体类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、通过BeansConfig注解配置

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

@Configuration
public class BeansConfig {

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

}

        3、开始测试

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

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

四、通过注解@PostConstruct、@PreDestroy

        1、创建实体类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、通过BeansConfig注解配置

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

@Configuration
public class BeansConfig {

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

}

        3、开始测试

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

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

五、通过实现BeanPostProcessor

        1、创建实体类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、创建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、通过BeansConfig注解配置

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、开始测试

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

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

六、总结分析

        本章主要学习了,Spring Bean设置初始化方法和销毁方法。《参考资料》

        

猜你喜欢

转载自blog.csdn.net/weixin_40968009/article/details/129802203