Spring框架的核心功能之一就是控制反转(Inversion of Control, IoC),也叫做依赖注入(dependency injection, DI)。关于依赖注入的具体内容可以参见Martin Fowler写的一篇文章《Inversion of Control Containers and the Dependency Injection pattern》

Spring容器接口是BeanFactory,其提供了一些方法来配置和管理对象。ApplicationContext是BeanFactory的子接口,它集成了Spring的AOP特性,信息资源管理(用于全球化),公共事件等。简单的说,BeanFactory提供了配置框架及基本的功能,而ApplicationContext增加了更多的企业级定制功能。比如其实现类WebApplicationContext可用于web应用程序中。

在Spring中,应用程序中受Spring IoC容器管理的对象叫做bean,即bean是一个由Spring IoC容器实例化、装配及其它管理的对象。下图是Spring IoC容器的一个简单图解。

以下列出了几个常用的实现了ApplicationContext的容器对象。

  • AnnotationConfigApplicationContext :接收注解的class作为输入来初始化配置。

  • GenericGroovyApplicationContext: 根据Groovy DSL来初始化配置。

  • ClassPathXmlApplicationContext:根据当前classpath下的xml文件初始化配置。

  • FileSystemXmlApplicationContext:根据文件系统路径下的xml文件初始化配置。

Bean的定义有多种方式,XML定义,Annoation定义,Java代码直接定义,Groovy DSL定义等。之前例子基本都演示过这些定义方法。

一个简单的XML定义是这样的。

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="movieService" class="huangbowen.net.service.DefaultMovieService”/> </beans>

其包含一个id和一个class。id是一个bean的唯一标示,同一个spring容器中不能有两个id一样的bean,不过你也可以给bean起别名,使用name属性即可,多个别名可以用逗号,分号或空格分开。

1
<bean id="movieService" name="service1 service2" class="huangbowen.net.service.DefaultMovieService"/>
1
<bean id="movieService" name=“service1,service2" class="huangbowen.net.service.DefaultMovieService"/>
1
<bean id="movieService" name="service1;service2" class="huangbowen.net.service.DefaultMovieService"/>

也可以使用alisa来起别名。

1
2
3
<bean id="movieService" name="service1,service2" class="huangbowen.net.service.DefaultMovieService"/>

<alias name="movieService" alias="service3"/>

如果你的bean的实例不是通过构造函数直接生成的,而是通过工厂方法生成那,那么也有相应的配置方法。

1
<bean id="defaultMovieService" class="huangbowen.net.service.MovieServiceFactory" factory-method="GetMovieService" />
MovieServiceFactory
1
2
3
4
5
6
7
8
9
10
package huangbowen.net.service;

public class MovieServiceFactory {

    private static DefaultMovieService defaultMovieService = new DefaultMovieService();

    public static MovieService GetMovieService() {
return defaultMovieService;
}
}

如果bean对象是由一个实例工厂生成的,那么应该这样配置。

1
2
3
    <bean id="serviceLocator" class="huangbowen.net.service.MovieServiceLocator"/>

    <bean id="instantMovieService" factory-bean="serviceLocator" factory-method="GetMovieService"/>
MovieServiceLocator
1
2
3
4
5
6
7
8
9
10
package huangbowen.net.service;

public class MovieServiceLocator {

    private static DefaultMovieService defaultMovieService = new DefaultMovieService();

    public MovieService GetMovieService() {
return defaultMovieService;
}
}

本例中的源码请在我的GitHub上自行下载。

