Spring(十二):IOC容器中Bean的生命周期方法
本文内容纲要:
- IOC容器中Bean的生命周期方法- 创建Bean后置处理器
IOC容器中Bean的生命周期方法
1)Spring IOC容器可以管理Bean的声明周期,Spring允许在Bean生命周期的特定点执行定制的任务。
2)Spring IOC容器对Bean的生命周期进行管理的过程:
--- 通过构造器或工厂方法创建Bean实例;
--- 为Bean的属性设置值和对其他Bean的引用;
--- Bean可以使用
--- 当容器关闭是,调用Bean的销毁方法
3)在Bean的声明里设置init-method和destroy-method属性,为Bean指定初始化和销毁方法。
演示:
**第一步:**创建一个Java Project,并导入以下包:
第二步:在src下添加com.dx.spring.beans.cycle.Member.java和spring bean配置文件bean-cycle.xml
Member.java
package com.dx.spring.beans.cycle;public class Member {
public Member() {
System.out.println("Member constructor ... ");
}
private String name;
public void setName(String name) {
System.out.println("setName " + name);
this.name = name;
}
public void initMethod() {
System.out.println("init method");
}
public void destroyMethod() {
System.out.println("despose method");
}
}
**注意:**Member中的initMethod和destroyMethod两个方法名字是任意的。
bean-cycle.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="member" class="com.dx.spring.beans.cycle.Member" init-method="initMethod" destroy-method="destroyMethod">
<property name="name" value="member1"></property>
</bean>
</beans>
第三步:使用IOC容器管理Bean并关闭IOC容器查看打印日志:
package com.dx.spring.beans.cycle;import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bean-cycle.xml");
Member member = (Member) ctx.getBean("member");
System.out.println(member);
ctx.close(); // 关闭IOC容器
}
}
打印信息如下:
信息: Loading XML bean definitions from class path resource [bean-cycle.xml]Member constructor ...
setName member1
init method
com.dx.spring.beans.cycle.Member@4f7d0008
May 24, 2018 9:41:33 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4bf558aa: startup date [Thu May 24 21:41:33 CST 2018]; root of context hierarchy
despose method
创建Bean后置处理器
1)Bean后置处理器允许在调用初始化方法前后对Bean进行额外的处理;
2)Bean后置处理器对IOC容器里的所有Bean实例逐一进行处理,而非单一实例。其经典应用是:检查Bean属性的正确性或根据特定的标准更改Bean的属性。
3)对Bean后置处理器而言,需要实现abstract interface org.springframework.beans.factory.config.BeanPostProcessor抽象接口。在初始化方法被调用前后,Spring将把每个Bean实例分别传递给上述接口的以下两个方法:
演示:
第一步:添加com.dx.spring.beans.cycle.MyBeanPostProcessor.java使其实现抽象接口类BeanPostProcessor
package com.dx.spring.beans.cycle;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization:" + beanName + "," + bean);
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization" + beanName + "," + bean);
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
}
第二步:修改spring-cycle.xml配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="member" class="com.dx.spring.beans.cycle.Member" init-method="initMethod" destroy-method="destroyMethod">
<property name="name" value="member1"></property>
</bean>
<!-- 注意:这里不需要指定id -->
<bean class="com.dx.spring.beans.cycle.MyBeanPostProcessor"></bean>
</beans>
第三步:执行Main测试,查看打印信息:
信息: Loading XML bean definitions from class path resource [bean-cycle.xml]Member constructor ...
setName member1
postProcessBeforeInitialization:member,com.dx.spring.beans.cycle.Member@1a968a59
init method
postProcessAfterInitializationmember,com.dx.spring.beans.cycle.Member@1a968a59
com.dx.spring.beans.cycle.Member@1a968a59
May 24, 2018 9:46:07 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4bf558aa: startup date [Thu May 24 21:46:06 CST 2018]; root of context hierarchy
despose method
本文内容总结:IOC容器中Bean的生命周期方法,创建Bean后置处理器,
原文链接:https://www.cnblogs.com/yy3b2007com/p/9085285.html
以上是 Spring(十二):IOC容器中Bean的生命周期方法 的全部内容, 来源链接: utcz.com/z/362487.html