1. 程式人生 > 實用技巧 >1.2. Container Overview

1.2. Container Overview

Theorg.springframework.context.ApplicationContextinterface 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.

介面ApplicationContext表示spring容器,並負責例項化、配置、組裝beans。容器通過讀取配置元資料來獲取那些需要例項化、配置、組裝的物件資訊。配置元資料可以使用xml、java註解,java程式碼表示。

Several implementations of theApplicationContextinterface are supplied with Spring. In stand-alone applications, it is common to create an instance ofClassPathXmlApplicationContextorFileSystemXmlApplicationContext

. 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 theweb.xmlfile of the application typically suffices (seeConvenient ApplicationContext Instantiation for Web Applications). If you use theSpring Tools for Eclipse(an Eclipse-powered development environment), you can easily create this boilerplate configuration with a few mouse clicks or keystrokes.

在大多數應用場景中,不需要顯示的程式碼來例項化spring容器。例如在web應用中,web.xml檔案中簡單的8行左右描述資訊就足夠了

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

下圖是spring工作的原理圖,類和配置資料組合在一起,當ApplicationContext建立和例項化後,你就擁有一個可執行的應用及其完整的配置

Figure 1. The Spring IoC container