特别说明

这是一个由simviso团队所组织进行的基于Spring Framework 5.2.2版本基础文档翻译。如果想要深入讨论,可扫描下方二维码,加入官方群和知秋的知识星球,免费给大家分享相关知识。

由于专业文档翻译难度比较大,我们内部本着翻译质量,也有一系列的规范,也因这些规范,消耗的时间更多,同时我们自己时间也是有限的,作为公益组织,当下也没什么收益,都是小伙伴每天晚上熬夜在做事情,请勿催更,望理解。

本节参与人员

知秋-掘金博客

https://juejin.im/user/59c764...

地球不在转-掘金博客

https://juejin.im/user/5aa0b2...

image.png

容器概述(Container Overview)

The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It lets you express the objects that compose your application and the rich interdependencies between those objects.

org.springframework.context.ApplicationContext 接口表示Spring IoC容器,并且负责初始化、配置和组装bean。Spring容器通过读取配置元数据来获取初始化、配置和组装对象的指令。可以使用XML文件、Java注解或者Java代码来配置元数据。以此来让你在你应用程序中对这些配置bean对象进行组合并得到丰富的依赖关系。

Several implementations of the ApplicationContext interface are supplied with Spring. In stand-alone applications, it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext. While XML has been the traditional format for defining configuration metadata, you can instruct the container to use Java annotations or code as the metadata format by providing a small amount of XML configuration to declaratively enable support for these additional metadata formats.

Spring提供了 ApplicationContext 的几种实现。在单体应用中,通常会创建ClassPathXmlApplicationContext或者 FileSystemXmlApplicationContext 的实例。尽管XML文件是定义配置文件的传统格式,但你可以通过使用少量的XML配置声明来对Java注解或代码这些额外的元数据格式进行支持。

In most application scenarios, explicit user code is not required to instantiate one or more instances of a Spring IoC container. For example, in a web application scenario, a simple eight (or so) lines of boilerplate web descriptor XML in the web.xml file of the application typically suffices (see context-create). If you use the [Spring Tool Suite] (an Eclipse-powered development environment), you can easily create this boilerplate configuration with a few mouse clicks or keystrokes.

在大多数应用场景中,用户不需要显式的通过代码初始化一个或者多个Spring IoC容器。例如,在web应用程序场景中,它的 web.xml 文件中一个简单的8行样板web描述XML通常就足够了(详情请见 context-create )。如果你使用 [Spring Tool Suite] (Eclipse驱动的开发环境),那么你只需要点击几次鼠标就可以创建这种样板配置文件了。

The following diagram shows a high-level view of how Spring works. Your application classes are combined with configuration metadata so that, after the ApplicationContext is created and initialized, you have a fully configured and executable system or application.

下图高度概括展示了Spring是如何工作的。你的应用程序中的类通过配置元数据从而整合到一起,这样,在 ApplicationContext 创建并初始化之后,你就会有一个配置好的可执行的系统或应用程序。

Figure 1.  Spring IoC 容器

配置元数据(Configuration Metadata)

As the preceding diagram shows, the Spring IoC container consumes a form of configuration metadata. This configuration metadata represents how you, as an application developer, tell the Spring container to instantiate, configure, and assemble the objects in your application.

如上图所示,是一种表示Spring IoC容器将配置元数据进行消费使用的形式。作为应用程序开发者的你,通过配置元数据来告诉Spring容器,在你的应用程序中应该如何去实例化、配置和组装对象。

XML-based metadata is not the only allowed form of configuration metadata. The Spring IoC container itself is totally decoupled from the format in which this configuration metadata is actually written. These days, many developers choose Java-based configuration for their Spring applications.

基于XML的元数据不是配置元数据的唯一格式。Spring IoC容器本身与使用XML配置元数据的格式完全分离。如今,许多开发者在他们的应用程序中选择基于Java 代码的形式 Java-based configuration 来进行配置。

For information about using other forms of metadata with the Spring container, see: Annotation-based configuration: Spring 2.5 introduced support for annotation-based configuration metadata. Java-based configuration: Starting with Spring 3.0, many features provided by the Spring JavaConfig project became part of the core Spring Framework. Thus, you can define beans external to your application classes by using Java rather than XML files. To use these new features, see the    @Configuration@Bean@Import,  and @DependsOn annotations.

对于在Spring容器中使用其他方式配置元数据的相关信息,请查阅:* Annotation-based configuration: Spring 2.5中引入了基于注解的配置元数据的支持。* Java-based configuration: 从Spring 3.0开始,由Java代码来配置的Spring项目提供的许多特性已经成为Spring框架核心的一部分。因此,你可以通过使用Java代码替代XML文件来在你的应用程序类外部定义bean。想要使用这些新特性,请查看 @Configuration@Bean@Import, and @DependsOn

Spring configuration consists of at least one and typically more than one bean definition that the container must manage. XML-based configuration metadata configures these beans as <bean/> elements inside a top-level <beans/> element. Java configuration typically uses @Bean-annotated methods within a @Configuration class.

Spring配置由容器管理的至少一个、通常是多个的bean定义组成。基于XML的配置元数据会将这些bean配置在 <beans/> 这个最顶级元素内部的 <bean/> 元素中。Java代码配置通常是在使用`@Configuration` 注解的类中使用 @Bean 注解方式来对bean进行配置。

These bean definitions correspond to the actual objects that make up your application. Typically, you define service layer objects, data access objects (DAOs), presentation objects such as Struts Action instances, infrastructure objects such as Hibernate SessionFactories, JMS Queues, and so forth. Typically, one does not configure fine-grained domain objects in the container, because it is usually the responsibility of DAOs and business logic to create and load domain objects. However, you can use Spring’s integration with AspectJ to configure objects that have been created outside the control of an IoC container. See Using AspectJ to dependency-inject domain objects with Spring

这些bean定义对应于实际组成应用程序的对象。通常,你定义服务层对象、数据访问层对象(DAO)、表现层对象如Struts的 Action 实例、基础结构对象如Hibernate的 SessionFactories 和JMS的 Queues 等等。通常不会在容器中配置细粒度的域对象,因为这一般是由DAOs和业务逻辑来创建和加载域对象的。然而,你可以使用Spring集成的AspectJ来对已创建的外部对象(知秋注:未在配置元数据中定义的bean)进行IoC容器控制配置。详情请看 Using AspectJ to dependency-inject domain objects with Spring。

The following example shows the basic structure of XML-based configuration metadata:

<?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="...">        <!-- 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. The id attribute is a string that identifies the individual bean definition.
  2. The class attribute defines the type of the bean and uses the fully qualified classname.

The value of the id attribute refers to collaborating objects. The XML for referring to collaborating objects is not shown in this example. See Dependencies for more information.

下例展示了基于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="...">        <!-- 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。


知秋o
20 声望14 粉丝

《Java编程方法论:响应式RxJava与代码设计实战》 作者,simviso开源分享团队核心,专注于源码解读与基础库编写