首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring day by day-day two-Spring Lifecycle

2012-10-13 
spring day by day---day two---Spring Lifecycle先看看spring生命周期有关的两个接口:interface Initiali

spring day by day---day two---Spring Lifecycle
先看看spring生命周期有关的两个接口:

interface InitializingBean{    void init();}interface DisposableBean{    void destroy();}


实现了InitializingBean接口的Bean,Spring会在创建实例该Bean后调用init()方法。Spring设置Bean属性的方法有两种,一种是Constructor,一种是Setters,这在applicationContext.xml文件配置:
<bean id="foo" init-method="myInit" destroy-method="myDestroy"><property name="name"><value>LIAOFENG</value></property><property name="age"><value>23</value></property></bean><bean id="bar" init-method="myInit" destroy-method="myDestroy"><constructor-arg index="0"><value>LIAOFENG</value></constructor-arg><constructor-arg index="1"><value>23</value></constructor-arg></bean>

     两个Bean foo和bar从配置上的效果是一致的,只是如果使用Constructor,则必须要有对应的构造器,而且参数类型要与index一致(MyEclipse会自动检测,没有则xml文件报错)。此时Spring容器构造该对象实例的顺序是先通过该构造函数得到实例,然后调用InitializingBean的afterPropertiesSet()方法,最后调用init-method指定的方法myInit();
     如果使用setter,当然要提供属性的setter。此时当代码使用beanFactory.getBean("bar")时,先通过缺省构造器得到一个实例,然后调用对应的setters设置属性,再调用init方法,最后才执行myInit()方法。

      对于DisposableBean的destroy()方法,用作容器销毁bean时调用。也可以在配置bean时使用destroy-method设置销毁该bean时需要调用的方法。如果同时实现了DisposableBean和配置了destroy-method方法,那么是先调用destroy方法,然后在执行destroy-method指定的方法。然而,容器什么时候调用这两个方法呢?如果是Servlet,可以在Servelt.destroy()方法中调用。bean对象是无法触发事件去调用两个销毁方法的,只用通过beanFactory.destroySingletons()方法来触发:
XmlBeanFactory factroy = new XmlBeanFactory(new FileSystemResource("src/applicationContext.xml"));Foo foo = (Foo)factroy.getBean("foo");System.out.println(foo);factroy.destroySingletons();

在具有多个出口点的程序呢?我们可以通过ShutdownHook线程,它会在程序停止之间执行run()方法中代码,因此我们将factory.destroySingletons()方法即可:
public class Foo implements InitializingBean,DisposableBean,Runnable{private static XmlBeanFactory factroy = new XmlBeanFactory(new FileSystemResource("src/applicationContext.xml"));public static void main(String[] args){Runtime.getRuntime().addShutdownHook(new Thread(new Foo()));/*if(1==1){System.exit(0);}*/Foo foo = (Foo)factroy.getBean("foo");System.out.println(foo);factroy.destroySingletons();System.exit(0);}public Foo(){System.out.println("THIS IS THE DEFAULT CONSTRUCTOR...");}@Overridepublic void run(){System.out.println("IN THE RUN METHOD,CALL THE TARGET METHOD:");System.out.println(factroy.isSingleton("foo"));factroy.destroySingletons();}@Overridepublic void destroy(){System.out.println("THE DESTROY METHOD OF DISPOSABLEBEAN INTERFACE...");}public void myDestoryMethod(){System.out.println("THIS IS DESTROY METHOD OF MYSELF...");}@Overridepublic void afterPropertiesSet(){System.out.println("THIS IS IN THE AFTERPROPERTIESSET METHOD...");}private String name;private int age;public Foo(String name,int age){this.name=name;this.age=age;}public void init(){if(name==null || "".equals(name)){System.out.println("USING DEFAULTNAME...");name = "DEFAULT_NAME";}if(age>Integer.MAX_VALUE || age<0){System.out.println("设置年龄中...");try {throw new Exception("年龄不正确!");} catch (Exception e) {e.printStackTrace();}}}@Overridepublic String toString(){return this.getClass().getName()+",name:"+this.name+",age:"+this.age;}public String getName() {return name;}public void setName(String name) {System.out.println("SET THE NAME...");this.name = name;}public int getAge() {return age;}public void setAge(int age) {System.out.println("SET THE AGE...");this.age = age;}}

热点排行