spring2.5.6学习笔记七:Spring管理的Bean的生命周期

来源:互联网 发布:狙击精英3 武器数据 编辑:程序博客网 时间:2024/06/03 08:18

默认情况下,会在初始化spring容器的时候实例化bean,验证

在构造方法中输出

public PersionServiceBean(){
    System.out.println("我被实例化了!");
}

测试类中单执行下面代码

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

输出:我被实例化了!

 

但如果scope="prototype"

是在获取bean的时候才实例化

 

如果设置延迟初始化lazy-init="true"

也是在获取bean的时候才实例化

如果设置所有的bean都延迟实例化则  default-lazy-init="true"

xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd " default-lazy-init="true">

可以设置对象被实例化时,调用的初始化方法

当对象被实例化时,即调用PersionServiceBean中的init()方法

可以设置对象被销毁时调用的方法

测试:

AbstractApplicationContext actx = new ClassPathXmlApplicationContext("beans.xml");

actx.close();//当正常关闭spring容器时,即调用PersionServiceBean中的destroy()方法

原创粉丝点击