1. IoC容器 1.1.Spring IoC容器和bean介绍外文翻译资料

 2022-03-30 09:03

1. The IoC container

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

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 the WebApplicationContext 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 the ApplicationContext, refer to 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.

1.2. Container overview

Theinterface 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 Convenient ApplicationContext instantiation for web applications). If you are using theSpring 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.

Figure 1. The Spring IoC container

1.2.1. Configuration metadata

As the preceding diagram shows, the Spring IoC container consumes a form of configuration metadata; this configuration metadata represents how you as an application developer tell the Spring container to instantiate, configure, and assemble the objects in your application.

Configuration metadata is traditionally supplied in a simple and intuitive XML format, which is what most of this chapter uses to convey key concepts and features of the Spring IoC container.

XML-based metadata is not the only allowed form of configuration metadata. The Spring IoC container itself is totally decoupled from the format in which this conf

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


1. IoC容器

1.1.Spring IoC容器和bean介绍

本章涵盖了控制反转(IoC)原理的Spring框架实现。IoC也被称为依赖注入(DI)。这是一个过程,对象通过构造函数参数,工厂方法的参数或在工厂方法构造或返回的对象实例上设置的属性来定义它们的依赖关系,即它们所处理的其他对象。然后容器 在创建bean时注入这些依赖关系。这个过程基本上是相反的,因此名为控制反转(IoC),通过使用类的直接构造或诸如Service Locator模式的机制来控制其依赖性的实例化或位置的bean本身。

在org.springframework.beans和org.springframework.context包是Spring框架的IoC容器的基础。该 BeanFactory 接口提供了一种能够管理任何类型的对象的高级配置机制。 ApplicationContext 是一个子接口BeanFactory。它增加了与Spring的AOP功能更容易的集成; 消息资源处理(用于国际化),事件发布; 以及应用程序层特定的上下文,例如WebApplicationContext 在Web应用程序中使用。

总之,BeanFactory提供了配置框架和基本功能,并ApplicationContext增加了更多的企业特定功能。这ApplicationContext是一个完整的超集,BeanFactory在本章中专门用于描述Spring的IoC容器。有关使用BeanFactory而不是ApplicationContext,引用BeanFactory的更多信息。

在Spring中,构成应用程序的骨干并由Spring IoC 容器管理的对象称为bean。bean是一个实例化,组装并由Spring IoC容器管理的对象。否则,bean只是应用程序中众多对象中的一个。Bean和 它们之间的依赖关系反映在容器使用的配置元数据中。

1.2.容器概述

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

ApplicationContextSpring的几个实现接口是开箱即用的。在独立应用程序中,通常会创建一个ClassPathXmlApplicationContext or 的实例FileSystemXmlApplicationContext。虽然XML是用于定义配置元数据的传统格式,但您可以通过提供少量的XML配置来指示容器使用Java注释或代码作为元数据格式,以声明方式支持这些额外的元数据格式。

在大多数应用程序场景中,显式用户代码不需要实例化Spring IoC容器的一个或多个实例。例如,在Web应用程序场景中,应用程序文件中的简单八行(或多行)样板Web描述符XML web.xml通常就足够了(请参阅Web应用程序的便捷ApplicationContext实例化)。如果您使用 Spring Tool Suite Eclipse开发环境,则只需点击几下鼠标或按键即可轻松创建样板配置。

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

图1. Spring IoC容器

1.2.1.配置元数据

如上图所示,Spring IoC容器使用一种形式的 配置元数据 ; 这个配置元数据表示作为应用程序开发人员如何告诉Spring容器在应用程序中实例化,配置和组装对象。

传统上,配置元数据是以一种简单直观的XML格式提供的,这是本章的大部分内容用来传达Spring IoC容器的关键概念和特性。

注意:基于XML的元数据不是唯一允许的配置元数据形式。Spring IoC容器本身与配置元数据实际写入的格式完全分离。现在许多开发人员为他们的Spring应用程序选择 基于Java的配置

有关在Spring容器中使用其他形式的元数据的信息,请参阅:

基于注释的配置:Spring 2.5引入了对基于注释的配置元数据的支持。

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

Spring配置由容器必须管理的至少一个,通常是多个bean定义组成。基于XML的配置元数据将这些bean配置为lt;bean/gt;顶层元素内的lt;beans/gt;元素。Java配置通常使用类中的@Bean注释方法@Configuration。

这些bean定义对应于组成应用程序的实际对象。通常,您可以定义服务层对象,数据访问对象(DAO),Struts Action实例等表示对象,Hibernate SessionFactories,JMS 等基础结构对象 Queues。通常,不会在容器中配置细粒度的域对象,因为创建和加载域对象通常是DAO和业务逻辑的责任。但是,您可以使用Spring与AspectJ的集成来配置在IoC容器控制之外创建的对象。请参阅使用AspectJ以依赖注入域对象与Spring

以下示例显示了基于XML的配置元数据的基本结构:

lt;?xml version='1.0' encoding='UTF-8'?gt;lt;beans xmlns='http://www.springframework.org/schema/beans'

xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'

xsi:schemaLocation='http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd'gt;

lt;bean id='...' class='...'gt;

lt;!-- collaborators and configuration for this bean go here --gt;

lt;/beangt;

lt;bean id='...' class='...'gt;

lt;!-- collaborators and configuration for this bean go here --gt;

lt;/beangt;

lt;!-- more bean definitions go here --gt;lt;/beansgt;

该id属性是一个字符串,用于标识各个bean的定义。该class属性定义了bean的类型并使用完全限定的类名。id属性的值是指协作对象。本示例中未显示用于引用协作对象的XML; 请参阅 依赖关系以获取更多信息。

1.2.2.实例化一个容器

实例化一个Spring IoC容器很简单。提供给ApplicationContext构造函数的位置路径实际上是资源字符串,它允许容器从各种外部资源(如本地文件系统,Java等)加载配置元数据CLASSPATH。

ApplicationContext context = new ClassPathXmlApplicationContext('services.xml', 'daos.xml');

注意:在了解了Spring的IoC容器之后,您可能想了解更多关于Spring的 Resource抽象的知识,如参考资料中所述,它提供了一种从URI语法中定义的位置读取InputStream的方便机制。具体来说,Resource路径用于构建应用程序上下文,如 应用程序上下文和资源路径中所述

以下示例显示服务层对象(services.xml)配置文件:

lt;?xml version='1.0' encoding='UTF-8'?gt;lt;beans xmlns='http://www.springframework.org/schema/beans'

xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'

xsi:schemaLocation='http://www.springframework.org/schema/beans

http://www.springframework.org/sc

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


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

原文和译文剩余内容已隐藏,您需要先支付 30元 才能查看原文和译文全部内容!立即支付

以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。