bean的加载过程

目录

1.加载过程

2.示例:

3.注意:


1.加载过程

①Bean 实例化

通过构造函数或工厂方法创建 Bean 的实例

②手动设置属性

通过set方法进行赋值

③属性注入

Spring 容器将配置的属性值注入到 Bean 中(通过 @Autowired@Value@NacosValue 等注解或 XML 配置)。

④初始化

先调用调用 @PostConstruct 方法,再调用InitializingBeanafterPropertiesSet 方法,再调用自定义的初始化方法(通过 initMethod 指定)

⑤bean加载完成可以使用

⑥容器关闭时进行销毁

先调用 @PreDestroy 方法再调用 DisposableBeandestroy 方法。然后调用自定义的销毁方法(通过 destroyMethod 指定)

2.示例:

配置bean

package com.example.demo6.service;import lombok.Data;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;@Data
public class MyBean implements InitializingBean, DisposableBean {private Integer id;public MyBean() {System.out.println("执行无参构造1");}static {System.out.println("static1");}@Autowiredprivate MyBeans myBeans;@PostConstructpublic void postConstruct() {System.out.println("postConstruct1");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("afterPropertiesSet1");}public void init() {System.out.println("init1");}@Overridepublic void destroy() throws Exception {System.out.println("afterPropertiesSet对应的销毁1");}@PreDestroypublic void preDestroy() {System.out.println("postConstruct对应的销毁1");}public void destroyMethod() {System.out.println("init对应的销毁1");}
}
package com.example.demo6.service;import lombok.Data;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.Serializable;@Data
public class MyBeans implements InitializingBean, DisposableBean {private Integer id;public MyBeans() {System.out.println("执行无参构造2");}static {System.out.println("static2");}@PostConstructpublic void postConstruct() {System.out.println("postConstruct2");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("afterPropertiesSet2");}public void init() {System.out.println("init2");}@Overridepublic void destroy() throws Exception {System.out.println("afterPropertiesSet对应的销毁2");}@PreDestroypublic void preDestroy() {System.out.println("postConstruct对应的销毁2");}public void destroyMethod() {System.out.println("init对应的销毁2");}
}
package com.example.demo6.config;import com.example.demo6.service.MyBean;
import com.example.demo6.service.MyBeans;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class TestConfig {@Bean(initMethod="init",destroyMethod="destroyMethod")public MyBean myBean(){MyBean myBean = new MyBean();myBean.setId(1);System.out.println("属性赋值1");return myBean;}@Bean(initMethod="init",destroyMethod="destroyMethod")public MyBeans myBeans() {System.out.println("属性赋值2");return new MyBeans();}}

启动容器 

import com.example.demo6.config.TestConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Test {public static void main(String[] args) {System.out.println("容器启动");AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);System.out.println("加载完成");System.out.println("bean使用");context.close();}
}

 执行结果:


容器启动
22:21:50.639 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5474c6c
22:21:50.649 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
22:21:50.789 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
22:21:50.790 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
22:21:50.790 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
22:21:50.794 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
22:21:50.810 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'testConfig'
22:21:50.816 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myBean'
static1
执行无参构造1
属性赋值1
22:21:50.839 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myBeans'
static2
执行无参构造2
属性赋值2
postConstruct2
afterPropertiesSet2
init2
postConstruct1
afterPropertiesSet1
init1
加载完成
bean使用
22:21:50.859 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5474c6c, started on Wed Mar 12 22:21:50 CST 2025
postConstruct对应的销毁1
afterPropertiesSet对应的销毁1
init对应的销毁1
postConstruct对应的销毁2
afterPropertiesSet对应的销毁2
init对应的销毁2

3.注意:

①静态代码块最先执行

②如果父类实现了InitializingBean接口,子类会执行父类的afterPropertiesSet方法,场景的动态数据源的实现类AbstractRoutingDataSource

③类的生命周期

  1. 加载(Loading)阶段:将类的字节码载入内存
  2. 链接(Linking)阶段
    1. 验证(Verification)确保 Class 文件的字节流符合 JVM 规范
    2. 准备(Preparation)为类的静态变量分配内存并设置初始值
    3. 解析(Resolution)将常量池中的符号引用替换为直接引用
  3. 初始化(Initialization)阶段
    1. 静态变量赋值
    2. 静态代码执行
  4. 使用(Using)阶段
    1. 类的实例化
    2. 属性注入
    3. 初始化方法执行
    4. 使用bean
  5. 卸载(Unloading)阶段

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/34120.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

