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. I/O知识

    1.jdk1.4之前(jdk1.4开始提供了nio)的早起版本,java对I/O的支持并不完善,开发人员开发高性能I/O程序时,面临的问题主要有:     没有缓冲区,I/O性能存在问题     没有 ...

  2. silverLight--绑定数据dataGrid

    后台代码编写 ,为表格绑定数据: using System; using System.Collections.Generic; using System.Linq; using System.Net ...

  3. SAAS在中国市场的发展前景

    发展现状 2008年1-7月软件行业实现收入4199.1亿元,同比增长32.4%.2008年第一季度,业务管理软件市场总量达16.31亿元,同比增长17.8%.面对成本的上升,企业选择了向信息化要效率 ...

  4. mybatis框架中动态SQL的编写

    1.动态SQL:在SQL语句中加入流程控制.比如加入if,foreach等. 重点掌握if语句: 案例1: <update id="updateItem" parameter ...

  5. html5新特性之画布

    1.canvas的理解 canvas是一个矩形区域,在这个区域内,通过js可以对区域内的每一帧像素控制 2.js操作canvas对象 canvas对象.getContext("2d" ...

  6. php socket解决方案

    最近一直在为移动应用提供 php服务端api,以前 实时交互数据需求不严格(定时从手机端发送http请求),现在业务需求变更, 需要实时交互式接口,必须增加socket. 服务端框架使用YII 1.1 ...

  7. 在ubuntu14.04上部署基于Docker的Gitlab

    首先在一台新的ubuntu上执行更新: sudo apt-get update 然后安装docker(采用国内源) curl -sSL https://get.daocloud.io/docker | ...

  8. oracle后台进程详解

    oracle后台进程伴随实例的启动而启动,他们主要是维护数据库的稳定,相当于一个企业中的管理者及内部服务人员.他们并不会直接给用户提供服务.   一:database write--数据写入  DBW ...

  9. nexus2.1.2的配置

    最近在学习maven,逐渐接触到私服的搭建,也就着手学习使用nexus了,在http://www.sonatype.org/nexus/go网站上nexus最新版本的是,不过版本要同jvm的版本匹对, ...

  10. sharepoint 网站创建

    打开开始菜单,右键sharepoint管理中心以管理员身份打开 打开管理web应用程序菜单,并新建web应用程序 新建web应用程序配置,大多数采用默认配置. 建议自定义端口号,URL的端口号和设置的 ...