登录

  • 登录
  • 忘记密码?点击找回

注册

  • 获取手机验证码 60
  • 注册

找回密码

  • 获取手机验证码60
  • 找回
毕业论文网 > 外文翻译 > 计算机类 > 计算机科学与技术 > 正文

控制反转容器外文翻译资料

 2022-07-21 03:07  

6. The IoC container

6.1 Introduction to the Spring IoC container and beans

This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) [1] principle. IoC is also known asdependency 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.

The org.springframework.beans and org.springframework.context packages are the basis for Spring Frameworkrsquo;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 Springrsquo;s AOP features; message resource handling (for use in internationalization), event publication; and application-layer specific contexts such as theWebApplicationContext for use in web applications.

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 Springrsquo;s IoC container. For more information on using the BeanFactory instead of theApplicationContext, refer to Sectionensp;6.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.

6.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.

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.

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 Sectionensp;6.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.

<stron

全文共33464字,剩余内容已隐藏,支付完成后下载完整资料</stron


  1. 控制反转容器

6.1对控制反转容器的介绍

本章节包括SPRING框架实施的控制反转容器原理。IOC同时也被称作依赖注入,它是对象定义它们的依赖的过程,即,与其工作的其它对象,只能通过构造函数的参数,传递给一个工厂方法,或是一个对象被创建后设置的属性,或是从工厂方法返回的属性。当容器构造了组件时,它会注入这些依赖。这个过程从根本上是逆向的,因而得名控制反转容器。这个容器通过直接创建类或服务定位器模式那样的机制来控制实例化和依赖的位置。

org.springframework.beans 和org.springframework.context两个包是spring框架中的ioc的基本包。BeanFactory接口提供了一个高级的构造机制可以完成任何类型的类。ApplicationContext是BeanFactory的一个子接口。它加入了更简单的Spring AOP的特征,信息资源操作,事件公开,应用层特殊环境诸如WebApplicationContext在网页应用上的使用的整合。简而言之,BeanFactory提供了一个系统框架和基本功能,并且ApplicationContext加入了更多企业具体功能。ApplicationContext 是BeanFactory的一个完整扩展集,在这一章中专门用来对IOC的描述。想获取更多BeanFactory而不是ApplicationContext 的信息,请看6.16:the BeanFactory。

在Spring中,构成你应用的主干并且是被IOC管理的类叫做Beans。 Beans是一个被实例化,组装的类,另一方面,他被IOC管理,另一方面,Beans仅仅是你很多应用类中的其中一个。Beans和它上面的依赖,反映在被容器使用的元数据上。

6.2 容器概述

org.springframework.context.ApplicationContext接口是IOC容器的代表,负责实例化、装配,聚集前面提到的Beans。容器通过读取装配元数据获得将对什么对象进行实例化、配置,组装的指令。配置元数据是可扩展标记语言、JAVA注释、JAVA代码的代表。它能让你表示组成你应用的对象和这些对象之间丰富的相互依赖关系。Spring直接提供几个ApplicationContext接口的实现。在单独的应用中,ClassPathXmlApplicationContext和FileSystemXmlApplicationContext在创建一个实例时是相同的。而XML已定义的配置元数据可以指示容器使用java代码注释或元数据格式提供少量的XML C传统格式设置来启用这些额外的元数据格式的支持。在大多数的应用程序方案中,关于IOC容器,显式用户代码不需要实例化一个或多个实例 。在web应用程序中,在web.xml 文件中一个简单的八行的模板描述符通常就已经足够了。如果你正在使用Spring Tool Suite Eclipse-powered开发环境,此模板配置可以通过点几下鼠标轻松创建。下面的图是关于Spring如何工作的视图。你的应用类是由结构元数据构成的。因此当ApplicationContext被创建和初始化后,你有一个完全配置的可执行文件系统或应用程序。

6.2.1配置元数据

正如前面的图中所展示的,此配置元数据表示你作为应用程序开发人员怎么告诉 Spring 容器实例化、 配置和组装您的应用程序中的对象。

配置元数据传统上提供简单、 直观的 XML 格式,这是本章中的大部分用于传达Spring IoC 容器的关键概念和特点。

有关 Spring 容器的使用其他形式的元数据信息。看下面:

基于注释的配置:Spring 2.5介绍了基于注释的配置元数据。

基于java的配置:从Spring 3.0开始,许多Spring JavaConfig 项目所提供的许多功能成为核心 Spring 框架的一部分,因此你可以定义 bean 外部到应用程序的类通过使用 Java 而不是 XML 文件。要使用这些新的功能,请参阅 @Configuration、 @Bean、 @Import 和 @DependsOn 的注释。

