spring中Bean的生命周期

先贴代码:
Life类
package com.open.bean;
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;
public class Life implements BeanFactoryAware, BeanNameAware,
InitializingBean, DisposableBean {
private String msg;
public Life() {
System.out.println("msg="+msg);
System.out.println("构造函数");
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("setBeanFactory");
}
public void setBeanName(String name) {
System.out.println("setBeanName");
}
public void init() {
System.out.println("初始化");
}
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessBeforeInitialization");
return null;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessAfterInitialization");
return null;
}
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet");
}
public void destroy() throws Exception {
System.out.println("destroy");
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}

BeanPostProcessorImp类
package com.open.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class BeanPostProcessorImp implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessBeforeInitialization");
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessAfterInitialization");
return bean;
}
}

BeanCounter类
package com.open.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class BeanCounter implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("类的数量="+beanFactory.getBeanDefinitionCount());
}
}

bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="life" name="life_name" class="com.open.bean.Life"
init-method="init">
<property name="msg" value="lifexxxx"/>
</bean>
<bean id="processor" class="com.open.bean.BeanPostProcessorImp"/>
<bean id="counter" class="com.open.bean.BeanCounter"/>
</beans>

测试类
package com.open.bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext cx=
new ClassPathXmlApplicationContext("bean.xml");
Life life=(Life)cx.getBean("life");
}
}

输出结果
[list]
[*]类的数量=3
[*]msg=null
[*]构造函数
[*]setBeanName
[*]setBeanFactory
[*]postProcessBeforeInitialization
[*]afterPropertiesSet
[*]初始化
[*]postProcessAfterInitialization
[/list]
解释:
[list]
[*]调用BeanFactoryPostProcessor接口的postProcessBeanFactory方法
[*]-->实例化,调用构造函数-->设置属性值
[*]-->调用BeanNameAware的setBeanName()方法
[*]-->调用BeanFactoryAware接口的setBeanFactory
[*]-->调用BeanPostProcessor接口的postProcessBeforeInitialization()
[*]-->调用InitializingBean接口的afterPropertiesSet()
[*]-->调用bean.xml中制定的init-method指定init()方法
[*]-->调用BeanPostProcessor接口的postProcessAfterInitialization()
[*]容器关闭
[*]-->DisposableBean接口的destroy()
[*]-->调用bean.xml中制定的destroy-method指定的go()方法
[/list]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值