http://blog.csdn.net/yerenyuan_pku/article/details/52834011

bean的初始化时机

前面讲解了Spring容器管理的bean的作用域。接着我们就要思考一个问题:bean到底是在什么时候才进行实例化的呢?我们以这个问题为引子来展开本文的说明。 
bean对象无外乎是在以下两个时刻进行实例化的:

  1. 调用getBean()方法时。
  2. Spring容器启动时。

那么bean对象到底是在哪个时刻进行实例化的,这与Bean的作用域有着某种联系。我们以配置Spring管理的bean的作用域的案例为基础进行深入探讨。为了能够清楚地看到bean对象的实例化,我们需要修改PersonServiceBean类的代码为:

public class PersonServiceBean implements PersonService {
public PersonServiceBean() {
System.out.println("我被实例化了");
} @Override
public void save() {
System.out.println("我是save()方法");
}
}
  • 1
  • 当Spring的配置文件——beans.xml的内容为:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans 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.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean> </beans>
    • 1

    即bean的作用域为singleton时,我们修改SpringTest类的代码为:

    public class SpringTest {
    
        @Test
    public void test() {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 实例化Spring容器
    } }
    • 1

    此时,测试test()方法,Eclipse控制台输出:

    我被实例化了

    这说明了当bean的作用域为singleton时,bean对象是在Spring容器启动时就进行创建了。即默认情况下会在容器启动时初始化bean,但我们也可以指定bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。如: 
    我们将Spring的配置文件——beans.xml的内容改为:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans 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.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" lazy-init="true"></bean> </beans>
    • 1

    此时,测试test()方法,Eclipse控制台根本就不会输出这句话:

    我被实例化了

    lazy-init=”true”指定了不要在Spring容器启动时对这个bean进行实例化。 
    这时,只有将SpringTest类的代码修改为:

    public class SpringTest {
    
        @Test
    public void test() {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 实例化Spring容器
    PersonService personService = (PersonService) ctx.getBean("personService"); // 从Spring容器取得bean
    } }
    • 1

    再次测试test()方法,Eclipse控制台才会输出这句话:

    我被实例化了

    如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true”,如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans 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.xsd" default-lazy-init="true"> ...... </beans>
    • 1
  • 当Spring的配置文件——beans.xml的内容为:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans 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.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype"></bean> </beans>
    • 1

    即bean的作用域为prototype时,若SpringTest类的代码为:

    public class SpringTest {
    
        @Test
    public void test() {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 实例化Spring容器
    } }
    • 1

    测试test()方法,可以发现Eclipse控制台没输出这句话:

    我被实例化了

    这就说明了当bean的作用域为prototype时,bean对象并不会在Spring容器启动时就进行创建。 
    但是若将SpringTest类的代码改为:

    public class SpringTest {
    
        @Test
    public void test() {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 实例化Spring容器
    PersonService personService = (PersonService) ctx.getBean("personService"); // 从Spring容器取得bean
    } }
    • 1

    此时,再测试test()方法,可以发现Eclipse控制台输出了这句话:

    我被实例化了

    证实了当bean的作用域为prototype时,bean对象将会在调用getBean()方法时进行创建。

指定bean的初始化方法和销毁方法

我们希望在bean被初始化的时候,就初始化某些资源。为了达到这样的目的,我们可修改PersonServiceBean类的代码为:

public class PersonServiceBean implements PersonService {
public void init() {
System.out.println("初始化某些资源");
} public PersonServiceBean() {
System.out.println("我被实例化了");
} @Override
public void save() {
System.out.println("我是save()方法");
}
}
  • 1

这样,我们的目的就具体地成为:当Spring容器初始化PersonServiceBean对象之后,就要执行该对象的init()方法。为了达成这样的目的,只须修改Spring的配置文件——beans.xml的内容为:

<?xml version="1.0" encoding="UTF-8"?>
<beans 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.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" lazy-init="false"
init-method="init" /> </beans>
  • 1
  • 2

若SpringTest类的代码为:

public class SpringTest {

    @Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 实例化Spring容器
} }
  • 1

测试test()方法,Eclipse控制台将打印: 

现在我们又希望在bean被销毁的时候,就释放或关闭某些资源。为了达到这样的目的,我们可修改PersonServiceBean类的代码为:

public class PersonServiceBean implements PersonService {
public void init() {
System.out.println("初始化某些资源");
} public PersonServiceBean() {
System.out.println("我被实例化了");
} @Override
public void save() {
System.out.println("我是save()方法");
} /**
* bean到底是什么时候销毁的呢?如果没有人为地删除它,默认该bean一直在Spring容器中,
* 也就是说随着Spring容器的关闭,该bean才会被销毁。
*/
public void destroy() {
System.out.println("释放初始化的资源");
}
}
  • 1

试着思考这样一个问题:bean对象到底是什么时候销毁的呢?答案是:如果没有人为地删除它,默认该bean一直在Spring容器中,也就是说随着Spring容器的关闭,该bean才会被销毁。 
紧接着,我们要修改Spring的配置文件——beans.xml的内容。

