Container Overview

org.springframework.context.ApplicationContext 接口表示 Spring IoC 容器,负责实例化、配置和装配 bean。容器通过读取配置元数据来获取有关要实例化、配置和装配的组件的说明。配置元数据可以表示为带注释的组件类、带工厂方法的配置类或外部 XML 文件或 Groovy 脚本。使用任一格式,您都可以编制您的应用程序以及那些组件之间丰富的相互依赖关系。 ApplicationContext 接口的几个实现是 core Spring 的一部分。在独立应用程序中,通常创建https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/AnnotationConfigApplicationContext.html[AnnotationConfigApplicationContext]或https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/support/ClassPathXmlApplicationContext.html[ClassPathXmlApplicationContext]的实例。 在大多数应用程序场景中,不需要显式用户代码来实例化 Spring IoC 容器的一个或多个实例。例如,在普通的 Web 应用程序场景中,应用程序的 web.xml 文件中的简单样板 Web 描述符 XML 就足够了(见 Convenient ApplicationContext Instantiation for Web Applications)。在 Spring Boot 场景中,应用程序上下文将根据常见的设置约定隐式为你引导。 下图显示了 Spring 的工作方式的高级视图。您的应用程序类与配置元数据相结合,以便在创建并初始化 ApplicationContext 之后,您就会拥有一个完全配置且可执行的系统或应用程序。 .The Spring IoC container image::container-magic.png[]

Configuration Metadata

如上图所示,Spring IoC 容器使用配置元数据的某个格式。作为应用程序开发人员,您可以使用此配置元数据告知 Spring 容器如何在您的应用程序中实例化、配置和装配组件。

Spring IoC 容器本身与该配置元数据实际编写的格式完全分离。如今,许多开发人员为他们的 Spring 应用程序选择Java-based configuration

  • Annotation-based configuration:在应用程序的组件类上使用基于注释的配置元数据来定义 bean。

  • Java-based configuration:通过使用基于 Java 的配置类来定义应用程序类外部的 bean。要使用这些功能,请参阅 @Configuration,https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html[@Bean],https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Import.html[@Import] 和 @DependsOn 注释。

Spring 配置至少包含一个(通常多个)容器必须管理的 bean 定义。Java 配置通常在 @Configuration 类中使用带 @Bean 注释的方法,每个方法对应一个 bean 定义。

这些 bean 定义对应于构成您的应用程序的实际对象。通常,您定义服务层对象、诸如存储库或数据访问对象 (DAO) 的持久层对象、诸如 Web 控制器之类的表示对象、诸如 JPA EntityManagerFactory 的基础架构对象、JMS 队列等。通常,人们不会在容器中配置细粒度的域对象,因为创建和加载域对象通常是存储库和业务逻辑的职责。

XML as an External Configuration DSL

基于 XML 的配置元数据将这些 bean 配置为顶级 <beans/> 元素内的 <bean/> 元素。以下示例显示基于 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"
	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 的类型,并使用完全限定类名。

`id`属性的值可用于引用协作对象。此示例中未显示引用协作对象的 XML。有关更多信息,请参阅Dependencies

要实例化容器,需要将 XML 资源文件的位置路径或路径提供给 ClassPathXmlApplicationContext 构造器,该构造器允许容器从各种外部资源(例如本地文件系统、Java CLASSPATH 等)加载配置元数据。

  • Java

  • Kotlin

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

在你了解了 Spring 的 IoC 容器之后,你可能希望更多地了解 Spring 的 Resource 抽象(如 Resources 所述),它提供了一种方便的机制来从在 URI 语法中定义的位置读取 InputStream。特别是,Resource 路径用于构造应用程序上下文,如 Application Contexts and Resource Paths 所述。

