测试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
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 {
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() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
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;
}