登录

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

注册

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

找回密码

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

Spring Framework开发参考手册外文翻译资料

 2021-12-18 11:12  

Spring Framework Documentation

Core Technologies

This part of the reference documentation covers all the technologies that are absolutely integral to the Spring Framework.

Foremost amongst these is the Spring Frameworkrsquo;s Inversion of Control (IoC) container. A thorough treatment of the Spring Frameworkrsquo;s IoC container is closely followed by comprehensive coverage of Springrsquo;s Aspect-Oriented Programming (AOP) technologies. The Spring Framework has its own AOP framework, which is conceptually easy to understand and which successfully addresses the 80% sweet spot of AOP requirements in Java enterprise programming.

Coverage of Springrsquo;s integration with AspectJ (currently the richestthinsp;—thinsp;in terms of featuresthinsp;—thinsp;and certainly most mature AOP implementation in the Java enterprise space) is also provided.

1. The IoC Container

This chapter covers Springrsquo;s Inversion of Control (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) principle. (See Inversion of Control.) 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) 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
  • 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, see 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

The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the 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 lets you express the objects that compose your application and the rich interdependencies between those objects.

Several implementations of the ApplicationContext interface are supplied with Spring. In stand-alone 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 typically suffices (see Convenient ApplicationContext Instantiation for Web Applications). If you use the Spring Tool Suite (an Eclipse-powered development environment), you can easily create this boilerplate configuration with a few mouse clicks or keystrokes.

The following diagram shows 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 exec

Spring Framework开发参考手册

核心技术

参考文档的这一部分涵盖了Spring框架中不可或缺的所有技术。

其中最重要的部分是Spring框架中的控制反转(IoC)功能。Spring框架的IoC功能是实现在Spring面向切面编程(AOP)技术之上的。Spring框架自行实现一套AOP框架,这套框架从概念上非常容易理解,而且成功解决了Java企业级应用中使用AOP解决的80%核心需求。

开发手册中还描述了Spring与AspectJ的集成方法。 (当前来说,AspectJ是Java企业级开发领域中特性最多、最成熟的AOP实现。)

1.IoC容器

本章将详细深入地探讨Spring框架的控制反转实现(IoC)原理。

1.1. Spring IoC容器和bean的介绍

本章介绍Spring框架中控制反转(IoC)的实现。IoC的概念与大家所熟知的依赖注入(DI)同理。这是一个通过依赖定义对象的过程,也就是说, 它们所使用的对象,只有通过构造函数参数、工厂方法的参数或这是从工厂方法的构造函数或返回值的对象实例设置的属性。然后容器在创建bean时注入这些需要的依赖。这个过程相对普通创建对象的过程是反向的(因此称之为IoC),bean本身通过直接构造类来控制依赖关系的实例化或位置,或提供诸如服务定位器模式之类的机制。

org.springframework.beans和org.springframework.context包是实现Spring IoC容器框架的基础。BeanFactory接口提供了一种更先进的配置机制来管理任意类型的对象。ApplicationContext是BeanFactory的一个子接口。它可以:

  • 让Springrsquo;s AOP功能集成变得更加简单
  • 资源信息处理(用于国际化)
  • 事件发布
  • 添加应用层的特殊上下文,例如用于web应用程序的WebApplicationContext

简而言之,BeanFactory提供了配置框架的基本功能,而ApplicationContext添加了配置企业应用的更多功能。ApplicationContext完全扩展了BeanFactory的功能,这些内容将在介绍Spring IoC容器的章节专门讲解。有关BeanFactory的更多信息,请参看“The BeanFactory”.

在Spring中,由Spring IoC容器管理的,构成程序骨架的对象称作bean。bean对象是指经过IoC容器实例化、组装和管理的对象。此外,bean就是应用程序中诸多对象之一。bean和bean的依赖被容器所使用的配置元数据反射。

1.2. 容器概览

org.springframework.context.ApplicationContext是Spring IoC容器实现的代表,它负责实例化、配置和装配实现bean的接口。容器是通过对象实例化、配置和读取元数据配置而构建对象的过程。配置元数据可以使用XML、Java注解或Java代码来呈现。它允许你处理应用程序的对象与其他对象之间的互相依赖关系。

Spring ApplicationContext接口提供了几种开箱即用的实现方式。在独立应用中,通常是创建ClassPathXmlApplicationContext或FileSystemXmlApplicationContext实例。虽然XML一直是用来定义元数据配置的传统方式,但也可以特指容器使用Java注解或编程的方式编写元数据格式,并通过提供少量的XML配置以声明对某些额外元数据的支持。

在大多数应用场合中,无需强迫用户编写代码来实例化Spring IoC容器的一个或多个实例。例如,创建Web应用程序时,只需在web.xml文件中添加一个简单的XML样板通常就足够了(见 “便捷的ApplicationContext实例化Web应用程序”)。如果您使用的是基于Eclipse的Spring Tool Suite开发环境,该样板配置只需点击几下鼠标或按几下键盘就能创建了。以便在ApplicationContext创建和初始化后,你立即拥有一个可配置的、可执行的系统或应用程序。

下图展示了Spring工作原理的高级视图。应用程序类与配置元数据相结合,这样,在创建并初始化ApplicationContext之后,就拥有了一个完全配置并可执行的系统或应用程序。

图片1 Sping IoC容器

1.2.1. 配置元数据

如上图所示,Spring IoC容器使用元数据配置(configuration metadata);这个元数据配置代表了应用程序开发人员告诉Spring容器以何种方式实例化、配置和装配对象。

传统的元数据配置是一个简单、直观的XML格式文件,本章节会大量使用XML配置来说明关键概念和IoC功能。

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

更多其他格式的元数据见:

  • 基于注解的配置: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配置是在使用@Configuration注解过的类中,在它的方法上使用@Bean注解。

这些bean定义会对应到构成应用程序的实际对象。通常,你会定义服务层对象,数据访问对象(DAOs),展示对象等等。例如在Struts Action的情况下定义架构的对象,如Hibernate的SessionFactories,JMS Queues等等。通常不会在容器中配置细粒度的域对象,因为它的创建和加载通常是DAOs和业务逻辑的责任。但是,你可以使用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;

The id attribute is a string that identifies the individual bean definition.

The class attribute defines the type of the bean and uses the fully qualified classname.

其中id属性是字符串,用来识别唯一的bean定义。class属性定义了bean的类型,需使用完整的类名。id属性的值是指引用协作对象(在这个例子没有显示用于引用协作对象的XML)。请参阅“依赖”以获取更多信息。

1.2.2.实例化容器

实例化Spring IoC容器是简单快捷的。提供给ApplicationContext构造器的路径就是实际的资源字符串,使容器能够从各种外部资源中装配元数据配置,例如本地文件系统,Java CLASSPATH等等。

下面的例子展示了服务层对象(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;

下面的例子展示了数据访问对象的daos.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='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 obj

资料编号:[4508]

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

企业微信

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