Spring - Bean生命周期

LifeCycleBean

1
2
3
4
5
6
7
8
9
10
11
12
package main.com.lee.d_lifecycle;
public class LifeCycleBean {
     public LifeCycleBean() {
         System.out.println( "LifeCycleBean 构造了..." );
     }
     public void setUp() {
         System.out.println( "LifeCycleBean 初始化了..." );
     }
     public void tearDown() {
         System.out.println( "LifeCycleBean 销毁了..." );
     }
}

applicationContext.xml

1
2
<!-- Bean生命周期 -->
< bean id = "lifeCycleBean" class = "main.com.lee.d_lifecycle.LifeCycleBean" init-method = "setUp" destroy-method = "tearDown" />

Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main.com.lee.d_lifecycle;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class LifeCycleTest {
     @Test
     public void demo1() {
         // ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
         ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml" );
         LifeCycleBean lifeCycleBean = (LifeCycleBean) applicationContext.getBean( "lifeCycleBean" );
         System.out.println(lifeCycleBean);
         System.out.println( 333 );
         applicationContext.close();
     }
}

Out

1
2
3
4
5
6
7
LifeCycleBean 构造了...
LifeCycleBean 初始化了...
main.com.lee.d_lifecycle.LifeCycleBean@c791b9
333
14:03:37,594  INFO ClassPathXmlApplicationContext:1042 - Closing org.springframework.context.support.ClassPathXmlApplicationContext@efd552: startup date [Mon Dec 23 14:03:37 CST 2013]; root of context hierarchy
14:03:37,595  INFO DefaultListableBeanFactory:444 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b48197: defining beans [lifeCycleBean]; root of factory hierarchy
LifeCycleBean 销毁了...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值