默认情况下,会在初始化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="
xmlns:xsi="
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()方法