Spring框架:bean的生命周期

bean的生命周期

1,bean的创建(使用了构造函数)

2,bean的初始化

3,bean的方法执行

4,bean的销毁

1  public void demoTest3() {
2                  //此处调用构造方法,并执行初始化方法setUp
3          ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
4          StuDAO studao = (StuDAO)ac.getBean("StuDAO");
5          studao.foo();//方法执行
6          ac.close();//bean被销毁
7      }
 1 /**
 2  * 执行顺序为:
 3  *     构造方法执行了
 4  *     初始化了。。。
 5  *     方法调用了
 6  *     被销毁了。。。
 7  *
 8  * 
 9  */
10 public class StuDAOImpl implements StuDAO{
11     public StuDAOImpl() {
12         System.out.println("构造方法执行了");
13     }
14 
15     public void setUp() {
16         System.out.println("初始化了。。。");
17     }
18 
19     @Override
20     public void foo() {
21         System.out.println("方法调用了");
22         
23     }
24     
25     public void setDown() {
26         System.out.println("被销毁了。。。");
27     }
28 
29 }

猜你喜欢

转载自www.cnblogs.com/noperx/p/11269334.html