0%

spring-container-overview

org.springframework.beans 和 org.springframework.context 包是Spring IOC 容器的基础。 BeanFactory 接口提供了一种能够管理任何类型对象的高级配置机制。ApplicationContext 是 BeanFactory 的一个子接口,主要扩展功能:

  • 更容易与 Spring 的 AOP 特性集成
  • 消息资源处理(用于国际化)
  • 事件发布
  • 应用层特定上下文,例如用于 Web 应用程序的 WebApplicationContext。

简而言之,BeanFactory 提供了配置框架和基本功能,ApplicationContext 增加了更多企业特定的功能。

org.springframework.context.ApplicationContext 接口代表 Spring IoC 容器,负责实例化、配置和组装 bean。容器通过读取配置元数据来获取有关要实例化、配置和组装哪些对象的指令。配置元数据以 XML、Java 注解或 Java 代码表示,用来描述对象和它们之间的依赖关系。

Spring 提供了 ApplicationContext 接口的几种实现,常见的有ClassPathXmlApplicationContext 或 FileSystemXmlApplicationContext 。XML 是定义配置元数据的传统格式,也可以通过提供少量 XML 配置启用对附加元数据格式的支持,从而指示容器使用 Java 注解或代码作为元数据格式。

下图显示了 Spring 如何工作,应用程序类与配置元数据相结合,在 ApplicationContext 被创建和初始化之后,就有了一个完全配置且可执行的系统或应用程序。

配置元数据

配置元数据表示作为应用程序开发人员如何告诉 Spring 容器实例化、配置和组装应用程序中的对象。

  • Spring 2.5 引入了对基于注解的配置元数据的支持。
  • 从 Spring 3.0 开始引入了基于 Java 的配置。

Spring 配置包含至少一个 bean 定义。

  • 基于 XML 的配置元数据将这些 bean 配置为顶级 <beans/> 元素内的 <bean/> 元素。
  • Java 配置通常在@Configuration 类中使用@Bean 注释的方法。

这些 bean 定义对应于构成应用程序的实际对象。

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="..." class="..."> 1, 2
<!-- collaborators and configuration for this bean go here -->
</bean>

<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>

<!-- more bean definitions go here -->

</beans>
1 id 属性是一个字符串,用于标识单个 bean 定义。
2 class 属性定义了 bean 的类型并使用了完全限定的类名。

实例化容器

提供给 ApplicationContext 构造函数的一个或多个位置路径是资源字符串,它允许容器从各种外部资源(例如本地文件系统、Java CLASSPATH 等)加载配置元数据。

1
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

以下示例显示了服务层对象 (services.xml) 配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- services -->

<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>

<!-- more bean definitions for services go here -->

</beans>

以下示例显示了数据访问对象 daos.xml 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="accountDao"
class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
<!-- additional collaborators and configuration for this bean go here -->
</bean>

<bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">
<!-- additional collaborators and configuration for this bean go here -->
</bean>

<!-- more bean definitions for data access objects go here -->

</beans>

服务层由 PetStoreServiceImpl 类和 JpaAccountDao 和 JpaItemDao 类型的两个数据访问对象组成。 property name 元素是指 JavaBean 属性的名称,ref 元素是指另一个bean的id或name。 id 和 ref 元素之间的联系表达了协作对象之间的依赖关系。

编写基于XML格式的配置元数据

每个单独的 XML 配置文件都代表一个逻辑层或模块

  • 可以使用上下文构造函数从所有XML 文件加载 bean 定义,此构造函数采用多个 Resource 位置,如上一节所示。
  • 使用一个或多个 <import/> 元素从另一个文件或多个文件加载 bean 定义。

示例:

1
2
3
4
5
6
7
8
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>

<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
</beans>

示例中,外部 bean 定义从三个文件加载:services.xml、messageSource.xml 和 themeSource.xml。所有位置路径都相对于执行导入的定义文件,因此 services.xml 必须与执行导入的文件位于同一目录或类路径位置,而 messageSource.xml 和 themeSource.xml 必须位于resources文件夹下。

容器的使用

ApplicationContext 是维护不同bean的注册以及bean之间的依赖关系的高级工厂接口。通过使用 T getBean(String name, Class requiredType) 方法,可以检索 bean 的实例。

1
2
3
4
5
6
7
8
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);

// use configured instance
List<String> userList = service.getUsernameList();