Spring 之 Bean 的生命周期
Bean的生命周期主要包括:创建---->初始化---->销毁 这几个过程,Spring中有很多管理Bean的生命周期的方式,下面我们就列举几种常用的.
一、通过@Bean注解指定
1、自定义Bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class Person { private String name; private int age; public Person(String name, int age) { this .name = name; this .age = age; System.out.println( "创建Person对象...." ); } public void init() { System.out.println( "Person的初始化...." ); } public void destory() { System.out.println( "Person的销毁...." ); } } |
2、配置类
1 2 3 4 5 6 7 8 9 10 11 | // 标记这是一个Spring配置类 @Configuration public class SpringConfiguration { // 指定初始化方法和销毁方法,init和destory的是Bean里面的方法名 @Bean (initMethod = "init" ,destroyMethod = "destory" ) // 通过@Scope注解来指定创建的Bean是单例的还是多例的 // @Scope("prototype") public Person person(){ return new Person( "xiaomaomao" , 22 ); } } |
3、测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class SpringDemo { @Test public void springTest01() throws Exception { // 单例的情况下,容器启动完成就会调用构造方法创建Bean的实例并进行初始化工作 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration. class ); // 多例的情况下,容器启动完成时并不会创建Bean对象,只有在实际获取的时候才去调用构造方法创建Bean的实例,并初始化Bean Person person = context.getBean( "person" , Person. class ); System.out.println(person); // 如果是单例的情况,调用close()方法对Bean对象进行销毁; // 如果是多例的情况,调用close()方法不会对Bean对象进行销毁,Bean何时销毁由容器自己去决定. context.close(); } } |
4、测试结果
1 2 3 4 5 6 | // 容器启动完成时就调用构造方法创建对象,并进行Bean实例的初始化工作 创建Person对象.... Person的初始化.... com.spring01.bean.Person @7ea37dbf // 调用close()方法进行Bean实例的销毁 Person的销毁.... |
二、自定义类实现InitializingBean和Disposable接口
1、自定义类实现InitializingBean接口,并重写afterPropertiesSet()方法,定义初始化逻辑;实现Disposable接口,并重写destory()方法,定义销毁逻辑.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Tiger implements InitializingBean,DisposableBean { // 单实例情况下,此方法在容器启动完成之后调用 // 多实例情况下,在获取Bean实例的时候调用 @Override public void afterPropertiesSet() throws Exception { System.out.println( "Tiger initial...." ); } // 单实例情况下,此方法在容器调用close()执行销毁方法, // 多实例情况下,不能自行销毁,由Spring管理销毁的时机 @Override public void destroy() throws Exception { System.out.println( "Tiger destory...." ); } } |
2、配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // 标记这是一个Spring配置类 @Configuration public class SpringConfiguration { // 指定初始化方法和销毁方法,init和destory的是Bean里面的方法名 @Bean (initMethod = "init" ,destroyMethod = "destory" ) public Person person(){ return new Person( "xiaomaomao" , 22 ); } @Bean public Tiger tiger(){ return new Tiger(); } } |
3、测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class SpringDemo { @Test public void springTest01() throws Exception { // 单例的情况容器启动完成调用了Person的构造方法创建Person对象,并且初始化Person对象,初始化完成之后调用Tiger的afterPropertiesSet()方法 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration. class ); Person person = context.getBean( "person" , Person. class ); // 多例的情况下获取Bean的时候才会调用Tiger的afterPropertiesSet()方法 Tiger tiger = context.getBean( "tiger" , Tiger. class ); // 单例的情况下调用close()方法会调用Lion的destory()方法进行销毁动作 // 多例的情况下,调用close()方法不会调用Lion的destory()方法进行销毁动作 context.close(); } } |
4、测试结果
1 2 3 4 5 | 创建Person对象.... Person的初始化.... Tiger initial.... Tiger destory.... Person的销毁.... |
三、Spring后置处理器(BeanPostProcessor)
1、自定义类实现BeanPostProcessor,在postProcessBeforeInitialization()方法中定义初始化之前的逻辑,在postProcessAfterInitialization()方法中定义初始化之后的逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println( "MyBeanPostProcessor..........postProcessBeforeInitialization" ); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println( "MyBeanPostProcessor..........postProcessAfterInitialization" ); return bean; } } |
2、配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // 标记这是一个Spring配置类 @Configuration public class SpringConfiguration { // 指定初始化方法和销毁方法,init和destory的是Bean里面的方法名 @Bean (initMethod = "init" , destroyMethod = "destory" ) public Person person() { return new Person( "xiaomaomao" , 22 ); } @Bean public Tiger tiger() { return new Tiger(); } @Bean public MyBeanPostProcessor myBeanPostProcessor(){ return new MyBeanPostProcessor(); } } |
3、测试类
1 2 3 4 5 6 7 8 9 10 11 | public class SpringDemo { @Test public void springTest01() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration. class ); Person person = context.getBean( "person" , Person. class ); Tiger tiger = context.getBean( "tiger" , Tiger. class ); MyBeanPostProcessor myBeanPostProcessor = context.getBean( "myBeanPostProcessor" , MyBeanPostProcessor. class ); context.close(); } } |
4、测试结果
1 2 3 4 5 6 7 8 9 10 11 | 创建Person对象.... // 初始化之前调用postProcessBeforeInitialization()方法 MyBeanPostProcessor..........postProcessBeforeInitialization Person的初始化.... // 初始化完成之后调用postProcessAfterInitialization()方法 MyBeanPostProcessor..........postProcessAfterInitialization MyBeanPostProcessor..........postProcessBeforeInitialization Tiger initial.... MyBeanPostProcessor..........postProcessAfterInitialization Tiger destory.... Person的销毁.... |
四、JSR250规范
@PostConstruct:在bean创建完成并且属性赋值完成时执行初始化方法
@PreDestory:在容器销毁bean之前通知我们执行清理工作
分类:
Spring 常用注解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?