CSDN博主:【java_wxid】
CSDN博主:点击【Java廖志伟】
CSDN社区:点击【幕后大佬】
码云:点击【互联网Java工程师知识扫盲】
随笔 - 882,  文章 - 0,  评论 - 1,  阅读 - 51800

IOC之Bean的生命周期
实验22:创建带有生命周期方法的bean

public class Person {
	private Integer id;
	private String name;

	public void init() {
		System.out.println("这是person对象的初始化方法");
	}

	public void destroy() {
		System.out.println("这是person对象的销毁方法");
	}

配置信息:

 <!-- 
 	init-method="init" 		初始化方法	在对象被创建之后
 	destroy-method="destroy"	销毁方法	在容器关闭的时候执行
  -->
<bean id="p24" class="com.pojo.Person" init-method="init" destroy-method="destroy">
	<property name="id" value="24"></property>
</bean>

测试代码:

@Test
public void test13() throws Exception {
	ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println(applicationContext.getBean("p24"));
	applicationContext.close();
}

Bean的后置处理器BeanPostProcessor
bean的后置处理器,可以在bean对象初始化之前或之后做一些工作。
要使用bean的后置处理器,需要实现这个接口并配置。

实验23:测试bean的后置处理器

person对象,一定要有初始化方法

public class Person {
	private Integer id;
	private String name;
	private Car car;

	public void init() {
		System.out.println("这是person对象的初始化方法");
	}

后置处理器对象

public class MyBeanPostProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessAfterInitialization(Object bean, String id) throws BeansException {
		System.out.println("初始化之后执行  bean => " + bean + ", id => " + id);
		return bean;
	}
	/**
	 * bean是当前正在初始化的对象
	 * id 是当前正在初始化对象的id值 
	 */
	@Override
	public Object postProcessBeforeInitialization(Object bean, String id) throws BeansException {
		System.out.println("初始化之前执行  bean => " + bean + ", id => " + id);
		return bean;
	}

}

配置信息:

 <!-- 
 	init-method="init" 		初始化方法	在对象被创建之后
 	destroy-method="destroy"	销毁方法	在容器关闭的时候执行
  -->
<bean id="p24" class="com.pojo.Person" init-method="init" destroy-method="destroy">
	<property name="id" value="24"></property>
</bean>

<!-- 配置bean的后置处理器 -->
<bean class="com.pojo.MyBeanPostProcessor" />

测试的代码:

@Test
public void test13() throws Exception {
	ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean2.xml");
	System.out.println(applicationContext.getBean("p24"));
	applicationContext.close();
}

对于单例的bean,生命周期有11个步骤:
1.instantiate bean对象实例化,bean对象实例化,是在加载配置文件的时候实例的。即,我们启动spring容器的时候,加载配置文件,此时就实例化bean了。
2.populate properties 封装属性
3.如果Bean实现BeanNameAware, 执行 setBeanName
4.如果Bean实现BeanFactoryAware 或者 ApplicationContextAware,设置工厂 setBeanFactory 或者上下文对象 setApplicationContext
5.如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization(此点常常用来增强bean)
6.如果Bean实现InitializingBean 执行 afterPropertiesSet
7.调用 指定初始化方法 init
8.如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessAfterInitialization(此点常常用来增强bean)
9.执行业务处理
10.如果Bean实现 DisposableBean 执行 destroy
11.调用 指定销毁方法

posted on   我是廖志伟  阅读(3)  评论(0编辑  收藏  举报  
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

南北踏尘
点击右上角即可分享
微信分享提示