一.概念理解

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. eclipse调试找不到源解决办法

    eclipse调试时有时显示找不到源码,首先得确定代码没问题 这是eclipse没有发现工程源码,解决办法是 右键工程>>Debug As >> Debug configura ...

  2. C# WInform 界面左导航菜单

    如图所示: 下载位置: http://pan.baidu.com/s/1c1uRwkw

  3. jdbc数据连接池dbcp要导入的jar包

    jdbc数据连接池dbcp要导入的jar包 只用导入commons-dbcp-x.y.z.jarcommons-pool-a.b.jar

  4. 如何使用Ninja快速编译LLVM和Clang

    在使用Make工具编译LLVM是非常耗时的.往往需要三四个小时.但是使用goolge开源的ninja编译LLVM只需要10到20分钟. 本文以llvm3.3为例,演示在linux上编译和安装过程. 第 ...

  5. FMS中的onStatus

    在FlashCom中的Camera, Microphone, LocalConnection, NetConnection,NetStream和 SharedObject对象都提供了事件响应,onst ...

  6. Spring IO Platform简介及示例

    什么是Spring IO Platform Spring IO Platform,简单的可以认为是一个依赖维护平台,该平台将相关依赖汇聚到一起,针对每个依赖,都提供了一个版本号: 这些版本对应的依赖都 ...

  7. ADXL345经验总结,采用SPI和I2C总线操作

    一. ADXL345简介       ADXL345是ADI公司推出的三轴(x,y,z)iMEMS数字加速度计(digital accelerometer),具有在16G下高分辨率(13Bit)测量能 ...

  8. LinQ 创建连接、简单增删改查

    LINQ--语言集成查询(Language Integrated Query)是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以查询数据库相同的方式操 ...

  9. JavaScript 语法

    一.JavaScript简介 1.JavaScript是个什么东西? 它是个脚本语言,需要有宿主文件,它的宿主文件是HTML文件. 2.它与Java什么关系? 没有什么直接的联系,Java是Sun公司 ...

  10. Express之get,pos请求参数的获取

    Express的版本4.X Get query参数的获取 url假设:http://localhost:3000/users/zqzjs?name=zhaoqize&word=cool& ...