Spring BeanFactory实例化Bean的过程
Bean的实例化是Bean生命周期的一个非常重要的环节,一般来说,初始化后,就不再改变了,直到bean被从BeanFactory中显式的移除。
当从BeanFactory中通过getBean()方法获取一个bean的时候,BeanFactory会经过如下的步骤来构建Bean的实例,这正是实例化Bean的过程:
1、调用Bean的默认构造方法,或者在指定的构造方法,生成bean实例(暂称为instance1);
3、如果Bean的配置文件中注入了Bean属性值,则在instance1基础上进行属性注入形成instance2,这种注入是覆盖性的。
2、如果Bean实现了InitializingBean接口,则调用afterPropertiesSet()方法,来改变或操作instance2,得到instance3;
4、如果Bean的配置文件中指定了init-method="init"属性,则会调用指定的初始化方法,则在instance3的基础上调用初始化方法init(),将对象最终初始化为instance4;当然,这个初始化的名字是任意的。
实际上,instance1、2、3、4仅仅是一个实例在不同阶段的状态。
下面给一个简单的例子验证一下上面的结论:
public class Person implements InitializingBean, DisposableBean {
    private String name;
    private int age;

public Person(){
        System.out.println("--------|| 构造方法在调用...");
        this.name = "默认用户名";
        this.age= 250;
        System.out.println(this);
    }

public String getName() {
        return name;
    }

public void setName(String name) {
        this.name = name;
    }

public int getAge() {
        return age;
    }

public void setAge(int age) {
        this.age = age;
    }

public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

public void init(){
        System.out.println("--------|| init()正在调用...");
        this.name="init";
        this.age=998;
        System.out.println(this);
    }

public void afterPropertiesSet() throws Exception {
        System.out.println("--------|| afterPropertiesSet()正在调用...");
        this.age=999;
        System.out.println(this);
    }

public void destroy() throws Exception {
        System.out.println("--------|| destroy()正在调用...");
    }
}

public class BeanContextHelper {
    private static ApplicationContext _instance;

static {
        if (_instance == null) _instance = buildApplicationContext();
    }

private BeanContextHelper() {
    }

/**
     * 重新构建ApplicationContext对象
     *
     * @return ApplicationContext
     */
    public static ApplicationContext buildApplicationContext() {
        return new ClassPathXmlApplicationContext("applicationContext.xml");
    }

/**
     * 获取一个ApplicationContext对象
     *
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return _instance;
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean id="person" class="com.lavasoft.springnote.ch01.Person" init-method="init" destroy-method="destroy">
        <property name="name">
            <value>lavasoft</value>
        </property>
        <property name="age">
            <value>22</value>
        </property>
    </bean>
</beans>

/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-4-17 16:58:07<br>
* <b>Note</b>: 客户端测试
*/
public class Test {
    public static void main(String args[]) {
        Test test = new Test();
        test.test();

}
    public void test(){
        ApplicationContext context = BeanContextHelper.getApplicationContext();
        Person person = (Person) context.getBean("person");
        System.out.println("--------|| 从Spring BeanFactory获取person...");
        System.out.println(person);
    }
}

运行结果:
--------|| 构造方法在调用...
log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
log4j:WARN Please initialize the log4j system properly.
Person{name='默认用户名', age=250}
--------|| afterPropertiesSet()正在调用...
Person{name='lavasoft', age=999}
--------|| init()正在调用...
Person{name='init', age=998}
--------|| 从Spring BeanFactory获取person...
Person{name='init', age=998}

Process finished with exit code 0

从控制台打印出来的信息可以很清楚看明白Bean实例化的过程。
对Person中实现的几个接口功能做个简单介绍:
BeanNameAware
只有一个方法void setBeanName(String name),用来设置bean的名字.
DisposableBean
为Bean的销毁提供回调功能,在bean实例销毁前调用接口的destroy()方法.
InitializingBean
Spirng的InitializingBean为bean提供了定义初始化方法的方式。InitializingBean是一个接口,它仅仅包含一个方法:afterPropertiesSet()。

Spring BeanFactory实例化Bean的过程相关推荐

  1. Spring生命周期Bean初始化过程详解

    Spring生命周期Bean初始化过程详解 Spring 容器初始化 Spring Bean初始化 BeanFactory和FactoryBean 源码分析 Bean的实例化 preInstantia ...

