Introduction to the Spring IoC container and beans

3 The IoC container(IOC容器)

3.1 Introduction to the Spring IoC container and beans(Spring IOC容器与beans的介绍)

This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) [1] principle. IoC is also known as dependency injection (DI). It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the Service Locator pattern.


本章主要介绍了Spring Framework 的IOC 容器实现原理,IOC容器也被成为依赖注入,这是个对象定义自己依赖的过程,也就是需要依赖其他对象的才能正常工作。通过构造参数,工厂方法的参数,或者使用set 到对象上的参数,从工厂方法构造出的。容易在创建bean时注入这些依赖,这个过程时反响的,因此命名为反向控制。 直接使用类的构造方法或者服务定位模式来实现bean本身实例化或者定位的


The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container. The BeanFactory interface provides an advanced configuration mechanism capable of managing any type of object. ApplicationContext is a sub-interface of BeanFactory. It adds easier integration with Spring’s AOP features; message resource handling (for use in internationalization), event publication; and application-layer specific contexts such as the WebApplicationContext for use in web applications.


org.springframework.beansorg.springframework.context 是springframework IOC 容易的基础,BeanFactory 接口提供了一个高级配置机制来管理所有类型的对象。ApplicationContext 是BeanFactory 的子接口,添加了以下简单的AOP功能集成,消息资源处理(用于国际化),事件发布,应用层特定的上下文,比如web程序中使用的WebApplicationContext 。


In short, the BeanFactory provides the configuration framework and basic functionality, and the ApplicationContext adds more enterprise-specific functionality. The ApplicationContext is a complete superset of the BeanFactory, and is used exclusively in this chapter in descriptions of Spring’s IoC container. For more information on using the BeanFactory instead of the ApplicationContext, refer to Section 3.16, “The BeanFactory”.


简而言之,BeanFactory提供了配置框架以及其基本功能。ApplicationContext 提供了更多的企业特定的功能。ApplicationContext是BeanFactory的一个完整的超集,本章节中专门对I哦擦容器进行说明,想要了解关于BeanFactory的信息请查看3.16 “The BeanFactory”


In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.


Spring中 构成应用程序的 主干被称为beans 并有IOC 容器进行管理。 Bean 是由IOC 容易实例化,组装以及操作管理对象。其他方面,一个bean就是你应用众多object中的一个。Beans和他们之间的依赖关系映射在容器使用的配置元信息进行反射。

3.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容器,并且负责实例化 配置以及组装上问中说到的beans 容器通过读取配置数据来知道那些对象要进行实例化配置以及组装,配置元数据使用XML 文件,JAVA 注解或者 JAVA 代码来实现。用来说明组装你应用的各个object,以及object之间的关系。


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 接口的几个具体实现,独立应用中通常创建ClassPathXmlApplicationContext 或者FileSystemXmlApplicationContext ,虽然习惯用XML 定义配置数据,你可以使用使用少量的XML 配置来支持 通过AVA 注解或者 JAVA 代码作为元数据来通知IOC 容器,


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.


多数应用场景中,不需要显示的实例化Spring IOC 容器, 举例例子,在WEB 开发过程中,在WEB.xml中添加8行左右的配置文件就够了。(参考第3.15.4 中“Convenient ApplicationContext instantiation for web applications”)如果你是用Eclipse 中 Spring Tool 这种强大的开发环境,这种配置样本可以通过点几下数据或者键盘来完成。


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 是怎么工作的,你的应用类和配置元数据结合在一起,以便于在ApplicationContext创建初始化后,你有一个完整的可执行的系统或者应用程序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值