Spring-Context之四:Spring容器及bean的定义的更多相关文章

  1. Spring学习(4)IOC容器配置bean:定义与实例化

    一.  IOC容器配置 1. 一些概念 (1)IOC容器: 定义:具有管理对象和管理对象之间的依赖关系的容器. 作用:应用程序无需自己创建对象,对象由IOC容器创建并组装.BeanFactory是IO ...

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

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

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

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

  4. spring beans源码解读之--Bean的定义及包装

    bean的定义,包装是java bean的基础.再怎么强调它的重要性都不为过,因此深入 了解这块的代码对以后的代码研究可以起到事半功倍的功效. 1. Bean的定义BeanDefinition 1.1 ...

  5. Spring学习记录(二)---容器和bean属性配置

    下载spring包,在eclipse搭建spring环境. 这步我在eclipse中无法导入包,看网上的: http://sishuok.(和谐)com/forum/blogPost/list/242 ...

  6. Spring核心技术(一)——IoC容器和Bean简介

    IoC容器和Bean简介 这章包括了Spring框架对于IoC规则的实现.Ioc也同DI(依赖注入).而对象是通过构造函数,工厂方法,或者一些Set方法来定义对象之间的依赖的.容器在创建这些Bean对 ...

  7. Spring(3)——装配 Spring Bean 详解

    装配 Bean 的概述 前面已经介绍了 Spring IoC 的理念和设计,这一篇文章将介绍的是如何将自己开发的 Bean 装配到 Spring IoC 容器中. 大部分场景下,我们都会使用 Appl ...

  8. Spring 系列教程之容器的功能

    Spring 系列教程之容器的功能 经过前面几章的分析,相信大家已经对 Spring 中的容器功能有了简单的了解,在前面的章节中我们一直以 BeanFacotry 接口以及它的默认实现类 XmlBea ...

  9. 使用Spring.NET的IoC容器

    使用Spring.NET的IoC容器 0. 辅助类库 using System; using System.Collections.Generic; using System.Linq; using ...

随机推荐

  1. NMAKE:fatal error U1077.“\..\.cl.exe” return code 0xc0000135

    NMAKE:fatal error U1077.“cl.exe” return code 0xc0000135 产生原因:在安装visual studio的时候没有勾选注册环境变量导致的. 解决办法: ...

  2. NSURLSession的基本使用

    一.简单说明 在iOS9.0之后,以前使用的NSURLConnection过期,苹果推荐使用NSURLSession来替换NSURLConnection完成网路请求相关操作. NSURLSession ...

  3. Unity 编辑器的 界面布局 保存方法

    在软件界面的右上角(关闭按钮的下方),点击  layout  (界面)的下拉箭头. 弹出选项中的 save layout....(保存界面选项),输入命名,就可以生成这个界面的布局.  (软件本身也有 ...

  4. WinCE下读取注册表获得SD路径

    WinCE下读取注册表获得SD路径 [要点]WinCE注册表中[HKEY_LOCAL_MACHINE\System\StorageManager\Profiles\SDMemory\] 下键Folde ...

  5. Struts1 action重定向跳转 带参数

    ActionForward forward = new ActionForward("kmRentalMain.do?method=view&fdId="+id);forw ...

  6. storm入门(二):关于storm中某一段时间内topN的计算入门

    刚刚接触storm 对于滑动窗口的topN复杂模型有一些不理解,通过阅读其他的博客发现有两篇关于topN的非滑动窗口的介绍.然后转载过来. 下面是第一种: Storm的另一种常见模式是对流式数据进行所 ...

  7. [Leetcode][JAVA] Path Sum I && II

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  8. 我的Sharepoint母版页的使用

    在Sharepoint 2010母版页文件夹里面,有一个叫v4.master的母版页文件. 在它的基础上修改成想要的母版页.我们用最少的修改完成我们想要的效果. 我们有时是要这样的一个母版页,顶部Ri ...

  9. ECMall——安装时的小bug解决办法

    第一次安装ECmall,安装了好多遍,总是出现Strict Standards: Non-static method这样的错误,折腾了五六遍,还是安装不上,仍然是类似的错误.气愤!于是上百度查:Ecm ...

  10. Reactnative 随笔一

    ---恢复内容开始--- 1.ReactDOM.render()    React的最基本方法,用于将模板转为HTML语言,并插入指定的DOM节点 将h标签插入example节点 2.HTML语言直接 ...