1.2. Container overview
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.
org.springframework.context.ApplicationContext
代表了spring IOC容器并且通过配置元数据来对bean进行实例化、装配等。这些配置元数据可以来自XML配置文件、java注解、或者直接用java代码定义。
Several implementations of the ApplicationContext interface are supplied out-of-the-box with Spring. In standalone 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接口的实现,这些实现类可以在spring框架之外创建对象。在一个单独的应用程序中,可以使用ClassPathXmlApplicationContext
或 FileSystemXmlApplicationContext
实现类并根据xml配置文件或java注解或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 will typically suffice (see 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.
通常情况下,我们是不需要用代码来手动的创建spring IOC容器,例如我们在开发web项目时,只需要在web.xml中对spring进行简单的配置即可
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.
下图是spring如何工作的概览,简单来说,你应用中的类对象结合相关的配置元数据,在spring IOC 容器即ApplicationContext对象被创建并被实例化之后并可以拥有一个可执行的系统或应用。
Figure 1. The Spring IoC container