Spring 配置包括至少一个且通常超过一个容器必须管理的 bean 定义。基于 XML 的配置元数据显示这些 bean 配置为lt;beangt;lt;/beangt;内顶级元素lt;beansgt;lt;/beansgt;元。Java 配置通常使用 @Configuration 类中注明的 @Bean 方法。

这些 bean 定义对应于实际的构成你实际应用的对象,通常您需要定义服务层对象,数据访问对象 (Dao),如 Struts 操作实例,演示文稿对象基础设施物件如 HibernateSessionFactories、 JMS 队列,等等。通常一个不配置细粒度域对象在容器中,因为它通常是 Dao 和业务逻辑,以创建和加载域对象的责任。然而,你可以使用 Spring 的集成 AspectJ 配置以外的 IoC 容器控件已创建的对象。

下面的示例演示基于 XML 的配置元数据的基本结构。Id 属性是一个字符串,用于标识每个 bean 定义。Class 属性定义 bean 的类型,并使用完全限定的类名。Id 属性的值是指合作对象。谈到合作对象的 XML 不是此示例中所示。

6.2.2实例化一个容器

实例化一个Spring IoC 容器是直截了当的。位置路径或路径提供给更构造函数是实际上允许容器加载配置元数据从外部资源,如本地文件系统,从 Java 类路径中,各种等等的资源字符串。

下面的示例演示服务层对象 (services.xml) 配置文件。

在前面的示例中,服务层包含的类 PetStoreServiceImpl,和两个数据访问对象的 typeJpaAccountDao 和 JpaItemDao (基于 JPA 对象/关系映射标准)。属性名称元素指的 JavaBean 属性,名称和 ref 元素指的是另一个 bean 定义的名称。这种联系 idand ref 元素之间表示合作对象之间的依赖关系。配置对象的依赖关系的详细信息。

基于XML的配置元数据

它可用于有跨多个 XML 文件的 bean 定义。通常每个单个的 XML 配置文件表示逻辑层或在您的体系结构的模块。您可以使用应用程序上下文构造函数从所有这些 XML 片段加载 bean 定义。此构造函数采用资源的多个位置,是上一节所示。或者,使用一个或多个匹配项的lt;importgt;lt;/importgt;元素,从另一个文件或文件加载 bean 定义。

在前面的示例中,从三个文件加载外部 bean 定义 ︰ services.xml、 messageSource.xml、 andthemeSource.xml。所有位置路径都是相对于做导入,因此 services.xml 必须在相同的目录或文件执行导入,而 messageSource.xml 和 themeSource.xml 必须在 aresources 位置下面的导入文件的位置的类路径位置定义文件。正如你所看到的前导斜杠是忽略,但鉴于这些路径都是相对的它是更好的形式,不能在所有使用斜线。正在导入,包括顶级的文件的内容lt;beansgt;lt;/beansgt;元素,必须根据弹簧架构的有效 XML bean 定义。

6.2.3使用容器

ApplicationContext的能保持一个注册表不同Beans及其依赖先进的工厂接口。使用方法getBean(String name,类lt;Tgt;几种获得bean的方法)可以检索您的bean实例。

ApplicationContext可以读取和访问bean定义如下:

检索beans,但理想的情况是您的应用程序代码应该永远不会使用它们。事实上,应用程序代码应该根本没有调用 thegetBean() 方法,并因此没有依赖项spring api。例如,spring的与 web 框架集成提供各种 web 框架类如控制器和 JSF 托管 bean 的依赖注入。

6.3 Beans概况

spring IoC 容器来管理一个或多个 bean。这些 bean 创建与配置元数据,例如,在 XML 的形式供应容器,lt;beangt;lt;/beangt;定义。在容器本身,这些 bean 定义表示为 BeanDefinition 对象,(以及其他信息) 包含下列元数据:

*一个包限定的类名称 ︰ 通常被定义 bean 的实际的实现类。

* Bean 行为配置元素状态 bean 应该如何表现在容器 (范围、 生命周期回调函数,等等)。

*引用到其他 bean 所需的 bean 来做的工作;这些引用也称为合作者。

*其他配置设置,以设置在新创建的对象,例如,要在管理着一个连接池,一个 bean 中使用的连接数或池的大小限制。

