g-beans-2.5.xsd">
<bean id="animal" class="ioc.test.Animal" init-method="start" destroy-method="end">
<property name="age" value="5"></property>
<property name="name" value="猪"></property>
</bean>
</beans>
Spring受管Bean的与处理和后处理 三(3)
时间:2011-10-02 残梦追月
创建含有主方法的测试类,代码如下:
代码
/**
*
*/
package ioc.test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author zhangyong
*
*/
public class TestMain {
public static void main(String[] args) {
AbstractApplicationContext ac = new ClassPathXmlApplicationContext ("applicationContext.xml");
//注册容器关闭钩子,才能关掉容器,调用 析构方法
ac.registerShutdownHook();
Animal animal = (Animal) ac.getBean("animal");
System.out.println(animal.speak());
}
}
/**
*
*/
package ioc.test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author zhangyong
*
*/
public class TestMain {
public static void main(String[] args) {
AbstractApplicationContext ac = new ClassPathXmlApplicationContext ("applicationContext.xml");
//注册容器关闭钩子,才能关掉容器,调用 析构方法
ac.registerShutdownHook();
Animal animal = (Animal) ac.getBean("animal");
System.out.println(animal.speak());
}
}
运行主类,结果如下:
需要注意的是:要看到析构方法的输出,也必须要注册关闭钩子。
本文地址:http://www.blogjava.net/cmzy/archive/2008/07/29/218059.html) |