怎么有效降低知网AIGC率

在学术创作日益规范且数字化检测技术不断发展的当下,知网 AIGC 检测成为了众多创作者关注的焦点。许多人苦恼于如何有效降低知网 AIGC 率,让自己的作品在通过检测的同时,彰显出真实的创作水平与独特性。接下来,我们就深入探讨降低…

代码随想录day17 二叉树part05

654.最大二叉树 给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建: 创建一个根节点,其值为 nums 中的最大值。 递归地在最大值 左边 的 子数组前缀上 构建左子树。 递归地在最大值 右边 的 子数组后缀上 构建右子树。 返回 nums …

【Python入门】一篇掌握Python中的字典(创建、访问、修改、字典方法)【详细版】

🌈 个人主页:十二月的猫-CSDN博客 🔥 系列专栏: 🏀《Python/PyTorch极简课》_十二月的猫的博客-CSDN博客 💪🏻 十二月的寒冬阻挡不了春天的脚步,十二点的黑夜遮蔽不住黎明的曙光 目…

LeetCode 环形链表II:为什么双指针第二次会在环的入口相遇?

快慢指针 为什么相遇后让快指针回到起点,再让快指针和慢指针都一步一步地走,它们就会在环的入口相遇? 复杂度 时间复杂度: O(n) 空间复杂度: O(1) public ListNode detectCycle(ListNode head) {ListNode slow head, fast head;ListNode …

HarmonyOS第24天:鸿蒙应用安全秘籍:如何为用户数据筑牢防线?

开篇引入 在数字化时代,我们的生活越来越依赖各种应用程序。从社交娱乐到移动支付,从健康管理到工作学习,应用已经渗透到生活的方方面面。然而,随着应用使用的日益频繁,用户隐私数据泄露的风险也在不断增加。 前几年&…

P2730 魔板 (写了巨久..有一些数字,字符,字符串之间的转换规则)

ac代码&#xff1a; #include<iostream> #include<map> #include<queue> using namespace std; map<string,int>mp1,mp2; map<string,string>mp3; queue<string>q; string str,res"12345678"; void pri(string str){if(resstr)…

Centos7使用docker搭建redis集群

前置准备&#xff1a; Centos7安装docker就不多说了… 本次目的是搭建3主3从&#xff08;当然你也可以按需扩展&#xff09;准备三台服务器&#xff0c;假定IP分别为&#xff1a;192.168.75.128、192.168.75.129、192.168.75.130安装 redis&#xff1a; #拉取redis docker p…

Java 用While语句判断密码是否输入正确

package com.MyJava; import java.util.Scanner;public class While {public static void main(String[] args) {Scanner Myscan new Scanner(System.in); int i 0,n 3; //n为有效密码次数System.out.print("请输入密码&#xff1a;");String Password Myscan.ne…

Browser Copilot 开源浏览器扩展,使用现有或定制的 AI 助手来完成日常 Web 应用程序任务。

一、软件介绍 文末提供源码和开源扩展程序下载 Browser Copilot 是一个开源浏览器扩展&#xff0c;允许您使用现有或定制的 AI 助手来帮助您完成日常 Web 应用程序任务。 目标是提供多功能的 UI 和简单的框架&#xff0c;以实现和使用越来越多的 copilots&#xff08;AI 助手&…

探索Maas平台与阿里 QWQ 技术:AI调参的魔法世界

摘要&#xff1a;本文介绍了蓝耘 Maas 平台在人工智能领域的表现及其核心优势&#xff0c;包括强大的模型支持、高效的资源调度和友好的操作界面。文章还探讨了蓝耘 Maas 平台与阿里 QWQ 技术的融合亮点及应用拓展实例&#xff0c;并提供了调参实战指南&#xff0c;最后对蓝耘 …

3.2 组件Props的TS高级类型校验模式

文章目录 1. 组件Props校验的核心价值2. 基础类型校验回顾2.1 基本类型声明2.2 类型系统限制3. 高级类型校验模式3.1 类型模板字面量3.2 条件类型约束3.3 递归类型结构4. 泛型组件模式4.1 基础泛型定义4.2 泛型约束扩展5. 高级联合类型应用5.1 动态表单校验5.2 状态机驱动类型6…

Vim软件使用技巧

目录 Demo Vim怎么看一个文件的行号&#xff0c;不用打开文件的前提下&#xff1f;进入文件后怎么跳转到某一行? 不打开文件查看行号&#xff08;查看文件的方法&#xff09; 方法1、使用命令行工具统计行数 方法2、通过vim的 - 参数查看文件信息 进入文件后跳转到指定行…

C 语 言 --- 二 维 数 组 的 应 用

C 语 言 --- 二 维 数 组 的 应 用 第 一 题 - - - 冒 泡 排 序冒 泡 排 序冒 泡 排 序 的 原 理 第 二 题 - - - 回 型 矩 阵特 点 第 三 题 - - - 蛇 形 矩 阵总结 &#x1f4bb;作者简介&#xff1a;曾 与 你 一 样 迷 茫&#xff0c;现 以 经 验 助 你 入 门 C 语 言 &…

微信小程序实现根据不同的用户角色显示不同的tabbar并且可以完整的切换tabbar

直接上图上代码吧 // login/login.js const app getApp() Page({/*** 页面的初始数据*/data: {},/*** 生命周期函数--监听页面加载*/onLoad(options) {},/*** 生命周期函数--监听页面初次渲染完成*/onReady() {},/*** 生命周期函数--监听页面显示*/onShow() {},/*** 生命周期函…

CTA重建:脑血管重建,CT三维重建,三维建模 技术,实现

CTA&#xff08;CT血管造影&#xff09;是一种基于CT扫描的医学成像技术&#xff0c;主要用于血管系统的三维重建和可视化。脑血管重建是CTA的重要应用之一&#xff0c;能够帮助医生诊断脑血管疾病&#xff08;如动脉瘤、狭窄、畸形等&#xff09;。以下是实现CTA脑血管重建、C…

告别XML模板的繁琐!Word文档导出,easy!

word模板导出 最近项目中有个功能&#xff0c;导出月报&#xff0c;发现同事使用了docx格式模板,感觉比之前转成xml的简单多了&#xff0c;这边记录下使用方法。 xml方式导出word,模板太复杂了 资料 poi-tl 一个基于Apache POI的Word模板引擎&#xff0c;也是一个免费开源的Jav…

Vue 过滤器深度解析与应用实践

文章目录 1. 过滤器概述1.1 核心概念1.2 过滤器生命周期 2. 过滤器基础2.1 过滤器定义2.2 过滤器使用 3. 过滤器高级用法3.1 链式调用3.2 参数传递3.3 动态过滤器 4. 过滤器应用场景4.1 文本格式化4.2 数字处理4.3 数据过滤 5. 性能优化与调试5.1 性能优化策略5.2 调试技巧 6. …

ST电机库电流采样 三电阻单ADC

一、概述 下图是三电阻采样的电路结构 其中流过三相系统的电流I1、I2、I3遵循以下关系: 因此,为了重建流过普通三相负载的电流,在我们可以用以上公式计算的情况下,只需要对三相中的两相进行采样即可。 STM32的ADC可以很灵活的配置成同步采集两路ADC数据,…

【测试篇】打破测试认知壁垒,从基础概念起步

前言 &#x1f31f;&#x1f31f;本期讲解关于测试的基本概念相关知识介绍~~~ &#x1f308;感兴趣的小伙伴看一看小编主页&#xff1a;GGBondlctrl-CSDN博客 &#x1f525; 你的点赞就是小编不断更新的最大动力 &#x1f386;那么废话…

SpringBoot MCP 入门使用

随着AI的火爆&#xff0c;最近发现MCP在未来确实大有可为&#xff0c;作为一名javaer怎么可以落后在历史洪流呢&#xff0c;根据官网和cursor也从零开始体验一下自定义mcp server。以后可以根据自己业务场景做出各种适合自身业务的工具。 至于什么是MCP 可以到https://modelcon…