Bean的生命周期问题

测试Bean的生命周期
//Bean的生命周期
	@Test
	public void test7() {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		BeanLifeCycle beanLifeCycle = (BeanLifeCycle) applicationContext.getBean("beanLifeCycle");
		beanLifeCycle.add();
		applicationContext.close();



结果:
第一步:实例化BeanLifeCycle对象
第二步:属性name注入second
第三步:得到bean的id值或name值:beanLifeCycle
第四步:到Application对象:org.springframework.context.support.ClassPathXmlApplicationContext@36c14a61: startup date [Thu Mar 28 11:00:15 CST 2019]; root of context hierarchy
第五步:BeanPostProcessor的before方法
处理的bean是:bean.BeanLifeCycle@6f1cf25c,它的名称是beanLifeCycle
第六步:属性注入完成后
第七步:自定义的init方法
第八步:BeanPostProcess的after方法
第九步:自定义功能方法add...
第十步:执行destory方法
第十一步:执行myDestory方法
	}
applicationContext
<bean name="beanLifeCycle" class="bean.BeanLifeCycle" init-method="myInit" destroy-method="myDestroy">
		<!-- 属性注入 -->
		<property name="name" value="second"></property>
	</bean>
	<bean class="bean.MyBeanPostProcessor"></bean>
BeanLifeCycle implements BeanNameAware
/**
 * 
1.instantiate bean对象实例化
2.populate properties 封装属性
3.如果Bean实现BeanNameAware执行setBeanName
4.如果Bean实现BeanFactoryAwar或ApplicationContextAwar设置工厂setBeanFactory或上下文对象setApplicationContext
5.如果存在类实现BeanPostProcessor(后处理Bean),执行postProcessBeforeInitialization
6.如果Bean实现InitializingBean执行afterPropertiesSet
7.调用自定义的init-method方法
8.如果存在类实现BeanPostProcessor(处理Bean),执行postProcessAfterInitialization
9.执行业务处理
10.如果Bean实现DisposableBean执行destroy
11.调用自定义的destroy-method
 * @author 45度炸
 *
 */
public class BeanLifeCycle implements BeanNameAware, ApplicationContextAware,InitializingBean,DisposableBean{
	private String name;
	public BeanLifeCycle() {
		System.out.println("第一步:实例化BeanLifeCycle对象");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		System.out.println("第二步:属性name注入"+name);
		this.name = name;
	}
	@Override
	public void setBeanName(String name) {
		System.out.println("第三步:得到bean的id值或name值:"+name);
		
	}
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		System.out.println("第四步:到Application对象:"+applicationContext);
		
	}
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("第六步:属性注入完成后");
		
	} 
	
	public void myInit() {
		System.out.println("第七步:自定义的init方法");
	}
	
	public void add() {
		System.out.println("第九步:自定义功能方法add...");
	}
	
	@Override
	public void destroy() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("第十步:执行destory方法");
	}
	
	public void myDestroy() {
		System.out.println("第十一步:执行myDestory方法");
	}
	
	
	
	
}
MyBeanPostProcessor implements BeanPostProcessor
public class MyBeanPostProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println("第八步:BeanPostProcess的after方法");
		return bean;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println("第五步:BeanPostProcessor的before方法");
		System.out.println("处理的bean是:"+bean+",它的名称是"+beanName);
		return bean;
	}

}
-------------------------------使用动态代理增强add方法---------------------------------------------------
@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
			//使用动态代理对BeanLifeCycle中的add方法做一个增强
			@Override
			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				// TODO Auto-generated method stub
				if(method.getName().equals("add")) {
					long currentTimeMillis1 = System.currentTimeMillis();
					Object invoke = method.invoke(proxy, args);
					long currentTimeMillis2 = System.currentTimeMillis();
					return invoke;
				} else {
					return method.invoke(proxy, args);
				}
				
			}
		});
				return bean;
		
//		System.out.println("第五步:BeanPostProcessor的before方法");
//		System.out.println("处理的bean是:"+bean+",它的名称是"+beanName);
//		return bean;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值