燃气表基础信息管理系统的设计与实现外文翻译资料

 2022-11-03 20:54:17

Part III. Core Technologies

part of the reference documentation covers all of those technolog This ies 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 richest - in terms of features - and certainly most mature AOP implementation in the Java enterprise space) is also provided.

7.1 Introduction to the Spring IoC container and beans

This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) 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 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.

7.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 orFileSystemXmlApplicationContext. 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, “Co

剩余内容已隐藏,支付完成后下载完整资料


第三部分. 核心技术

该部分的参考文档涵盖了Spring Framework中所有绝对不可或缺的技术。

这其中最重要的部分就是Spring Framework中的控制反转(IoC)容器。Spring Framework中IoC容器是紧随着Spring中面向切面编程(AOP)技术的全面应用的来完整实现的。Spring Framework有它自己的一套AOP框架,这套框架从概念上很容易理解,而且成功解决了Java企业级应用中AOP需求80%的核心要素。

同样Spring与AspectJ的集成(目前从功能上来说是最丰富,而且也无疑是Java企业领域最成熟的AOP实现)也涵盖在内。

  • Chapter 6, IoC容器
  • Chapter 7, 资源
  • Chapter 8, 验证,数据绑定和类型转换
  • Chapter 9, Spring表达式语言(SpEL)
  • Chapter 10, Spring中的面向切面编程
  • Chapter 11, Spring AOP API

7.1介绍 Spring IoC 容器和 bean

本章涵盖了 Spring Framework 中控制反转原则(IoC)的实现。IoC 也就是被大家所熟知的依赖注入(DI)。他是一个对象定义他的依赖的一个过程,所谓依赖,就是和它一起工作的对象,这个过程只能通过构造函数的参数,工厂方法的参数,或者已经被构造或者从工厂方法返回的对象的 setter 方法设置其属性来实现。接着容器在创建Bean后注入这些依赖。这个过程从根本上来讲是反向的,由于bean自己控制实例,或者直接通过类的结构,类似于 Service Locator 模式来定位他的依赖。

org.springframework.beans 和 org.springframework.context 包是 Spring Framework 的 IoC 容器的根本。BeanFactory 接口提供了一种更先进的配置机制来管理任意类型的对象。ApplicationContext 是 BeanFactory 的一个子接口。ApplicationContext 使得和 Spring 的AOP 功能集成变得更简单;添加了信息资源处理(国际化中使用),事件发布;还添加了应用程序层的特殊上下文 ,如用于 web 应用程序的 WebApplicationContext。

简而言之,BeanFactory提供了配置框架和基本功能,而ApplicationContext 添加了更多企业应用功能。ApplicationContext完整扩展了BeanFactory, 这些内容在介绍Spring的IoC容器的章节里面会专门讲到。更多使用 BeanFactory 请参阅 章节 6.16, “The BeanFactory”.

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

7.2容器总览

org.springframework.context.ApplicationContext 代表 Spring IoC 容器,并负责实例化,配置和组装上述 bean 的接口。容器是通过对象实例化,配置,和读取配置元数据汇编得到对象的构建。配置元数据可以是用 XML,Java 注解,或 Java 代码来展示。它可以让你描述组成应用程序的对象和对象间丰富的相互依赖。

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

在大多数应用场合,不需要明确的用户代码来实例化一个 Spring IoC 容器的一个或多个实例。例如,在 web 应用程序中,在应用程序的 web.xml文件中一个简单的样板网站的 XML 描述符通常就足够了(见第6.15.4,“便捷的 ApplicationContext 实例化 Web 应用程序”)。如果您使用的是基于Eclipse的 Spring Tool Suite开发环境,该样板配置可以很容易地用点击几下鼠标或键盘创建。

7.2.1配置元数据

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

传统的配置元数据是一个简单而直观的 XML 格式,这是大多数本章用来传达关键概念和 Spring IoC 容器的功能。

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

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

  • ·基于注解的配置:Spring 2.5 的推出了基于注解的配置元数据支持。
  • ·基于Java的配置:Spring3.0 开始,由 Spring JavaConfig 项目提供了很多功能成为核心 Spring 框架的一部分。因此,你可以通过使用Java,而不是 XML 文件中定义外部 bean 到你的应用程序类。要使用这些新功能,请参阅 @Configuration,@Bean,@Import 和 @DependsOn 注解。

Spring 配置至少一个,通常不止一个 bean 由容器来管理。基于 XML 的配置元数据将这些 bean 配置为lt;bean/gt;元素,并放置到lt;bean/gt; 元素内部。 Java 配置通常在 @Configuration注解的类中使用 @Bean 注解到方法上。

这些 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 用于参照协作对象未在本例中示出;请参阅 依赖以获取更多信息。

7.2.2 实例化容器

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

ApplicationContext context =

new ClassPathXmlApplicationContext(new String[] {'services.xml', 'daos.xml'});

当你了解 Spring 的 IoC 容器,你可能想知道更多关于 Spring 的Resource 抽象,如第7章,资源,它提供了一种方便的从一个 URI 语法定义的位置读取一个InputStream 描述。特别地,资源路径被用作构建应用程序的上下文,详见第7.7节,“应用环境和资源的路径”

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

剩余内容已隐藏,支付完成后下载完整资料


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

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

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