Spring容器通过配置可以实现对每个 bean初始化时的查找和销毁时的回调调用。这也就是说,一个应用的开发者可以借助于初始化的回调方法init() 轻松的写一个类(不必像XML配置文件那样为每个bean都配置一个'init-method="init"'属性)。Spring IoC容器在创建bean的时候将自动调用这个方法。
为了完全弄清如何使用该特性,让我们看一个例子。
首先,创建一个Bean
package cn.csdn.cycle; public class CycleLife { private String var; public void setVar(String var) { this.var = var; } public void display() { System.out.println("J2EE Spring:" + var); } public void init() { System.out.println("调用初始化方法init()...."); } public void destroy() { System.out.println("调用销毁方法destroy()...."); } }
其次,在xml文件中对你创建的Bean进行相应的配置,这里我将xml文件取名为applicationContext.xml
<bean id="cle" class="cn.csdn.cycle.CycleLife" init-method="init" destroy-method="destroy"> <property name="var"> <value>Hello</value> </property> </bean>
第三,新建一个JUnit用例测试你的应用(当然,你亦可以创建一个含有main()方法的Java类测试)
@Test public void test() { ApplicationContext ac = new ClassPathXmlApplicationContext( new String[] { "application*.xml" }); //执行此行代码时即调用init()方法 CycleLife cle = (CycleLife) ac.getBean("cle"); cle.display(); //注意下面的代码,它主要用来调用destroy()方法 AbstractApplicationContext aac = (AbstractApplicationContext) ac; aac.close(); }
使用这个功能可以把你从为每个bean指定初始化和销毁回调的繁杂工作中解救出来。为了一致性,应该强制性的为初始化和销毁回调方法采用一致的命名规则。
在前面的基础上,我们再来分析一下实现了BeanPostProcessor接口的Bean的生命周期。同样,举例来加以说明。
首先呢,我们创建三个JavaBean,第一个同上面的CycleLife.java,这里只创建另外两个,TempService.java代码清单如下:
package cn.csdn.cycle; public class TempService { private String str; public void setStr(String str) { this.str = str; } public void init() { System.out.println("这是TempService:" + str); } } PostService.java代码清单: package cn.csdn.cycle; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class PostService implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization"); return bean; } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization"); if (bean instanceof TempService) { ((TempService) bean).setStr("sxpgog"); } 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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="ps" class="cn.csdn.cycle.PostService"></bean> <bean id="ts" class="cn.csdn.cycle.TempService" init-method="init"></bean> <bean id="cle" class="cn.csdn.cycle.CycleLife" init-method="init" destroy-method="destroy"> <property name="var"> <value>^_^</value> </property> </bean> </beans>
测试代码同上面的JUnit用例,这里我就不再写了。执行后的效果如下:
postProcessBeforeInitialization
这是TempService:sxpgog
postProcessAfterInitialization
postProcessBeforeInitialization
调用初始化方法init()....
postProcessAfterInitialization
J2EE Spring:^_^
调用销毁方法destroy()....
从上面的案例中我们不难发现,postProcessBeforeInitialization()方法是在init()方法之前执行。也就是说,在IoC对Bean进行初始化之前我们还可以做一些前期的处理,例如在上面的postProcessBeforeInitialization()方法中我们对Bean中的str进行赋值(重新赋值)的操作,这也从另外一个方面说明,postProcessBeforeInitialization()方法在setter()方法之前执行。