Java Spring Bean生命周期管理的示例分析
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,小编给大家分享一下Java Spring Bean生命周期管理的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!Spring Bean的生命周期管理一、Spring Bean的
千家信息网最后更新 2026年11月03日Java Spring Bean生命周期管理的示例分析
小编给大家分享一下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.*;@Configurationpublic 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.*;@Configurationpublic 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.*;@Configurationpublic 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;@Componentpublic 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;/** * 后置处理器:初始化前后进行处理工作 */@Componentpublic 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 Spring Bean生命周期管理的示例分析"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
容器
方法
测试
管理
结果
配置
周期
生命
处理
处理器
工作
示例
分析
实例
接口
篇文章
结构
项目
参数
完了
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技工作委员会副主任毛军
互联网的科技创新
平安科技 互联网
喔噻互联网科技
丰台科技园 互联网
沃里互联网科技
互联网高科技企业
互联网科技汛
山东吉时兔互联网科技有限公司
科技与互联网的利与弊
茂名市互联网科技产业园
受益于互联网科技的高速发展
互联网和黑科技的看法
崇明区互联网教育科技诚信经营
互联网金融科技名词解释
雨阳互联网卡科技有限公司
广西科技产业互联网是什么
安徽零酒互联网科技有限公司
现代科技的标志之一的互联网
恒生科技互联网指数十大成分股
工业互联网科技新基建
互联网 科技公司
深圳天众互联网科技公司
长亮科技互联网金融
互联网科技有限公司 翻译
青岛中康互联网医疗科技有限公司
中国互联网公司排名锤子科技
互联网的科技体现在哪里
恒生互联网科技趋势
前海云享互联网科技