Skip to content

Latest commit

 

History

History
23 lines (11 loc) · 3.91 KB

32-容器概述.md

File metadata and controls

23 lines (11 loc) · 3.91 KB

3.2 容器概述

The interface org.springframework.context.ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned 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 allows you to express the objects that compose your application and the rich interdependencies between such objects.

Several implementations of the ApplicationContext interface are supplied out-of-the-box with Spring. In standalone applications it is common to create an instance ofClassPathXmlApplicationContext 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.

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 will typically suffice (see Section 3.15.4, “Convenient ApplicationContext instantiation for web applications”). If you are using the Spring Tool Suite Eclipse-powered development environment this boilerplate configuration can be easily created with few mouse clicks or keystrokes.

The following diagram is 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.

接口org.springframework.context.ApplicationContext表示Spring IoC容器,并负责实例化,配置和组合上述bean。容器通过读取配置元数据获取关于要实例化,配置和组合的对象的指令。配置元数据以XMLJava注释Java代码表示。它允许你表达组成你的应用程序的对象和这些对象之间丰富的相互依赖。

ApplicationContext接口的几个实现是Spring提供的开箱即用的。在独立应用程序中,通常创建一个ClassPathXmlApplicationContextFileSystemXmlApplicationContext的实例。尽管XML是定义配置元数据的传统格式,但您可以通过提供少量的XML配置来声明性地支持这些额外的元数据格式,从而指示容器使用Java注释代码作为元数据格式。

在大多数应用场景中,不需要显式用户代码来实例化Spring IoC容器的一个或多个实例。例如,在Web应用程序场景中,应用程序的web.xml文件中的一个简单的样板网站的 XML 描述符通常就足够了 (请参见第3.15.4节“便捷的 ApplicationContext 实例化 Web 应用程序 ”) 。如果您使用的是Eclipse工具套件Eclipse开发环境,那么只需点击鼠标或敲击即可轻松创建此样板配置。

下图是Spring如何工作的高级视图。您的应用程序类与配置元数据相结合,以便在创建和初始化ApplicationContext之后,您具有完全配置和可执行的系统或应用程序。

Figure 3.1.Spring IoC容器

container magic