一、Bean 生命周期
- Bean的定义
- Bean的实例化
- 属性注入
- Bean的初始化
- Bean的使用
- Bean的销毁
可以增强的位置:
-
@PostConstruct:属性注入后,afterPropertiesSet方法 (前提实现:InitializingBean接口)前增强。
-
@PreDestroy:销毁前,destroy方法(前提实现:DisposableBean接口)前增强。
二、作用域
-
singleton 单例
-
prototype 原型
-
request
(请求作用域,仅适用于 Web 应用) -
session
(会话作用域,仅适用于 Web 应用)
package com.example.demo.beanLifecycleTest;import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;class AnotherBean {public AnotherBean() {System.out.println("AnotherBean 实例化");}
}class MyLifecycleBean implements InitializingBean, DisposableBean {private AnotherBean anotherBean;public void setAnotherBean(AnotherBean anotherBean) {this.anotherBean = anotherBean;System.out.println("MyLifecycleBean 属性注入");}public MyLifecycleBean() {System.out.println("MyLifecycleBean 构造函数初始化");}@PostConstructpublic void beforInit() {System.out.println("MyLifecycleBean 使用 PostConstruct 初始化前增强");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("MyLifecycleBean 属性注入后增强");}@PreDestroypublic void beforDestroy() {System.out.println("MyLifecycleBean 销毁前增强");}@Overridepublic void destroy() throws Exception {System.out.println("MyLifecycleBean 销毁");}}@Configuration
class AppConfig {// 定义 AnotherBean,默认单例作用域@Beanpublic AnotherBean anotherBean() {return new AnotherBean();}// 定义单例作用域的 MyLifecycleBean@Beanpublic MyLifecycleBean singletonMyLifecycleBean() {MyLifecycleBean myLifecycleBean = new MyLifecycleBean();myLifecycleBean.setAnotherBean(anotherBean());return myLifecycleBean;}// 定义原型作用域的 MyLifecycleBean@Scope("prototype")@Beanpublic MyLifecycleBean protoTypeMyLifecycleBean() {MyLifecycleBean myLifecycleBean = new MyLifecycleBean();myLifecycleBean.setAnotherBean(anotherBean());return myLifecycleBean;}
}public class MainApp {public static void main(String[] args) {// 创建 Spring 容器AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);// 获取单例作用域的 Bean// 获取单例作用域的 BeanMyLifecycleBean singletonBean1 = context.getBean("singletonMyLifecycleBean", MyLifecycleBean.class);System.out.println("-------------------------------------------------------------------------------------");MyLifecycleBean singletonBean2 = context.getBean("singletonMyLifecycleBean", MyLifecycleBean.class);System.out.println("单例 Bean 是否为同一个实例: " + (singletonBean1 == singletonBean2));System.out.println("-------------------------------------------------------------------------------------");System.out.println("-------------------------------------------------------------------------------------");System.out.println("-------------------------------------------------------------------------------------");System.out.println("-------------------------------------------------------------------------------------");// 获取原型作用域的 BeanMyLifecycleBean prototypeBean1 = context.getBean("protoTypeMyLifecycleBean", MyLifecycleBean.class);System.out.println("-------------------------------------------------------------------------------------");MyLifecycleBean prototypeBean2 = context.getBean("protoTypeMyLifecycleBean", MyLifecycleBean.class);System.out.println("原型 Bean 是否为同一个实例: " + (prototypeBean1 == prototypeBean2));context.close();}
}/*** /Users/weiwei/Library/Java/JavaVirtualMachines/corretto-21.0.5/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=62666:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath /Users/weiwei/Documents/demo/target/classes:/Users/weiwei/.m2/repository/org/springframework/boot/spring-boot-starter/3.4.2/spring-boot-starter-3.4.2.jar:/Users/weiwei/.m2/repository/org/springframework/boot/spring-boot/3.4.2/spring-boot-3.4.2.jar:/Users/weiwei/.m2/repository/org/springframework/spring-context/6.2.2/spring-context-6.2.2.jar:/Users/weiwei/.m2/repository/org/springframework/spring-aop/6.2.2/spring-aop-6.2.2.jar:/Users/weiwei/.m2/repository/org/springframework/spring-beans/6.2.2/spring-beans-6.2.2.jar:/Users/weiwei/.m2/repository/org/springframework/spring-expression/6.2.2/spring-expression-6.2.2.jar:/Users/weiwei/.m2/repository/io/micrometer/micrometer-observation/1.14.3/micrometer-observation-1.14.3.jar:/Users/weiwei/.m2/repository/io/micrometer/micrometer-commons/1.14.3/micrometer-commons-1.14.3.jar:/Users/weiwei/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/3.4.2/spring-boot-autoconfigure-3.4.2.jar:/Users/weiwei/.m2/repository/org/springframework/boot/spring-boot-starter-logging/3.4.2/spring-boot-starter-logging-3.4.2.jar:/Users/weiwei/.m2/repository/ch/qos/logback/logback-classic/1.5.16/logback-classic-1.5.16.jar:/Users/weiwei/.m2/repository/ch/qos/logback/logback-core/1.5.16/logback-core-1.5.16.jar:/Users/weiwei/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.24.3/log4j-to-slf4j-2.24.3.jar:/Users/weiwei/.m2/repository/org/apache/logging/log4j/log4j-api/2.24.3/log4j-api-2.24.3.jar:/Users/weiwei/.m2/repository/org/slf4j/jul-to-slf4j/2.0.16/jul-to-slf4j-2.0.16.jar:/Users/weiwei/.m2/repository/jakarta/annotation/jakarta.annotation-api/2.1.1/jakarta.annotation-api-2.1.1.jar:/Users/weiwei/.m2/repository/org/springframework/spring-core/6.2.2/spring-core-6.2.2.jar:/Users/weiwei/.m2/repository/org/springframework/spring-jcl/6.2.2/spring-jcl-6.2.2.jar:/Users/weiwei/.m2/repository/org/yaml/snakeyaml/2.3/snakeyaml-2.3.jar:/Users/weiwei/.m2/repository/org/slf4j/slf4j-api/2.0.16/slf4j-api-2.0.16.jar com.example.demo.beanLifecycleTest.MainApp* AnotherBean 实例化* MyLifecycleBean 构造函数初始化* MyLifecycleBean 属性注入* MyLifecycleBean 使用 PostConstruct 初始化前增强* MyLifecycleBean 属性注入后增强* -------------------------------------------------------------------------------------* 单例 Bean 是否为同一个实例: true* -------------------------------------------------------------------------------------* -------------------------------------------------------------------------------------* -------------------------------------------------------------------------------------* -------------------------------------------------------------------------------------* MyLifecycleBean 构造函数初始化* MyLifecycleBean 属性注入* MyLifecycleBean 使用 PostConstruct 初始化前增强* MyLifecycleBean 属性注入后增强* -------------------------------------------------------------------------------------* MyLifecycleBean 构造函数初始化* MyLifecycleBean 属性注入* MyLifecycleBean 使用 PostConstruct 初始化前增强* MyLifecycleBean 属性注入后增强* 原型 Bean 是否为同一个实例: false* MyLifecycleBean 销毁前增强* MyLifecycleBean 销毁** Process finished with exit code 0*/