bean的生命周期(可控点的位置)
【PersonServiceImpl 】
package com.athl.service;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.core.ResolvableType;
@SuppressWarnings("unused")
public class PersonServiceImpl implements IPersonService,BeanNameAware,BeanFactoryAware,InitializingBean,DisposableBean {
private String adao;
private String bdao;
public void setAdao(String adao) {
System.out.println("Step2:执行setAdao()");
this.adao = adao;
}
public void setBdao(String bdao) {
System.out.println("Step2:执行setBdao()");
this.bdao = bdao;
}
public PersonServiceImpl(){
System.out.println("Step1:对象创建");
}
@Override
public String doFirst() {
System.out.println("Step9:执行主业务方法");
return "";
}
@Override
public void doSecond() {
System.out.println("执行doSecond()方法");
}
public void initAfter(){
System.out.println("Step7:初始化之后");
}
public void destroyPre(){
System.out.println("Step11:销毁之前");
}
@Override
public void setBeanName(String arg0) {
System.out.println("Step3:beanName = personService");
}
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("Step4:获取beanFactory容器");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Step6:当前Bean的初始化工作完毕");
}
@Override
public void destroy() throws Exception {
System.out.println("Step10:准备开始销毁工作,进行销毁流程");
}
}
【BeanPostProcessor】
package com.athl.beanPostProcessor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* Bean后处理器是一种特殊的Bean,容器中所有的Bean在初始化时,容器均会自动调用该类的两个方法.
* 一个是在目标bean初始化之前调用;另一个在目标bean初始化之后调用.
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
/**
* bean:当前调用执行Bean后处理器的Bean对象
* beanIdOrName:当前调用执行Bean后处理器的Bean对象的id,若Bean没有id就是name.
* 利用对参数beanIdOrName和method.getName()的判断可实现对指定的Bean的指定方法进行加强、扩展.
*/
@Override
public Object postProcessBeforeInitialization(final Object bean, String beanIdOrName)
throws BeansException {
System.out.println("Step5:执行postProcessBeforeInitialization()");
return bean;
}
@Override
public Object postProcessAfterInitialization(final Object bean, String beanIdOrName)
throws BeansException {
System.out.println("Step8:执行postProcessAfterInitialization()");
return bean;
}
}
【applicationContext.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="personService" class="com.athl.service.PersonServiceImpl" init-method="initAfter" destroy-method="destroyPre">
<property name="adao" value="AAA"/>
<property name="bdao" value="BBB"/>
</bean>
<!-- 注册Bean后处理器(无id,是容器自动调用的) -->
<bean class="com.athl.beanPostProcessor.MyBeanPostProcessor" />
</beans>
【test】
package com.athl.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.athl.service.IPersonService;
public class Mytest {
@Test
public void test(){
/*加载Spring配置文件,创建Spring容器对象,找的是src根目录下配置文件*/
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
/*从容器中获取指定Bean对象*/
IPersonService service = (IPersonService) ac.getBean("personService");
service.doFirst();
/*显示关闭容器*/
((ClassPathXmlApplicationContext)ac).close();
}
}
【控制台结果打印】
Step1:对象创建
Step2:执行setAdao()
Step2:执行setBdao()
Step3:beanName = personService
Step4:获取beanFactory容器
Step5:执行postProcessBeforeInitialization()
Step6:当前Bean的初始化工作完毕
Step7:初始化之后
Step8:执行postProcessAfterInitialization()
Step9:执行主业务方法
Step10:准备开始销毁工作,进行销毁流程
Step11:销毁之前