Spring IOC容器中 Bean 的生命周期方法(还有添加Bean后置处理器后的生命周期)

IOC容器中 Bean 的生命周期方法

在spring中Bean的生命周期分为五步,或者说是五个过程:

  1. 调用构造器(生产Bean实例)
  2. set方法注入对象
  3. 调用对象的初始化方法(既是init()方法)
  4. 执行main方法中要执行的语句
  5. 容器关闭时执行对象的销毁方法(destroy方法)`

这里是我写的对象代码:

package com.spring.cycle;

public class Car {
	public Car() {
		System.out.println("Car's Constructor...");
	}
	private String brand;
	public void setBrand(String brand) {
		System.out.println("setBrand...");
		this.brand = brand;
	}
	public void init() {
		// TODO Auto-generated method stub
		System.out.println("init...");
	}
	public void destroy() {
		System.out.println("destroy...");
	}
}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="car" class="com.spring.cycle.Car"
	p:brand="Audi" init-method="init" destroy-method="destroy"></bean>

</beans>

init-method=“init”, destroy-method="destroy"这两个属性是指定Bean的初始化和销毁方法。

最后执行代码块,以及运行结果:

package com.spring.cycle;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext act = new ClassPathXmlApplicationContext("beans-cycle.xml");
		Car car = (Car)act.getBean("car");
		System.out.println(car);
		//关闭IOC容器
		act.close();
	}
}

在这里插入图片描述

接下来是添加Bean后置处理器之后。。。。

上源码:

package com.spring.cycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessAfterInitialization:"+ bean+","+beanName);
		return bean;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessBeforeInitialization:"+ bean +","+beanName);
		return bean;
	}

}

在包中新建MyBeanPostProcessor 类并且实现BeanPostProcessor接口。

然后,,把这个类对象(bean)配置到spring配置文件中去。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="car" class="com.spring.cycle.Car"
	p:brand="Audi" init-method="init" destroy-method="destroy"></bean>
	
	<!-- 
		实现BeanPostProcessor接口,并具体提供两个方法的实现
		Object postProcessAfterInitialization(Object bean, String beanName):在init-method之前被调用
		Object postProcessBeforeInitialization(Object bean, String beanName):在init-method之后被调用
		
		bean:bean实例本身,Object对象
		beanName:IOC容器配置的bean的名字
		返回值:是实际上返回给用户的那个Bean,注意,可以在以上两个方法中修改返回的bean,甚至返回一个新的bean
	 -->
	<!-- 配置bean的后置处理器 -->
	<bean class="com.spring.cycle.MyBeanPostProcessor"></bean>
	
</beans>

上面的 Car.java 和 Main.java 不用做出改变。
直接执行main方法:
在这里插入图片描述
最后总结:

  1. 调用构造器(创建bean实例)
  2. set方法注入对象
  3. 执行Bean实例传递给Bean后置处理器的postProcessBeforeInitialization方法(如果不在配置文件中配置Bean的后置处理器,则没有这一步)
  4. 调用对象的初始化方法(既是init()方法)
  5. 执行Bean实例传递给Bean后置处理器的postProcessAfterInitialization方法(如果不在配置文件中配置Bean的后置处理器,则没有这一步)
  6. 执行main方法中要执行的语句
  7. 当容器关闭时,执行对象的销毁方法(destroy方法)

打完收工。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值