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. 前端div层级控制

    z-index:20000;使用此属性可以控制div的层级即哪个在上哪个在下

  2. ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: An attempt by a client to chec

    数据库连接超时,是数据库连接时的相关配置写错,例如:数据库密码,驱动等问题

  3. WordPress二级菜单设置

    关于二级菜单的设置,首先建立几个自己的分类目录,然后打开菜单设置界面,用鼠标自由拖动即可.注意,这里说的一级菜单和二级菜单都是分类目录,所 以我们写文章的时候应该同时选择一级菜单和二级菜单两个分类目录 ...

  4. mySQL语法中的存储过程及if语句的使用简例

    create procedure gh() #注意各个地方的分号!此代码应先运行除掉最后一句的部分,然后运行call gh显示已经存储的结果 BEGIN declare c_no int; #声明数据 ...

  5. Python基础-week04

    本节内容摘要:#Author:http://www.cnblogs.com/Jame-mei 装饰器 迭代器&生成器 Json & pickle 数据序列化 软件目录结构规范 作业:A ...

  6. Java 缩写总结

    1.JVM:Java Virtual Machine(Java虚拟机)的缩写. 它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的. Java语言的一个非常重要的特点就是与 ...

  7. Mycat 分片规则详解--枚举分片

    实现方式:切分规则根据文件(partition-hash-int.txt)配置的可能的枚举来进行分片,此种分片规则理解为枚举分区,会比较适合于取值固定的场合,比如说省份(固定值) 优点:适用于按照省份 ...

  8. java设计模式------建造者模式

    建造者模式(Builder),将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 类图 描述 Builder:定义一个建造者抽象类,以规范产品对象的各个组成部分的建造.这个接口 ...

  9. 微信小程序中实现微信支付

    最近在做微信小程序,今天刚好做到小程序里的微信支付这块,踩过不少坑,特此写个博客记录下,希望能帮到其它人吧. 我总结了一下,小程序中的微信支付和之前其它的公众号里的微信支付有两个区别,第一就是小程序必 ...

  10. Alpha冲刺No.7

    一.站立式会议 彻底完成初步的界面设计 实现界面的简单跳转 完成部分事件监听 移植摄像头.图库功能到真实手机环境测试 数据库上传获取日记 二.项目实际进展 完成了简单的界面设计 大致完成了跳转任务 数 ...