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. 如何写出如散文般的代码――《代码整洁之道》读书笔记(Ch1-Ch3)

    不知道有多少人像我一样,程序出现问题时添加函数添加变量解决,变量名用a,b,c等"简单"的字母来表示.不知道有多少人像我一样,看完自己的代码,心里暗骂"什么玩意儿!&qu ...

  2. ASP.NET Core Web 支付功能接入 支付宝-电脑网页支付篇

    这篇文章将介绍ASP.NET Core中使用 开源项目 Payment,实现接入支付宝-电脑网页支付接口及同步跳转及异步通知功能. 开发环境:Win 10 x64.VS2017 15.6.4..NET ...

  3. sklearn包中有哪些数据集你都知道吗?

    注册了博客园一晃有3个月了,同时接触机器学习也断断续续的算是有1个月了.今天就用机器学习神器sklearn包的相关内容作为我的开篇文章吧. 本文将对sklearn包中的数据集做一个系统介绍,并简单说一 ...

  4. window.open打开文件乱码

    问题:刚开始使用window.open在IE兼容模式下打开文件下载出现乱码. 一开始以为是文件名是中文导致的.然后使用a标签的download属性更改文件名解决. <a class=" ...

  5. 笔记:Maven 项目报告插件

    Maven 项目报告插件,都是对于前面生成的项目站点的内容丰富,因此都是基于项目站点的,生成的命令和生成项目站点一致(mvn site),项目报告插件的配置和一般插件不同,是在 project-> ...

  6. Dagger2的基本概念与实际应用。

    本文系原创博客,文中不妥烦请指出,如需转载摘要请注明出处! Dagger2的基本概念与实际应用 Alpha Dog 2016-11-30  10:00:00 本文Demo的github地址:https ...

  7. C语言描述链表的实现及操作

    一.链表的创建操作 // 操作系统 win 8.1 // 编译环境 Visual Stuido 2017 #include<stdio.h> #include<malloc.h> ...

  8. [poj3468]A Simple Problem with Integers_线段树

    A Simple Problem with Integers 题目大意:给出n个数,区间加.查询区间和. 注释:1<=n,q<=100,000.(q为操作次数). 想法:嗯...学了这么长 ...

  9. 记录一个古老的Sql分页过程

    /* 根据单位ID获取排班信息 For:WXX TIme:2017-11-22 */ ALTER proc [dbo].[proc_ScheduleInfo] )='', --单位ID )='', - ...

  10. 数据库ACID,SQL和NoSQL

    数据库中的事务(transaction)有ACID4个基本特性,可以类比交易: 1,A(Atomicity)原子性 事务里的事情要么全部做完,要么执行过程中失败,此时回滚. 2,C(Consisten ...