Java Spring Bean的生命周期管理详解

 更新时间:2021年12月23日 11:22:50   作者:大树下躲雨  
这篇文章主要为大家介绍了Java Spring Bean的生命周期管理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助

Spring Bean的生命周期管理

一、Spring Bean的生命周期

通过以下方式来指定Bean的初始化和销毁方法,
当Bean为单例时,Bean归Spring容器管理,Spring容器关闭,就会调用Bean的销毁方法
当Bean为多例时,Bean不归Spring容器管理,Spring容器关闭,不会调用Bean的销毁方法

二、通过@Bean的参数(initMethod ,destroyMethod)指定Bean的初始化和销毁方法

1、项目结构

2、Person

public class Person {
    public Person(){
        System.out.println("Person 创建了...");
    }
    public void init(){
        System.out.println("Person 初始化了...");
    }
    public void destroy(){
        System.out.println("Person 被销毁了...");
    }
}

3、Bean注册配置类(单实例)

import com.dashu.bean.Person;
import org.springframework.context.annotation.*;
@Configuration
public class BeanConfig {
    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Person person(){
        return new Person();
    }
}

4、测试类

import com.dashu.bean.Person;
import com.dashu.config.BeanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
    public static void main(String[] args) {
        //加载配置类获取容器
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
        //获取Bean
        Person bean = annotationConfigApplicationContext.getBean(Person.class);
        //关闭容器
        annotationConfigApplicationContext.close();
    }
}

5、测试结果

6、Bean注册配置类(多实例)

import com.dashu.bean.Person;
import org.springframework.context.annotation.*;
@Configuration
public class BeanConfig {
    @Scope("prototype")
    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Person person(){
        return new Person();
    }
}

7、测试结果

三、Bean实现接口InitializingBean, DisposableBean

1、Person

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Person implements InitializingBean, DisposableBean {
    public Person(){
        System.out.println("Person 创建了...");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Person 初始化了...");
    }
    @Override
    public void destroy() throws Exception {
        System.out.println("Person 被销毁了...");
    }
}

2、Bean注册配置类

import com.dashu.bean.Person;
import org.springframework.context.annotation.*;
@Configuration
public class BeanConfig {
    @Bean
    public Person person(){
        return new Person();
    }
}

3、测试结果

四、通过注解@PostConstruct和@PreDestroy

@PostConstruct:标注在Bean的初始化方法上
@PreDestroy:标注在Bean的销毁方法上

1、Person

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Person {
    public Person(){
        System.out.println("Person 创建了...");
    }
    @PostConstruct
    public void init(){
        System.out.println("Person 初始化了...");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("Person 被销毁了...");
    }
}

2、测试结果

五、使用接口BeanPostProcessor实现类(后置处理器)

1、项目结构

2、Person

import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component
public class Person {
    public Person(){
        System.out.println("Person 创建了...");
    }
    @PostConstruct
    public void init(){
        System.out.println("Person 初始化了...");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("Person 被销毁了...");
    }
}

3、Bean注册配置类

import org.springframework.context.annotation.*;
@Configuration
@ComponentScan({"com.dashu"})
public class BeanConfig {
}

4、BeanPostProcessor实现类(后置处理器)

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
/**
 * 后置处理器:初始化前后进行处理工作
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    /**
     * 初始化之前工作
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("初始化之前..."+beanName+"=["+bean+"]");
        return bean;
    }
    /**
     * 初始化之后工作
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public  Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("初始化之后..."+beanName+"=["+bean+"]");
        return bean;
    }
}

5、测试结果

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

相关文章

  • java多线程Future和Callable类示例分享

    java多线程Future和Callable类示例分享

    JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的。今天我们就来研究下Future和Callable的实现方法
    2016-01-01
  • Java之Runnable启动线程的使用方式

    Java之Runnable启动线程的使用方式

    这篇文章主要介绍了Java之Runnable启动线程的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • IDEA 创建多级文件夹的操作

    IDEA 创建多级文件夹的操作

    这篇文章主要介绍了IDEA 创建多级文件夹的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • java利用SMB读取远程文件的方法

    java利用SMB读取远程文件的方法

    这篇文章主要为大家详细介绍了java利用SMB读取远程文件的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • 如何解决freemarker静态化生成html页面乱码的问题

    如何解决freemarker静态化生成html页面乱码的问题

    这篇文章主要介绍了如何解决freemarker静态化生成html页面乱码的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • Java中数组的定义与使用详解

    Java中数组的定义与使用详解

    这篇文章主要给大家介绍了关于Java中数组的定义与使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-11-11
  • Spring事务Transaction配置的五种注入方式详解

    Spring事务Transaction配置的五种注入方式详解

    这篇文章主要介绍了Spring事务Transaction配置的五种注入方式详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-04-04
  • 使用IDEA对SpringBoot应用进行远程调试方式

    使用IDEA对SpringBoot应用进行远程调试方式

    文章介绍了如何在IDEA中对部署在服务器上的SpringBoot应用进行远程调试,通过配置远程调试端口和启动参数,本地IDEA可以设置断点并进行调试
    2025-02-02
  • java分布式流处理组件Producer入门详解

    java分布式流处理组件Producer入门详解

    这篇文章主要为大家介绍了java分布式流处理组件Producer入门详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • springboot2中session超时,退到登录页面方式

    springboot2中session超时,退到登录页面方式

    这篇文章主要介绍了springboot2中session超时,退到登录页面方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01

最新评论