一.概念理解

1.容器

IoC容器负责容纳并管理bean,在Spring中,BeanFactory是IoC容器的核心接口。 它的职责包括:实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。

Spring提供两种IoC容器的实现:

(1)BeanFactory       :org.springframework.beans.factory.BeanFactory 是Spring IoC容器的基本实现,是Spring IoC容器实际代表者

BeanFactory是Spring框架的基础设施,面向Spring本身

(2)ApplicationContext:提供了更多的高级特性,是BeanFactory的子类。

ApplicationContext面向开发者。几乎所有的场合都直接使用ApplicationContext而非底层的BeanFactory。

ApplicationContext 的主要实现类:
                      ClassPathXmlApplicationContext:从 类路径下加载配置文件
                      FileSystemXmlApplicationContext: 从文件系统中加载配置文件

ConfigurableApplicationContext 扩展于 ApplicationContext,新增加两个主要方法:refresh() 和 close(), 让 ApplicationContext 具有启动、刷新和关闭上下文的能力

2.bean

在Spring中,那些组成你应用程序的主体(backbone)及由Spring IoC容器所管理的对象,被称之为bean。 简单地讲,bean就是由Spring容器初始化、装配及管理的对象

bean的定义由BeanDefinition 对象来表示,该定义将包含以下信息:

  1. 全限定类名:           这通常就是已定义bean的实际实现类。
  2. bean行为的定义:    这些定义将决定bean在容器中的行为(作用域、生命周期回调等等)
  3. 对其他bean的引用: 这些引用bean也可以称之为协作bean(collaborators) 或依赖bean(dependencies).
  4. 创建bean实例时的其他配置设置。比如使用bean来定义连接池,可以通过属性或者构 造参数指定连接数,以及连接池大小限制等。

上述内容直接被翻译为每个bean定义包含的一组properties。

二.bean的配置

1. 简单实例

java bean:  HelloWorld.java

package com.ray.spring;

public class HelloWorld {
    private String name;

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

    public void hello(){
        System.out.println("hello:"+name);
    }
}

配置文件:applicationContext.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"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 配置一个 bean
        class:bean的全类名,通过反射的方式在IoC容器中穿件Bean.所以要求Bean中必须有无参构造器
        id:标识容器中的bean,id唯一。若有多个id,则第一个是标识符,其他为别名。
    -->
    <bean id="helloWorld" class="com.ray.spring.HelloWorld">
        <!-- 为属性赋值
             通过属性注入: 通过 setter 方法注入属性值
        -->
        <property name="name" value="Tom"></property>
    </bean>

</beans>

bean的实例化:Main.java

package com.ray.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {

        //1. 创建 Spring 的 IOC 容器,ApplicationContext代表IoC容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        /*2. 从 IOC 容器中获取 bean 的实例
          2.1利用id定位到IoC容器中的bean
        */
        HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");

        //2.2利用类型返回IoC容器中的bean,但要求IoC容器中必须只有一个该类型的bean
        //HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");

        //3. 使用 bean
        helloWorld.hello();
    }

}

2. bean的依赖注入

Spring 支持 3 种依赖注入的方式:属性注入、构造器注入、工厂方法注入(很少使用,不推荐)

2.1 属性注入

属性注入即通过 setter 方法注入Bean 的属性值或依赖的对象

属性注入是实际应用中最常用的注入方式。

<?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:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <bean id="helloWorld" class="com.ray.spring.HelloWorld">
        <!-- 通过属性注入: 通过 setter 方法注入属性值
               <property>节点:设置属性
            name :指定 Bean 的属性名称
            value 或 <value> 子节点:指定属性值
        -->
        <property name="name" value="Tom"></property>
    </bean>

</beans>

2.2 构造器注入

构造器注入即 通过配置构造器参数,来注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。

Java bean: Car.java

package com.ray.spring;

public class Car {
    private String brand;
    private String  corp;
    private double price;
    private int maxSpeed;

    public Car(String brand, String corp, double price) {
        super();
        this.brand = brand;
        this.corp = corp;
        this.price = price;
    }

    public Car(String brand, String corp, int maxSpeed) {
        super();
        this.brand = brand;
        this.corp = corp;
        this.maxSpeed = maxSpeed;
    }

    @Override
    public String toString() {
        return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
                + ", maxSpeed=" + maxSpeed + "]";
    }

}

配置文件: applicationContext.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"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 通过构造器注入属性值
      1.按参数顺序注入-->
    <bean id="car1" class="com.ray.spring.Car">
        <!-- 都要求: 在 Bean 中必须有对应的构造器.  -->
        <constructor-arg value="Audi"></constructor-arg>
        <constructor-arg value="ShangHai"></constructor-arg>
        <constructor-arg value="300000"></constructor-arg>
    </bean>

    <!--2.通过参数索引注入-->
    <bean id="car2" class="com.ray.spring.Car">
        <constructor-arg value="Audi"   index="0"></constructor-arg>
        <constructor-arg value="ShangHai" index="1"></constructor-arg>
        <constructor-arg value="300000"   type="double"></constructor-arg>
    </bean>

    <!--3.通过参数类型注入-->
    <bean id="car3" class="com.ray.spring.Car">
        <constructor-arg value="Audi"   type="java.lang.String"></constructor-arg>
        <constructor-arg value="ShangHai" type="java.lang.String"></constructor-arg>
        <constructor-arg value="300000"   type="int"></constructor-arg>
    </bean>
