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. HTML5之tabindex属性

    1 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title&g ...

  2. 关于Oracle中查询的数字值的显示格式需要保留小数点后两位(或者三位,及其他位数)

    关于Oracle中查询的数字值的显示格式需要保留小数点后两位(或者三位,及其... 方法一:使用to_char的fm格式,即: to_char(round(data.amount,2),'FM9999 ...

  3. css3中与背景相关的元素

    1.background-origin:border-box/padding-box()默认值/content-box背景图片从边框出现.从边距开始出现.在盒子的内容中出现. 2.background ...

  4. ASP.NET页面之间传递值的几种方式

    目录 QueryString Session Cookie Application 一.QueryString QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中. ...

  5. 时间同步ntp服务的安装与配置

    1,首先安装ntp服务. [root@localhost /]# yum install ntp -y 2,修改ntp配置文件.(ntp配置文件在:/etc/ntp.conf) [root@local ...

  6. Sublime无法使用package control安装插件

    我之前想通过安装sftp,但是出现了这个问题,百度了很久才解决.东西也是从网上找的,现总结下:   网上说什么安装个新的,我也是简直醉了,其实新的并不好使. 但是,我们最好安装个新的,再继续下面的操作 ...

  7. 通过其他页面跳转到tableBar指示的界面

    通过代理实现 让tableBar遵从代理,让代理的实现方法设置 [self setSelectedIndex:2]; setSelectedIndex:2 代表tableBar的item第3个处于选中 ...

  8. [UCSD白板题] Covering Segments by Points

    Problem Introduction You are given a set of segments on a line and your goal is to mark as few point ...

  9. poj1274(匈牙利算法)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22809   Accepted: 101 ...

  10. MongoDB学习笔记—03 增删改查操作

    MongoDB的CURD操作分别通过函数insert().update().find().remove()进行 MongoDB文档新增与删除 MongoDB中关于文档的新增与删除比较简单.主要通过in ...