此元数据转化为一组弥补每个 bean 定义的属性。除了包含有关如何创建特定的 bean 的 bean 定义,更实现还允许在容器外由用户创建的现有对象的注册。这是通过访问更 BeanFactory 通过方法 getBeanFactory() 返回 BeanFactory implementationDefaultListableBeanFactory。DefaultListableBeanFactory 支持此登记通过 methodsregisterSingleton(..) 和 registerBeanDefinition(..)。

然而,典型的应用程序工作仅与定义通过元数据 bean 定义的 bean。

6.3.1命名beans

每个 bean 中有一个或多个标识符。这些标识符必须是唯一的承载该 bean 的容器内。Bean 通常具有唯一的标识符,但如果它需要不止一个,可以算是额外的别名。在基于 XML 的配置元数据中,你使用的 id 和/或名称的属性指定 bean 标识符。Id 属性允许您指定一个 id。通常这些名字都是字母数字 (#39;myBean#39;、 #39;fooService#39; 等),但可能包含特殊字符以及。如果你想要到 bean 介绍其他别名,您还可以指定他们在由逗号 (,)、 分号 (;) 或空格分隔的名称属性。作为一个历史的说明,在春天 3.1 以前的版本中的 id 属性被定义为一种 xsd:ID 类型,限制可能的字符。从 3.1 开始,它被定义为 xsd: string 类型。注意,beanid 仍然强制唯一性由容器,虽然不再是通过 XML 解析器。你不需要提供一个名称或ID的一个beans。如果没有提供名称或标识,容器将为该bean生成唯一的名称。然而,如果你想通过名称来引用该bean,通过ref元素或服务定位方式查找使用,您必须提供一个名称。不提供名字的动机是使用内bean里的合作者关系。

Beans命名协定

约定是使用标准java公约例如字段名称命名时,bean。那就是,bean名称以小写字母开头。这样的例子会(没有引号)#39;accountmanager #39;、#39; AccountService #39;,#39; UserDAO #39;,”logincontroller #39;,等等。

命名bean一直让你的配置更容易阅读和理解,如果你使用的是Spring AOP的应用有很大帮助.

在 bean 定义本身中,您可以通过使用达人名称由 id 属性和任意数量的其他名称,在名称属性中指定的组合提供的 bean。这些名称可以等效别名到同一 bean,并且可用于某些情况下,例如在应用程序中,通过使用特定于该组件本身的 bean 名称常见的相关性是指允许每个组件。指定该 bean 实际上定义的所有别名并不总是充分,然而。它有时是可取的其他地方定义 bean 的别名。通常,这是案件在大型系统中在哪里配置分散在每个子系统,每个子系统有其自己的对象定义集。在基于 XML 的配置元数据,您可以使用lt;aliasgt;lt;/aliasgt;元素,做到这一点。在这种情况下,一个bean,相同的容器是命名为 fromName,可能也后使用此别名定义,被称为多元逐步回归分析。

例如,子系统 A 的配置元数据可能指通过名称 subsystemA 数据源的数据源。B 子系统的配置元数据可能指通过名称 subsystemB 数据源的数据源。撰写的主要的应用程序使用两个这些子系统时主应用程序是指通过名称 myApp 数据源的数据源。若要有指向相同的对象的所有三个名字你添加 MyApp 配置元数据以下的别名定义:

现在每个组件和主应用程序但它们引用同一 bean 可以引用通过是独一无二的保证不与任何其他定义 (有效地创建一个命名空间),发生冲突的名称的数据源。

实例化beans

一个bean定义基本上是一个创造一个或多个对象的配方。容器在被询问时看了一个名为“beans”的配方,并使用该“定义”(或获得)一个实际对象封装的配置元数据。

如果你使用基于XML的配置元数据,您指定的类型(或类)的对象是在lt;beangt;元/ classattribute实例化。这类属性,其内部是一个BeanDefinition实例的类属性,通常是强制性的。(例外情况,参见“实例化使用实例工厂方法”部分ensp;6.7、“bean定义继承”。)你的方式有两种使用类属性:

*通常情况下,若要指定 bean 类要建造在哪里容器本身直接创建 bean 通过沉思,调用其构造函数的情况有点等同于 Java 代码使用 new 运算符.

*若要指定

全文共10775字,剩余内容已隐藏,支付完成后下载完整资料


资料编号:[154929],资料为PDF文档或Word文档,PDF文档可免费转换为Word

您需要先支付 30元 才能查看全部内容!立即支付

企业微信

Copyright © 2010-2022 毕业论文网 站点地图