</beans>

bean的实例化:

package com.ray.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {

        //1. 创建 Spring 的 IOC 容器,ApplicationContext代表IoC容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        //2. 从 IOC 容器中获取 bean 的实例
        Car car=(Car)ctx.getBean("car1");
        System.out.println(car);

        car=(Car)ctx.getBean("car2");
        System.out.println(car);

        car=(Car)ctx.getBean("car3");
        System.out.println(car);

    }

}

Spring总结_04_容器和bean的更多相关文章

  1. Spring学习-- IOC 容器中 bean 的生命周期

    Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...

  2. Spring IOC 一——容器装配Bean的简单使用

    下文:SpringIOC 二-- 容器 和 Bean的深入理解 写在前面 这篇文章去年写的,缘起于去年某段时间被领导临时"抓壮丁"般的叫过去做java开发,然后在网上找了一个 Sp ...

  3. Spring重点—— IOC 容器中 Bean 的生命周期

    一.理解 Bean 的生命周期,对学习 Spring 的整个运行流程有极大的帮助. 二.在 IOC 容器中,Bean 的生命周期由 Spring IOC 容器进行管理. 三.在没有添加后置处理器的情况 ...

  4. Spring为IOC容器注入Bean的五种方式

    一 @Import导入组件,id默认是组件的全类名 //类中组件统一设置.满足当前条件,这个类中配置的所有bean注册才能生效: @Conditional({WindowsCondition.clas ...

  5. Spring技术内幕_IOC容器载入Bean定义资源文件

    转自:http://blog.csdn.net/chjttony/article/details/6259723 1.当spring的IoC容器将Bean定义的资源文件封装为Spring的Resour ...

  6. SpringIOC 二—— 容器 和 Bean的深入理解

    上文:Spring IOC 一--容器装配Bean的简单使用 上篇文章介绍了 Spring IOC 中最重要的两个概念--容器和Bean,以及如何使用 Spring 容器装配Bean.本文接着记录 S ...

  7. spring容器对bean生命周期的管理三中方式

    spring容器对bean的生命周期管理主要在两个时间点:bean的初始化完成(包括属性值被完全注入),bean的销毁(程序结束,或者引用结束)方式一:使用springXML配置中的init-meth ...

  8. [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  9. Spring基础——在 IOC 容器中 Bean 之间的关系

    一.在 Spring IOC 容器中 Bean 之间存在继承和依赖关系. 需要注意的是,这个继承和依赖指的是 bean 的配置之间的关系,而不是指实际意义上类与类之间的继承与依赖,它们不是一个概念. ...

随机推荐

  1. Spring学习笔记IOC与AOP实例

    Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...

  2. 第四组UI组件:AdapterView及子类

    AdapterView组件是一组重要的组件,AdapterView本省是一个抽象基类,它派生的子类在用法上十分相似,只是显示界面与一定的区别,因此这次针对它们的共性集中讲解,并突出介绍他们的区别. A ...

  3. Java线程:线程安全类和Callable与Future(有返回值的线程)

    一.线程安全类 当一个类已经很好的同步以保护它的数据时,这个类就称为线程安全的.当一个集合是安全的,有两个线程在操作同一个集合对象,当第一个线程查询集合非空后,删除集合中所有元素的时候,第二个线程也来 ...

  4. U盘为什么还有剩余空间,但却提示说空间不够

    你的U盘是FAT32格式,它只支持单一小于4G的文件复制,将U盘改为NTFS格式,可以解决题.方法:开始——运行,输入“cmd”,回车,在命令符后输入:convert h: /fs:ntfs,回车(假 ...

  5. lo dash api

    https://lodash.com/docs 用 Lo-Dash 替换 underscore http://segmentfault.com/a/1190000000359484

  6. 用Zephir编写PHP扩展

    自从NodeJS,和Golang出来后,很多人都投奔过去了.不为什么,冲着那牛X的性能.那PHP的性能什么时候能提升一下呢?要不然就会被人鄙视了.其实大牛们也深刻体会到了这些威胁,于是都在秘密开发各种 ...

  7. Flex组件的生命周期

    组件实例化生命周期描述了用组件类创建组件对象时所发生的一系列步骤,作为生命周期的一部分,flex自动调用组件的的方法,发出事件,并使组件可见. 下面例子用as创建一个btn控件,并将其加入容器中 va ...

  8. Java泛型的类型擦除

    package com.srie.testjava; import java.util.ArrayList; import java.util.List; public class TestGener ...

  9. POJ2796(单调栈)

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12987   Accepted: 3639 Case T ...

  10. C++ 头文件系列 (bitset)

    简介 该头文件有关位集,实际上是vector 位 位本质上对应bool的概念,只有0或1,true或false两种对立的值. 但很可惜,字节才是机器上最小的存储单元,所以bool基本上是由一个字节大小 ...