<?xml version="1.0" encoding="UTF-8"?>
<beans 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.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" lazy-init="false"
init-method="init" destroy-method="destroy" /> </beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

最后,我们要修改测试类——SpringTest.java的代码为:

public class SpringTest {

    @Test
public void test() {
// ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 实例化Spring容器 AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ctx.close(); // 正常关闭Spring容器
} }
  • 1

此时,测试test()方法,Eclipse控制台将打印: 

这就是Spring管理的Bean的生命周期。源码可点击Spring管理的Bean的生命周期进行下载。

(转)Spring管理的Bean的生命周期的更多相关文章

  1. Spring 容器中 Bean 的生命周期

    Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...

  2. Spring 学习笔记---Bean的生命周期

    生命周期图解 由于Bean的生命周期经历的阶段比较多,我们将通过一个图形化的方式进行描述.下图描述了BeanFactory中Bean生命周期的完整过程: Bean 的生命周期从Spring容器着手实例 ...

  3. Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)

    Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...

  4. Spring实战(二)Spring容器和bean的生命周期

    引入问题: 在XML配置文件中配置bean后,这些文件又是如何被加载的?它们被加载到哪里去了? Spring容器——框架核心 1.什么是Spring容器?它的功能是什么? 在基于Spring的应用中, ...

  5. Spring基础14——Bean的生命周期

    1.IOC容器中的Bean的生命周期方法 SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务.SpringIOC容器对Bean的生命周期进行管理 ...

  6. spring(二):bean的生命周期

    bean的生命周期指的是bean的创建——>初始化——>销毁的过程,该过程是由spring容器进行管理的 我们可以自定义bean初始化和销毁的方法:容器在bean进行到当前生命周期时,调用 ...

  7. spring框架中Bean的生命周期

    一.Bean 的完整生命周期 在传统的Java应用中,bean的生命周期很简单,使用Java关键字 new 进行Bean 的实例化,然后该Bean 就能够使用了.一旦bean不再被使用,则由Java自 ...

  8. (spring-第1回【IoC基础篇】)Spring容器中Bean的生命周期

    日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...

  9. spring中的bean的生命周期

    bean的生命周期:bean的创建 —— 初始化 ——销毁的过程 容器管理bean的生命周期,我们可以自定义初始化和销毁方法,容器在bean进行到当前生命周期就会调用我们的方法 在xml配置文件中是在 ...

随机推荐

  1. 识别String类型变量的问题

    碰到了android无法识别string的问题 Cursor cursor = db.query(true, "user", new String[]{"id" ...

  2. 安装YCM出现:YouCompleteMe unavailable no module named frozendict或者 YouCompleteMe unavailable no module named future

    参考博文:http://blog.sina.com.cn/s/blog_8f70642d0102wo57.html 原因就是你或者没用Vundle安装,或者Vundle由于网速太慢下载到一半不能把安装 ...

  3. 【hdu 2222】Keywords Search

    [题目链接] 点击打开链接 [算法] 此题是AC自动机模板题 AC自动机是很神奇的算法,简单点来说,就是在一棵字典树上进行KMP,它的应用范围很广,非常实用 这篇博客写得很好,推荐阅读 : http: ...

  4. Filter的基本配置

    1.<dispatcher></dispatcher>节点:指定过滤器所拦截的servlet容器调用资源的方式,有REQUEST,INCLUDE,FORWARD,ERROR,默 ...

  5. 微信小程序wxml和wxss样式

    对于css不熟悉的android程序员来说,开发微信小程序面临的一个比较困难的问题就是界面的排版了.微信小程序的排版就跟wxml和wxss有关了,它们两者相当于android的布局文件,其中wxml指 ...

  6. Python 函数的参数传递

    C/C++中,传递参数的类型是可以指定的.一般来说,传递参数可以分为两种:值传递和引用传递.对于值传递,参数传递的过程中进行了复制操作,也就是说,在函数中对参数的任何改动都不会影响到传入的变量:对于引 ...

  7. C++ STL自学总结,仅供参考

    本文内容,为博主在网上看到资料总结整合而来 一.stl格式简介 .stl文件是在计算机图形应用系统,来表示封闭的面或者体,用来表示三角形网格的一种文件格式.为STereo Lithography的缩写 ...

  8. Managed C++ wtypes.h DATE 转化为 .net的 DateTime

    http://stackoverflow.com/questions/570224/how-do-i-convert-from-mfcs-coledatetime-to-c-sharp-datetim ...

  9. ubuntu 下配置django 项目能够被局域网下的其他电脑访问

    在项目下的路径下下运行 python manage.py runserver 后面的端口换成其他可用的端口也可以 如何让外网也能访问呢,有待更新

  10. UVa第十章数学概念与方法

    Bryce1010模板 10.1数论初步 1.欧几里得算法和唯一分解定理 2.Eratosthenes筛法 补充素数筛选 const int MAXN=1e6+10; ll prime[MAXN]; ...