当前位置: 首页 > news >正文

Spring7指南(三)之Bean的生命周期

项目工程完整地址:https://github.com/YiyiCoding/Spring7Guides

Bean的单例说明

  • 多例Bean( @Scope(“prototype”) )每次都由IOC容器New一个新对象,此处之外跟容器没有太多的联系性

  • 单例才具有生命周期的概念

  • 单例有懒加载的概念:需要时才由IOC容器创建Bean

    只要在类上面或@Bean 上注解@Lazy即可

Bean的生命周期

  1. 实例化(Instantiation)→
  2. 属性填充(Populate)→
  3. 前置初始化(BeanPostProcessor.postProcessBeforeInitialization)→
  4. 初始化(Initialization)→
  5. 后置初始化(BeanPostProcessor.postProcessAfterInitialization)→
  6. 就绪(Ready):【在容器中可用】 →
  7. 销毁前处理(DestructionAwareBeanPostProcessor)→
  8. 销毁(Destruction)

【今天Spring7包含的知识点】:

  • @Lazy

  • Bean的生命周期

  • 构造函数注入

    publicUserService2(OrderServiceorderService){this.orderService=orderService;}
  • 需适配 Jakarta EE 9+

    implementation("jakarta.annotation:jakarta.annotation-api:3.0.0")
  • @PostConstruct

  • @PreDestroy

  • InitializingBean

  • DisposableBean

代码开始

  • 新增依赖,Spring7 要求 Jakarta EE 9+
implementation("jakarta.annotation:jakarta.annotation-api:3.0.0")
  • UserService2,采用构造函数注入
@ServicepublicclassUserService2{privatefinalOrderServiceorderService;publicUserService2(OrderServiceorderService){this.orderService=orderService;}publicvoidtest(){System.out.println(orderService.demo());}}

Bean的生命周期代码LifecycleBean

packagecom.yiyi.coding.spring.ioc.bean;importjakarta.annotation.PostConstruct;importjakarta.annotation.PreDestroy;importorg.springframework.beans.factory.DisposableBean;importorg.springframework.beans.factory.InitializingBean;importorg.springframework.stereotype.Component;@ComponentpublicclassLifecycleBeanimplementsInitializingBean,DisposableBean{// 步骤1:实例化(无代码,容器自动执行)publicLifecycleBean(){System.out.println("【1. 实例化】创建Bean实例");}// 步骤2:属性填充(模拟注入,实际可通过配置注入)privateStringname;publicvoidsetName(Stringname){this.name=name;System.out.println("【2. 属性填充】设置属性name="+name);}// 步骤3:BeanPostProcessor前置处理(需自定义实现)// 步骤4:初始化@PostConstructpublicvoidpostConstruct(){System.out.println("【4.1 初始化】@PostConstruct");}@OverridepublicvoidafterPropertiesSet()throwsException{System.out.println("【4.2 初始化】InitializingBean#afterPropertiesSet");}publicvoidinitMethod(){System.out.println("【4.3 初始化】自定义init-method");}// 步骤5:BeanPostProcessor后置处理(需自定义实现)// 步骤6:就绪(可用)publicvoiddoBusiness(){System.out.println("【6. 就绪】Bean执行业务逻辑");}// 步骤7:销毁前处理(需自定义DestructionAwareBeanPostProcessor)// 步骤8:销毁@PreDestroypublicvoidpreDestroy(){System.out.println("【8.1 销毁】@PreDestroy");}@Overridepublicvoiddestroy(){System.out.println("【8.2 销毁】DisposableBean#destroy");}publicvoiddestroyMethod(){System.out.println("【8.3 销毁】自定义destroy-method");}publicStringgetName(){returnname;}}

运行main函数的测试情况:LifecycleBean 中的某些方法未完全执行,有些需进行配置,如initMethod()等,本次略过。

packagecom.yiyi.coding.spring.ioc;// 测试类importcom.yiyi.coding.spring.ioc.bean.LifecycleBean;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;importorg.springframework.context.annotation.ComponentScan;@ComponentScan("com.yiyi.coding.spring.ioc")publicclassLifecycleTest{staticvoidmain(){// 启动容器AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext(LifecycleTest.class);LifecycleBeanbean=context.getBean(LifecycleBean.class);bean.setName("test");// 手动触发属性填充(模拟容器注入)bean.doBusiness();// 关闭容器(触发销毁)context.close();}}

以下生命周期的钩子已经足够使用。

【1. 实例化】创建Bean实例 【4.1 初始化】@PostConstruct 【4.2 初始化】InitializingBean#afterPropertiesSet 【2. 属性填充】设置属性name=test 【6. 就绪】Bean执行业务逻辑 【8.1 销毁】@PreDestroy 【8.2 销毁】DisposableBean#destroys
http://www.cnnetsun.cn/news/10892.html

相关文章:

  • HBase vs. 传统数据库:大数据时代的存储革命
  • Flutter 从原理到实战:深入理解跨平台框架核心与高效开发实践
  • Wan2.2-T2V-A14B实现物理级动态模拟的三大核心技术
  • 再谈ST表
  • 2026年机器人感知与智能控制国际学术会议(RPIC 2026)
  • Wan2.2-T2V-A14B生成视频可用于YouTube盈利吗?合规性解读
  • 【Docker Scout AI漏洞扫描揭秘】:如何利用人工智能精准发现容器安全盲点
  • Spring Kafka 动态消费实现案例
  • Wan2.2-T2V-A14B模型推理性能调优实战技巧分享
  • GraniStudio零代码平台调试算子方式有多少种?分别都是如何调试?
  • 小米14C刷国际版步骤
  • 智谱开源天团登陆 AtomGit,4 大模型覆盖多模态全场景!
  • 开源视频生成技术再突破:Wan2.1-FLF2V-14B模型实现720P高清流畅过渡
  • 教学辅助微信小程序设计毕业设计(源码+lw+部署文档+讲解等)
  • 【AUTOSAR AP Core】AUTOSAR AP核心:Executor角色揭秘
  • Chrony时间同步服务:从底层原理到技术演进的全景解析
  • 线性回归与KNN算法的核心原理及实践应用
  • Windows右键菜单革命:从混乱到高效的终极解决方案
  • 入门友好的低代码平台推荐,其中一款完全免费又能私有化部署
  • 基于VUE的小剧场票务系统[VUE]-计算机毕业设计源码+LW文档
  • AI不再“失忆“!揭秘让大模型记住一切的神奇技术,代码详解+实战教程,小白也能变大神!
  • Wan2.2-T2V-A14B模型API接口设计与调用示例详解
  • 如何快速实现Unity游戏翻译:XUnity.AutoTranslator终极指南
  • 阿里Qwen3双模型震撼开源:嵌入式与重排序技术革新RAG应用生态
  • HNU分布式数据库华为云数据库TaurusDB实践
  • 阿里Qwen3-Next模型震撼登场:800亿参数“轻装上阵“,香港企业AI应用成本大降90%
  • 备考华为HCIE的秘诀!轻松拿下顶级认证
  • 协同过滤扶贫助农系统系统
  • 现代 AI 代理设计:17 种架构的系统化实战合集
  • B站视频下载利器DownKyi:专业用户的终极操作指南