  2. 第三节 Spring加载bean的过程

    前言 通过前面两节的学习我们已经知道了Spring是如何解析XML与装载BeanDefinition的:在本章节中我们将继续学习bean的装载过程这将会面临更大的挑战,bean加载的功能实现远比bea ...

  3. 关于Spring容器管理Bean的过程以及加载模式

    1.需要将bean的定义信息声明在Spring的配置文件中: 2.需要通过Spring抽象出的各种Resource来指定对应的配置文件: 3.需要显示声明一个Spring工厂,该工厂用来掌控我们在配置 ...

  4. spring容器实例化bean的3种方式

    1. 使用公共的无参构造器 2. 使用静态工厂 (与设计模式的工厂无关) 3. 使用实例工厂 (与设计模式的工厂无关) 注解:第一种常用 转载于:https://www.cnblogs.com/wan ...

  5. 【Spring】BeanFactory解析bean详解

    为什么80%的码农都做不了架构师?>>>    本文是Spring源码分析中的一篇,来讲讲Spring框架中BeanFactory解析bean的过程,先来看一个在Spring中一个基 ...

  6. Spring装配Bean的过程

    首先说一个概念:"懒加载" 懒加载:就是我们在spring容器启动的是先不把所有的bean都加载到spring的容器中去,而是在当需要用的时候,才把这个对象实例化到容器中. spr ...

  7. Spring源码系列(十二)Spring创建Bean的过程(二)

    1.写在前面 上篇博客主要Spring在创建Bean的时候,第一次调用的Bean的后置处理器的过程,同时笔者也打算将整个Spring创建的Bean的过程,通过这个系列,将Bean的创建过程给讲清楚,废 ...

  8. 通过Debug带你详细了解Spring创建Bean的过程,一清二楚!

    Spring流程Debug 1.1 Spring测试环境搭建 Spring模块概览,绿色是模块,Spring中八大模块,黑色表示该模块包含的jar包(组件).例如我们想要用IOC容器,也就是绿色的Co ...

  9. Spring 初始化与 Bean 初始化

    目录 1.Spring 容器初始化 2.Spring Bean 初始化 2.1 BeanFactory 和 FactoryBean 2.2 Bean 的实例化 Spring生命周期Bean初始化过程详 ...

最新文章

  1. dos命令关闭所有dos窗口
  2. 远程桌面无法复制文本时解决办法
  3. Java GC系列(4):垃圾回收监视和分析
  4. drools dmn_DMN 1.1 XML:从建模到使用Drools 7.0的自动化
  5. ubuntu如何安装samba
  6. python CV2裁剪图片并保存
  7. Xshell连接阿里云服务器ECS
  8. 凯利公式自动计算表_EXCEL——可自动计算的收支表,全函数计算,拿上就用!
  9. linux mac docky,Ubuntu 7.10中安装酷酷的MAC风格 dock(图)
  10. zend studio设置自动保存
  11. TDtree冲刺第四天
  12. java怎么控制页面打印次数_java – 控制打印页面上面板的大小
  13. Python——词频统计
  14. CNKI E-Study与Endnote 的参考文献题录互导
  15. 深度学习之Bias/Variance偏差、方差
  16. word里画的流程图怎么全选_流程图怎么做?用Word制作流程图超方便!
  17. java 转16进制_java中进制的转换,Byte与16进制的转换方法
  18. linux 进程 ldt,LInux 描述符GDT, IDT LDT结构定义
  19. 抢答器c语言程序设计,基于单片机的智能抢答器设计与实现
  20. 自定义View之仿QQ运动步数进度效果

热门文章

  1. CSS画基本图形——圆
  2. SQL SERVER 用sql语句将一列数据拼接成一个字符串
  3. .NET三种事务处理详解
  4. 实战:移动APP项目产品开发流程详解
  5. 亚马逊生鲜的产品质疑!国外设计师怎么分析用户体验(中英图文对照版)
  6. 报名中 | 微软首席人工智能科学家邀你参加一场AI领域的私密聚会
  7. PMCAFF | 从200块到300万,史上最全自媒体报价单
  8. 资深猎头揭露:他们为何能年薪百万?
  9. C语言进阶剖析第三课--浮点数的秘密
  10. [note]浏览器兼容性(embed标签、字体兼容相关