目录
1.加载过程
2.示例:
3.注意:
1.加载过程
①Bean 实例化
通过构造函数或工厂方法创建 Bean 的实例
②手动设置属性
通过set方法进行赋值
③属性注入
Spring 容器将配置的属性值注入到 Bean 中(通过 @Autowired
、@Value
、@NacosValue
等注解或 XML 配置)。
④初始化
先调用调用 @PostConstruct
方法,再调用InitializingBean
的 afterPropertiesSet
方法,再调用自定义的初始化方法(通过 initMethod
指定)
⑤bean加载完成可以使用
⑥容器关闭时进行销毁
先调用 @PreDestroy
方法,再调用 DisposableBean
的 destroy
方法。然后调用自定义的销毁方法(通过 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
③类的生命周期
- 加载(Loading)阶段:将类的字节码载入内存
- 链接(Linking)阶段
- 验证(Verification)确保 Class 文件的字节流符合 JVM 规范
- 准备(Preparation)为类的静态变量分配内存并设置初始值
- 解析(Resolution)将常量池中的符号引用替换为直接引用
- 初始化(Initialization)阶段
- 静态变量赋值
- 静态代码执行
- 使用(Using)阶段
- 类的实例化
- 属性注入
- 初始化方法执行
- 使用bean
- 卸载(Unloading)阶段