Spring IoC容器和bean的介绍外文翻译资料

 2021-12-19 09:12

英语原文:

5.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 Framework#39;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#39;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. TheApplicationContext is a complete superset of the BeanFactory, and is used exclusively in this chapter in descriptions of Spring#39;s IoC container. For more information on using the BeanFactory instead of the ApplicationContext, refer to Sectionensp;5.15, “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.

5.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 J2EE web descriptor XML in the web.xml file of the application will typically suffice (see Sectionensp;5.14.4, “Convenientensp;ApplicationContextensp;instantiation for web applications”). If you are using the SpringSource Tool Suite Eclipse-powered development environment or Spring Roo 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 ApplicationContextis created and initialized, you have a fully configured and executable system or application.

The Spring IoC container

5.2.1 Configuration metadata

As the preceding

Spring IoC容器和bean的介绍

这节内容主要介绍Spring Framework实现IoC的原理。IoC也被叫做依赖注入(DI)。这是一个对象定义他们的依赖的过程。其他对象使用它时,只能通过构造方法参数,传递给工厂方法的参数,或者被设置到对象的属性上并在此对象实例化后被构建或者从一个工厂方法返回。容器在创建bean时注入这些依赖。这个过程是从根本上看是完全相反的,所以叫做控制反转(IoC)。

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

总之,BeanFactory提供了配置框架和基本功能。ApplicationContext提供更多企业级应用的相关功能。ApplicationContext是BeanFactory完整的超集,专门用于此章节。如果想使用BeanFactory来替代ApplicationContext,可以参考后面专门介绍BeanFactory的章节。

在Spring中,构成你的应用程序的主干对象(译者注:例如发送邮件的对象),是被SpringIoC容器来管理的,它们被称为bean。bean是一个对象,它被Spring IoC容器来实例化,组装和管理。bean只是应用程序中许多对象之一。bean和bean之间的依赖关系则反映在容器使用的配置中。

容器概述

org.springframework.context.ApplicationContext接口代表了Spring IoC容器,它负责实例化,组装和配置上面所说的bean。容器通过读取配置来实例化,组装和配置bean。配置的主要形式有XML,Java注解和Java代码,它允许你来定义应用中的对象和他们之间复杂的依赖关系。

ApplicationContext接口的一些实现做到了开箱即用。在单机应用中通常会创建一个ClassPathXmlApplicationContextgt;FileSystemXmlApplicationContext的实例。

大多数应用场景中,不需要用户显示的创建一个或多个Spring IoC容器的实例。例如,一个web应用,简单的8行(大约)代码的web.xml文件即可满足需求。(后面章节会给出在web应用中如何方便的创建ApplicationContext实例)。如果你正在使用基于eclipse的Spring Tool Suite,那么这个配置将会很容易创建,只需要

Spring IoC容器使用了配置;作为一个应用开发者,这些配置可以让你告诉Spring容器去实例化,配置,组装应用程序中的对象。

配置元数据传统上是使用简单,直观的XML文件,这节中,主要用XML配置来说明Spring IoC容器的关键概念和特性。

注意:基于XML并不是配置的唯一途径。Spring IoC容器与配置文件完全解耦。现在,许多开发者在他们的Spring应用中选择使用基于Java的配置(后续章节会介绍此部分内容)。

Spring的配置由至少一个,通常多个Spring容器管理的bean定义组成。基于XML来配置bean,是配置在lt;bean/gt;元素内,其父元素,也是顶级元素为lt;beans/gt;。基于Java配置通常用@Bean来标注方法或用@Configuration来标注类。

这些bean定义对应于构成应用程序的实际对象。通常你定义服务层对象,数据访问层对象(DAO),展示层对象例如Struts Action实例,基础底层对象例如Hibernate的SessionFactoriesJMS Queues等等。通常,不会在容器中配置细粒度的domain对象(例如数据库po对象),因为加载这些类通常是DAO和业务逻辑代码的职责。然而,你可以利用Spring集成的AspectJ来在Spring IoC容器之外创建和加载domain对象。这部分内容在后面相关章节会给出介绍。

下面的例子基于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;!-- 在这里定义bean和它相关的内容 --gt;

lt;/beangt;

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

lt;!-- 在这里定义bean和它相关的内容 --gt;

lt;/beangt;

lt;!-- 更多的bean定义... --gt;

lt;/beansgt;

属性id是一个字符串,是某个bean的唯一标识。class属性用来定义bean的类型,这里需要使用类的完全限定名。id属性的值可以被协作的bean来引用。关于协作bean的配置,这个例子并没有给出,但是可以参考下面Dependencies章节。

实例化一个容器对象

实例化一个Spring IoC容器非常简单。传递给ApplicationContext构造方法的相对路径或绝对路径,实际上是资源的路径,它允许容器从外部各种资源(例如本地文件系统,Java类路径等等)加载配置元数据。

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

学习了Spring IoC容器之后,你可能会想去知道更多有关于Spring的Resource,像在Resource章节描述的一样,Spring Resource提供了方便的机制来使用URI规则读取InputStream。尤其是,Resource路径被用来构建应用程序context,下面章节会给出具体描述。

下面的例子展示了service层对象(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/schema/beans/spring-beans.xsd'gt;

lt;!-- services --gt;

lt;bean id='petStore' class='org.springframework.samples.jpetstore.services.PetStoreServiceImpl'gt;

lt;property name='accountDao' ref='accountDao'/gt;

lt;property name='itemDao' ref='itemDao'/gt;

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

lt;/beangt;

lt;!-- more bean definitions for services go here --gt;

lt;/beansgt;

下面的例子给出了数据处理层对象的配置:

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='accountDao'

class='org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao'gt;

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

lt;/beangt;

lt;bean id='itemDao' class='org.springframework.samples.jpetstore.dao.jpa.JpaItemDao'gt;

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

lt;/beangt;

lt;!-- more bean definitions for data access objects go here --gt;

lt;/beansgt;

上面的例子,service层由PetStoreServiceImpl和两个JpaAccountDao、JpaItemDao类型的数据处理对象(基于JPA标准)组成。property标签的name属性的值意为Java Bean属性的名称,ref属性值则为引用的另一个定义好的bean。id和ref的关联,表达了对象之间的关系和相互引用等。对象之间详细的依赖关系介绍,可以参考后面的Dependencies章节。

编写基于XML的配置文件

通过多个XML配置文件来定义bean是非常有必要的。通常,在你的架构中,每一个XML配置文件表示特定的逻辑层或模块。

你可以使用application context构造方法来从所有的XML来加载bean。这个构造方法可以接受多个资源,如上一节所示。还有一种选择,使用lt;import/gt;标签来加载其他的XML配置文件,如下面例子:

lt;beansgt;

lt;import resource='services.xml'/gt;

lt;import resource='resources/messageSource.xml'/gt;

lt;import resource='/resources/themeSource.xml'/gt;

lt;bean id='bean1' class='...'/

资料编号:[4492]

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

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