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

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

    @Autowired
    private MyBeans myBeans;

    @PostConstruct
    public void postConstruct() {
        System.out.println("postConstruct1");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet1");
    }

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

    @Override
    public void destroy() throws Exception {
        System.out.println("afterPropertiesSet对应的销毁1");
    }

    @PreDestroy
    public 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");
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println("postConstruct2");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet2");
    }

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

    @Override
    public void destroy() throws Exception {
        System.out.println("afterPropertiesSet对应的销毁2");
    }

    @PreDestroy
    public 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)阶段

相关文章:

  • WPF从初学者到专家:实战项目经验分享与总结
  • Gymnasium Taxi‐v3 环境 与 Q-learning 算法 —— 强化学习入门 I
  • 2min搞定~Mac Pro 编译安装 Nginx 1.8.1
  • IP地址到地理地址查询的实现逻辑
  • 【Go】字符串相关知识点
  • ranger集成starrock报错
  • CTFshow 【WEB入门】信息搜集 【VIP限免】 web1-web17
  • 【第四节】windows sdk编程:windows 中的窗口
  • 隐藏的git文件夹
  • 【解决报错】:detected dubious ownership in repository at ‘D:/idea_code/xxx‘问题
  • 每日一题——两两交换链表中的节点
  • koltin在android 开发
  • 微电网管理 实现分布式能源的智能调度和管理
  • 【Java】并发编程实战:单例模式 + 阻塞队列的终极实现指南
  • 系统化战略管理国产软件的开创者与引领者
  • 【结构光相机的精度极限】
  • 【网络通信安全】子专栏链接及简介
  • 哪家LIMS厂商交付好?
  • Python 程序配置文件管理库介绍
  • 基于 GEE 利用 Sentinel-2 数据反演叶绿素与冠层水分含量
  • 纽约市长称墨西哥海军帆船撞桥事故已致2人死亡
  • 《掩耳盗邻》:富人劫富,是犯罪,也是赎罪?
  • 世界高血压日|专家:高血压患者控制血压同时应注重心率管理
  • 王东杰评《国家与学术》︱不“国”不“故”的“国学”
  • 上海市重大工程一季度开局良好,多项生态类项目按计划实施
  • 获派驻6年后,中国驻厄瓜多尔大使陈国友即将离任