以下示例显示服务层对象 (services.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"
	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 文件:

<?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 类和两个数据访问对象(基于 JPA 对象关系映射标准)的类型为 JpaAccountDaoJpaItemDao 构成。property name 元素引用 JavaBean 属性的名称,而 ref 元素引用另一个 bean 定义的名称。idref 元素之间的这种关联表示协作对象之间的依赖项。有关配置对象的依赖项的详细信息,请参阅Dependencies

Composing XML-based Configuration Metadata

将 Bean 定义跨越多个 XML 文件可能非常有用。通常,每个单独的 XML 配置文件表示架构中的一个逻辑层或模块。

你可以使用 ClassPathXmlApplicationContext 构造函数从 XML 片段加载 Bean 定义。此构造函数采用多个 Resource 位置,如 previous section 中所示。或者,使用一个或多个 <import/> 元素的出现来从其他文件或文件加载 Bean 定义。以下示例显示了如何执行此操作:

<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.xmlmessageSource.xmlthemeSource.xml。所有位置路径都与执行导入的定义文件相关,因此,services.xml 必须与执行导入的文件位于同一个目录或类路径位置,而 messageSource.xmlthemeSource.xml 则必须位于导入文件位置之下的 resources 位置。如你所见,会忽略前导斜杠。然而,鉴于这些路径是相对路径,所以最好根本不使用斜杠。根据 Spring Schema,要导入的文件的内容(包括顶层 <beans/> 元素)必须是有效的 XML Bean 定义。

可以使用相对 ../ 路径引用父目录中的文件,但这是不推荐的。这样做相当于创建对当前应用程序外部文件的依赖关系。特别是,不推荐对 classpath: URL(例如,classpath:../services.xml)使用此引用,在这里,运行时解析进程会选择“最近的” classpath 根,然后查找其父目录。Classpath 配置的更改可能会导致选择不同的不正确目录。 你始终可以使用完全限定资源位置代替相对路径:例如,file:C:/config/services.xmlclasspath:/config/services.xml。不过,需要注意的是,这相当于将应用程序的配置与特定的绝对位置耦合。通常,通过针对运行时 JVM 系统属性解析的 ${…​} 占位符,最 好对这些绝对位置保持间接引用。

命名空间本身提供 import 指令功能。除了纯 Bean 定义之外,Spring 提供的选择 XML 命名空间中还有一些超出范围的配置功能,例如 contextutil 命名空间。

The Groovy Bean Definition DSL

作为外部化配置元数据的另一个示例,Bean 定义也可以在 Spring 的 Groovy Bean Definition DSL 中表达,如 Grails 框架中所知。通常,此类配置保存在 “.groovy” 文件中,其结构如以下示例所示:

beans {
	dataSource(BasicDataSource) {
		driverClassName = "org.hsqldb.jdbcDriver"
		url = "jdbc:hsqldb:mem:grailsDB"
		username = "sa"
		password = ""
		settings = [mynew:"setting"]
	}
	sessionFactory(SessionFactory) {
		dataSource = dataSource
	}
	myService(MyService) {
		nestedBean = { AnotherBean bean ->
			dataSource = dataSource
		}
	}
}

此配置样式基本上等同于 XML Bean 定义,甚至支持 Spring 的 XML 配置命名空间。它还允许通过 importBeans 指令导入 XML Bean 定义文件。

Using the Container

ApplicationContext 是一个接口,用于具备高级工厂,该工厂能够维护不同 Bean 及其依赖关系的注册表。通过使用 T getBean(String name, Class<T> requiredType) 方法,你可以检索你的 Bean 实例。

ApplicationContext 让你能够读取 Bean 定义并访问它们,如下所示:

  • Java

  • Kotlin

// 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();
   import org.springframework.beans.factory.getBean

// create and configure beans
   val context = ClassPathXmlApplicationContext("services.xml", "daos.xml")

   // retrieve configured instance
   val service = context.getBean<PetStoreService>("petStore")

   // use configured instance
   var userList = service.getUsernameList()

使用 Groovy 配置,引导看起来非常相似。它有一个不同的上下文实现类,该类对 Groovy 有感知(但也理解 XML Bean 定义)。以下示例展示了 Groovy 配置:

  • Java

  • Kotlin

ApplicationContext context = new GenericGroovyApplicationContext("services.groovy", "daos.groovy");
val context = GenericGroovyApplicationContext("services.groovy", "daos.groovy")

最灵活的变体是 GenericApplicationContext,与 Reader 委托一起使用,例如,对于 XML 文件,则与 XmlBeanDefinitionReader 一起使用,如下所示:

  • Java

  • Kotlin

GenericApplicationContext context = new GenericApplicationContext();
new XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml");
context.refresh();
val context = GenericApplicationContext()
XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml")
context.refresh()

你也可以对 Groovy 文件使用 GroovyBeanDefinitionReader,如下所示:

  • Java

  • Kotlin

GenericApplicationContext context = new GenericApplicationContext();
new GroovyBeanDefinitionReader(context).loadBeanDefinitions("services.groovy", "daos.groovy");
context.refresh();
val context = GenericApplicationContext()
GroovyBeanDefinitionReader(context).loadBeanDefinitions("services.groovy", "daos.groovy")
context.refresh()

你可以在相同的 ApplicationContext 上混合并匹配此类 Reader 委托,从不同的配置源读取 Bean 定义。

然后,你可以使用 getBean 来检索 Bean 实例。ApplicationContext 接口有一些用于检索 Bean 的其他方法,但理想情况下,你的应用程序代码永远不应该使用它们。事实上,你的应用程序代码根本不应调用 getBean() 方法,从而根本不依赖 Spring API。例如,Spring 与 Web 框架的集成针对各种 Web 框架组件(如控制器和 JSF 托管 Bean)提供依赖注入,允许你通过元数据(如自动连接注解)声明对特定 Bean 的依赖关系。