spring容器对于bean的装配提供了两个接口容器分别是"ApplicationContext接口容器"和"BeanFactory接口容器",其中"BeanFactory接口容器"是spring的顶级接口容器,"ApplicationContext接口容器"继承了"BeanFactory接口容器",并且两个接口容器对于Bean的装配时机是不同的,ApplicationContext接口容器是在容器初始化阶段就对容器中所有的bean进行了装配,而BeanFactory接口容器接口容器是在容器对象调用getBean方法是才对bean进行装配的。

  另外,spring的这两种接口容器在对bean进行装配时,都是先去调用类的无参构造方法,创建一个空值对象。

  下面我们通过实例来进行说明。

  添加maven依赖。

<properties>
<org.springframework.version>4.1.4.RELEASE</org.springframework.version>
</properties>
<dependencies>
<!-- spring相关jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
</dependencies>

Student类

public class Student {

    public Student() {
System.out.println("spring加载String类");
} }

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <bean id="student" class="com.opensource.bean.Student"></bean> </beans>

测试类:

public class MyTest {

    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-bean.xml");

        BeanFactory bf= new XmlBeanFactory(new ClassPathResource("spring-bean.xml"));
bf.getBean("student"); } }

我们分别来看一下两个容器对于创建Student类的实例时的日志:

(1)ApplicationContext接口容器

(2) BeanFactory接口容器,只有在调用getBean("student")方法时才会调用Student的无参构造方法,在容器中创建Studnet的实例。

调用getBean("student");

  在这里,我们可以狭义的把spring的配置文件看作是spring容器的显式表现,因为在配置文件中我们可以看到容器对那些bean进行了装配。从容器的顶级接口BeanFactory我们不难看出,spring容器对于bean的创建使用了工厂模式,从<bean id="student" class="com.opensource.bean.Student"></bean>标签的class属性使用了全类名,我们也可以知道对于实例的创建使用了java的反射机制。id属性具有唯一性,我们从容器中取得实例使用了getBean("student"),也可以看出来spring容器对于本的装配使用的数据结构应该是Map集合的数据形式。

  最后说一点,我们作为程序员,研究问题还是要仔细深入一点的。当你对原理了解的有够透彻,开发起来也就得心应手了,很多开发中的问题和疑惑也就迎刃而解了,而且在面对其他问题的时候也可做到触类旁通。当然在开发中没有太多的时间让你去研究原理,开发中要以实现功能为前提,可等项目上线的后,你有大把的时间或者空余的时间,你大可去刨根问底,深入的去研究一项技术,为觉得这对一名程序员的成长是很重要的事情。

spring2——IOC之Bean的装配的更多相关文章

  1. 使用Spring IoC进行Bean装配

    Spring概述 Spring的设计严格遵从的OCP(开闭原则),保证对修改的关闭,也就是外部无法改变spring内部的运行流程:提供灵活的扩展接口,也就是可以通过extends,implements ...

  2. IoC容器-Bean管理XML方式(自动装配)

    IoC操作Bean管理(XML自动装配) 1,什么是自动装配 (1)根据指定装配规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入 2,演示自动装配过程 (1)根据属性名称自动注入 ...

  3. 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)

    Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...

  4. Spring学习记录(三)---bean自动装配autowire

    Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系,少写几个ref autowire: no ---默认情况,不自动装配,通过ref手动引用 byName---根据 ...

  5. Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配 1.1 使用:只需在<bean>中使用autowire元素 <bean id="student" class="com.k ...

  6. Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...

  7. Spring温故而知新 - bean的装配(续)

    按条件装配bean 就是当满足特定的条件时Spring容器才创建Bean,Spring中通过@Conditional注解来实现条件化配置bean package com.sl.ioc; import ...

  8. Spring温故而知新 - bean的装配

    Spring装配机制 Spring提供了三种主要的装配机制: 1:通过XML进行显示配置 2:通过Java代码显示配置 3:自动化装配 自动化装配 Spring中IOC容器分两个步骤来完成自动化装配: ...

  9. bean的装配方式(注入方式,构造注入,setter属性注入)

    bean的装配方式有两种,构造注入和setter属性注入. public class User { private String username; private String password; ...

随机推荐

  1. Problem : (1.2.1) Text Reverse

    #include<iostream> using namespace std; void main() { char arr[1000]; int a,n; int s,t; cin> ...

  2. 关于 preHandle 重写和添加参数问题,重写HttpServletRequestWrapper和Filter

    由于 preHandle 中HttpServletRequest 只有setAttribute而没有setParameter 也没有 add 方法 所以是没办法直接添加参数的.从网上查了很多资料,基本 ...

  3. CMake 条件判断

    CMake简介 CMake 是做什么的? CMake是一套类似于automake的跨平台辅助项目编译的工具. 我觉得语法更加简单易用. CMake的工作流程 CMake处理顶级目录的CMakeList ...

  4. epel扩展库的安装

    epel扩展库的安装 2017-10-09  18:07:48 个人原创,转载请注明作者,出处,否则追究法律责任 1,centos6.x系统中,必需安装epel-release-6-8.noarch. ...

  5. windows下安装mysql以及启动

    配置环境变量,在path中添加 ;E:\wamp\Apache24\mysql(这是你的mysql安装路径),然后在修改一下配置文件my-default.ini(mysql安装文件夹目录下) 修改其中 ...

  6. MongoDB入门系列:复制机制

    一.复制原理 MongoDB的复制功能是使用操作日志oplog实现的,oplog包含主节点(Master)的每一次写操作,oplog是local本地数据库中的一个数据集合,其它非主节点(Seconda ...

  7. ASP.NET MVC编程——模型

    1 ViewModel 是一种专门提供给View使用的模型,使用ViewModel的理由是实体或领域模型所包含的属性比View使用的多或少,这种情况下实体或领域模型不适合View使用. 2模型绑定 默 ...

  8. AndroidStudio R 文件标红

    一种不常见的问题 AndroidStudio 文件大小会有一定的限制,超过一定大小将无法解析.大型的Android项目容易出现这个问题. 可以按照下面的步骤解决这个问题: 在AndroidStudio ...

  9. ping通但打不开网页

    ping通但打不开网页 网页出现: The proxy server is refusing connections Firefox is configured to use a proxy serv ...

  10. New UWP Community Toolkit - RadialGauge

    概述 New UWP Community Toolkit  V2.2.0 的版本发布日志中提到了 RadialGauge 的调整,本篇我们结合代码详细讲解  RadialGauge 的实现. Radi ...