spring day by day---day two---Spring Lifecycle
先看看spring生命周期有关的两个接口:
interface InitializingBean{ void init();}interface DisposableBean{ void destroy();}<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>
XmlBeanFactory factroy = new XmlBeanFactory(new FileSystemResource("src/applicationContext.xml"));Foo foo = (Foo)factroy.getBean("foo");System.out.println(foo);factroy.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;}}