当前位置: 首页> 文旅> 酒店 > 前端页面设计_我要免费发布信息_东莞有限公司seo_外链推广软件

前端页面设计_我要免费发布信息_东莞有限公司seo_外链推广软件

时间:2025/7/10 7:39:38来源:https://blog.csdn.net/m0_37145844/article/details/145699844 浏览次数:0次
前端页面设计_我要免费发布信息_东莞有限公司seo_外链推广软件

一、Bean 生命周期

  1. Bean的定义
  2. Bean的实例化
  3. 属性注入
  4. Bean的初始化
  5. Bean的使用
  6. Bean的销毁

可以增强的位置:

  1. @PostConstruct:属性注入后,afterPropertiesSet方法 (前提实现:InitializingBean接口)前增强。

  2. @PreDestroy:销毁前,destroy方法(前提实现:DisposableBean接口)前增强。

二、作用域

  1. singleton 单例

  2. prototype 原型

  3. request(请求作用域,仅适用于 Web 应用)

  4. 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*/

关键字:前端页面设计_我要免费发布信息_东莞有限公司seo_外链推广软件

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: