這是一個由simviso團隊所組織進行的基於Spring Framework 5.2.2版本基礎文檔翻譯。若是想要深刻討論,可掃描下方二維碼,加入官方羣和知秋的知識星球,免費給你們分享相關知識。ios
因爲專業文檔翻譯難度比較大,咱們內部本着翻譯質量,也有一系列的規範,也因這些規範,消耗的時間更多,同時咱們本身時間也是有限的,做爲公益組織,當下也沒什麼收益,都是小夥伴天天晚上熬夜在作事情,請勿催更,望理解。
web
知秋-掘金博客spring
https://juejin.im/user/59c764...express
地球不在轉-掘金博客app
https://juejin.im/user/5aa0b2...框架
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.dom
org.springframework.context.ApplicationContext
接口表示Spring IoC容器,而且負責初始化、配置和組裝bean。Spring容器經過讀取配置元數據來獲取初始化、配置和組裝對象的指令。能夠使用XML文件、Java註解或者Java代碼來配置元數據。以此來讓你在你應用程序中對這些配置bean對象進行組合並獲得豐富的依賴關係。ide
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.ui
Spring提供了 ApplicationContext
的幾種實現。在單體應用中,一般會建立ClassPathXmlApplicationContext
或者 FileSystemXmlApplicationContext
的實例。儘管XML文件是定義配置文件的傳統格式,但你能夠經過使用少許的XML配置聲明來對Java註解或代碼這些額外的元數據格式進行支持。this
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 容器
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>
id
attribute is a string that identifies the individual bean definition. 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>
id
屬性是一個用於標識單獨bean定義的字符串。class
屬性使用全類名的方式定義一個bean的類型。 id
屬性的值用於指向協做對象。用於參考的協做對象的XML未在本例中進行展現。更多信息請參考Dependencies。