2:spring介绍

来源:互联网 发布:java百度课程 编辑:程序博客网 时间:2024/06/15 02:46
  1. Introduction to the Spring Framework
    The Spring Framework is a Java platform that provides comprehensive infrastructure support for
    developing Java applications. Spring handles the infrastructure so you can focus on your application.
    Spring enables you to build applications from “plain old Java objects” (POJOs) and to apply enterprise
    services non-invasively to POJOs. This capability applies to the Java SE programming model and to
    full and partial Java EE.
    Examples of how you, as an application developer, can benefit from the Spring platform:
    • Make a Java method execute in a database transaction without having to deal with transaction APIs.
    • Make a local Java method a remote procedure without having to deal with remote APIs.
    • Make a local Java method a management operation without having to deal with JMX APIs.
    • Make a local Java method a message handler without having to deal with JMS APIs.
    2.spring框架的介绍
    spring框架是一个提供综合基础设施支持java应用程序java开发平台,Spring处理基础设施,因此您可以专注于应用程序。
    spring可以让你构建应用时使用简单的java类b并且应用非侵入性的企业服务到简单java类。此功能适用于java编程模型全部和部分java EE。
    例如作为一名应用开发者,你可以从spring平台如何获益。
    编写一个java方法执行数据库中的事务可以没有处理事务API。
    不使用java事物处理API编写一个本地管理操作方法。
    2.1 Dependency Injection and Inversion of Control
    A Java application — a loose term that runs the gamut from constrained, embedded applications to n-tier,server-side enterprise applications — typically consists of objects that collaborate to form the application proper. Thus the objects in an application have dependencies on each other.Although the Java platform provides a wealth of application development functionality, it lacks the means to organize the basic building blocks into a coherent whole, leaving that task to architects and developers. Although you can use design patterns such as Factory, Abstract Factory, Builder, Decorator,and Service Locator to compose the various classes and object instances that make up an application,
    these patterns are simply that: best practices given a name, with a description of what the pattern does,where to apply it, the problems it addresses, and so forth. Patterns are formalized best practices that you must implement yourself in your application.
    The Spring Framework Inversion of Control (IoC) component addresses this concern by providing a formalized means of composing disparate components into a fully working application ready for use.The Spring Framework codifies formalized design patterns as first-class objects that you can integrate into your own application(s). Numerous organizations and institutions use the Spring Framework in this manner to engineer robust, maintainable applications.
    2.1:依赖注入和控制反转
    一个java应用-一种松散的术语,从受约束的范围内运行。嵌入式应用程序开发,服务器端的企业应用-通常由协作形成应用程序的对象组成。因此,应用程序中的对象相互依赖。
    虽然java平台提供丰富的应用开发功能。它缺乏方式去把基本的建筑块组织成一个整体。把这个问题留给架构师和开发者。尽管你可以使用设计模式例如工厂模式,抽象工厂,builder,装饰模式和服务定位器来组成构成应用程序的各种类和对象实例。这些模式是简单的:给定名称的最佳实践,描述模式的作用,在哪里应用它,它所处理的问题,等等。模式是形式化的最佳实践。您必须在应用程序中实现自己。
    Spring的控制反转组件通过提供一个解决这个问题的方式,将完全不同的组件组合成一个完全可用的应用程序准备使用的形式化方法。Spring框架的编纂形式化设计模式作为对象,你可以整合到你自己的应用程序中去。许多组织和机构都使用Spring框架设计健壮、可维护的应用程序的方式。
    2.2 Modules
    The Spring Framework consists of features organized into about 20 modules. These modules are grouped into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming),Instrumentation, Messaging, and Test, as shown in the following diagram.
    这里写图片描述
    Figure 2.1. Overview of the Spring Framework
    The following sections list the available modules for each feature along with their artifact names and the
    topics they cover. Artifact names correlate to artifact IDs used in Dependency Management tools.
    2.2模块
    spring框架包括的特性被组织成20个模块。这些模块被分到核心容器模块,数据连接、集成,web,AOP(面向切面编程),声明式,消息,测试,正如下边的图表中所展示的:
    这里写图片描述
    2.1:spring框架整体概述
    下面的部分列出了他们包括的每个功能的可用模块以及它们的工件名和主题,工件名与依赖管理工具中使用的工件ID相关。

Core Container
The Core Container consists of the spring-core, spring-beans, spring-context, springcontext-support, and spring-expression (Spring Expression Language) modules.
The spring-core and spring-beans modules provide the fundamental parts of the framework, including the IoC and Dependency Injection features. The BeanFactory is a sophisticated implementation of the factory pattern. It removes the need for programmatic singletons and allows you to decouple the configuration and specification of dependencies from your actual program logic. The Context (spring-context) module builds on the solid base provided by the Core and Beans
modules: it is a means to access objects in a framework-style manner that is similar to a JNDI registry. The Context module inherits its features from the Beans module and adds support for internationalization (using, for example, resource bundles), event propagation, resource loading, and the transparent creation of contexts by, for example, a Servlet container. The Context module also supports Java EE features such as EJB, JMX, and basic remoting. The ApplicationContext interface is the focal point of the Context module. spring-context-support provides support for integrating
common third-party libraries into a Spring application context for caching (EhCache, Guava, JCache),mailing (JavaMail), scheduling (CommonJ, Quartz) and template engines (FreeMarker, JasperReports,Velocity).
The spring-expression module provides a powerful Expression Language for querying and manipulating an object graph at runtime. It is an extension of the unified expression language (unified EL) as specified in the JSP 2.1 specification. The language supports setting and getting property values,property assignment, method invocation, accessing the content of arrays, collections and indexers,Spring Framework Reference Documentation 4.3.0.RELEASE Spring Framework 5 logical and arithmetic operators, named variables, and retrieval of objects by name from Spring’s IoC container. It also supports list projection and selection as well as common list aggregations.

核心容器包括 spring-core, spring-beans, spring-context, springcontext-support和spring-expression(spring表达式模块)
spring-core和spring-beans模块提供框架的基础部分,包括控制反转和依赖注入特性,BeanFactory是一个复杂的工厂模式的实施,它不单需要让你编程将依赖项的配置和规范与实际的程序逻辑解耦

上下文(spring-context)模块建立在核心模块和bean模块提供的坚实基础上:这是一个方法来访问一个框架式的方式,类似于一个JNDI对象注册表。上下文模块继承了bean模块的特性,并增加了对国际化的支持(例如resourcebundles),事件传播、资源加载和上下文的透明创建,例如,一个servle容器,上下文模块也支持javaEE特性如EJB,JMX喝一些基础远程控制。应用程序上下文接口是上下文模块的焦点,spring-context-support提供集成支持用于缓存Spring应用程序上下文的常用的第三方库 (EhCache, Guava, JCache),邮件(JavaMail),行程安排 (CommonJ, Quartz)和一些模版引擎(FreeMarker, JasperReports,Velocity).
spring-expression模块在程序运行期间提供强大的查询和操作对象表达式语言。它是统一表达式语言的扩展(统一)。EL)如JSP 2.1规范中指定的。这种语言支持setting/getting属性值,属性分配,方法调用,访问数组的内容,收集和索引,逻辑和算法操作,变量命名,从Spring的IOC容器中检索对象的名称。它还支持列表投影和选择以及公共列表聚合。
AOP and Instrumentation
The spring-aop module provides an AOP Alliance-compliant aspect-oriented programming
implementation allowing you to define, for example, method interceptors and pointcuts to cleanly
decouple code that implements functionality that should be separated. Using source-level metadata
functionality, you can also incorporate behavioral information into your code, in a manner similar to that
of .NET attributes.
The separate spring-aspects module provides integration with AspectJ.
The spring-instrument module provides class instrumentation support and classloader
implementations to be used in certain application servers. The spring-instrument-tomcat module
contains Spring’s instrumentation agent for Tomcat.
AOP详解:
spring-aop模块提供了AOP联盟标准切面编程的实现允许你去定于,例如,方法拦截器和切点去实现应该分离的功能的解耦代码。使用源级元数据功能性,您还可以将行为信息合并到代码中。以一种和.NET属性相似的方法。
分离开的spring-aspects模块提供AspectJ实现。
spring-instrument模块提供类声明支持和在几个热定应用服务器端类加载实现。spring-instrument-tomcat包括spring对tomcat的声明代理。


Messaging
Spring Framework 4 includes a spring-messaging module with key abstractions from the Spring Integration project such as Message, MessageChannel, MessageHandler, and others to serve as a foundation for messaging-based applications. The module also includes a set of annotations for mapping messages to methods, similar to the Spring MVC annotation based programming model.
消息模块
spring框架4包括spring-messaging模块和来自Spring的关键抽象消息等集成项目, MessageChannel, MessageHandler,和其他充当基于消息的应用程序基础,这个模块也包括一系列注解将消息映射到方法,和Spring MVC注解映射基础编程模型类似。


Data Access/Integration
The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS, and Transaction modules.
The spring-jdbc module provides a JDBC-abstraction layer that removes the need to do tedious
JDBC coding and parsing of database-vendor specific error codes.
The spring-tx module supports programmatic and declarative transaction management for classes
that implement special interfaces and for all your POJOs (Plain Old Java Objects).
The spring-orm module provides integration layers for popular object-relational mapping APIs,
including JPA, JDO, and Hibernate. Using the spring-orm module you can use all of these O/Rmapping frameworks in combination with all of the other features Spring offers, such as the simple declarative transaction management feature mentioned previously.
The spring-oxm module provides an abstraction layer that supports Object/XML mapping implementations such as JAXB, Castor, XMLBeans, JiBX and XStream.
The spring-jms module (Java Messaging Service) contains features for producing and consuming messages. Since Spring Framework 4.1, it provides integration with the spring-messaging module.
数据连接/集成
数据连接/集成层包括JDBC,ORM,OXM,JM和事物模块
spring-jdbc模块提供JDBC抽象层消除枯燥乏味的数据库JDBC编码与解析。
spring-tx 模块实现了特定接口和你所有的java简单类,支持对类的程序和声明事物管理
spring-orm模块 提供对流行的ORM映射框架的支持,包括JPA,JDO和Hibernate.使用spring-orm模块,你可以使用这些ORM框架组合spring提供的特性,例如前面提到的简单事物声明特性。
spring-oxm模块提供一个对象-XML文件映射抽象层例如JAXB, Castor, XMLBeans, JiBX and XStream.
spring-jms模块(java消息服务)包括生产和消费信息。自从spring4.1开始,它提供了spring-messaging模块的集成


Web
The Web layer consists of the spring-web, spring-webmvc, spring-websocket, and spring webmvc-portlet modules.
The spring-web module provides basic web-oriented integration features such as multipart file upload functionality and the initialization of the IoC container using Servlet listeners and a web-oriented application context. It also contains an HTTP client and the web-related parts of Spring’s remoting support.
The spring-webmvc module (also known as the Web-Servlet module) contains Spring’s model view-controller (MVC) and REST Web Services implementation for web applications. Spring’s MVC framework provides a clean separation between domain model code and web forms and integrates with all of the other features of the Spring Framework.
The spring-webmvc-portlet module (also known as the Web-Portlet module) provides the MVC implementation to be used in a Portlet environment and mirrors the functionality of the spring-webmvc module.
web层包括spring-web, spring-webmvc, spring-websocket和springwebmvc-portlet模块
spring-web 模块提供基础的面向web集成特性例如multipart file 文件上传和使用servlet监听器和面向web应用程序上下文对IOC容器的初始化。溶蚀它包括HTTP客户端和与web相关部分的spring远程支持
spring-webmvc模块(也因为web-servletmo模块而著名)包括spring对web应用的MVC模式和REST风格的web服务层的实现。spring的MVC框架提供明显的域模型代码和web表单的解耦并且集成所有的spring框架的其它特性。
spring-webmvc-portlet模块提供在porlet环境和镜像的spring-webmvc模块功能的mvc实现


Test
The spring-test module supports the unit testing and integration testing of Spring components with JUnit or TestNG. It provides consistent loading of Spring
ApplicationContexts and caching of those contexts. It also provides mock objects that you can use to test your code in isolation
测试
spring-test模块支持使用junit和TestNG为spring组件的单元测试和集成测试。它提供了一致的对ApplicationContexts加载和缓存的语境。它还提供了可以用来隔离测试代码的模拟对象。


2.3 Usage scenarios
The building blocks described previously make Spring a logical choice in many scenarios, from embedded applications that run on resource-constrained devices to full-fledged enterprise applications that use Spring’s transaction management functionality and web framework integration.
2.3使用场景
以前描述的构建块使Spring成为许多场景中的逻辑选择。从在资源受限的设备上运行到完整企业应用程序的嵌入式应用程序(它使用Spring的事务管理功能和Web框架集成)。

这里写图片描述

Figure 2.2. Typical full-fledged Spring web application
Spring’s declarative transaction management features make the web application fully transactional, just as it would be if you used EJB container-managed transactions. All your custom business logic can be implemented with simple POJOs and managed by Spring’s IoC container. Additional services include support for sending email and validation that is independent of the web layer, which lets you choose where to execute validation rules. Spring’s ORM support is integrated with JPA, Hibernate and JDO; for example, when using Hibernate, you can continue to use your existing mapping files and standard Hibernate SessionFactory configuration. Form controllers seamlessly integrate the web-layer with the domain model, removing the need for ActionForms or other classes that transform HTTP parameters to values for your domain model.
spring的声明式事物管理特性让web应用更加完全事物性,就像你使用EJB容器管理事物。所有自定义业务逻辑被Spring IOC容器管理可以实现简单的POJOs。其它的服务包括和web层独立的一些支持邮件发送和验证的功能。它将会使你在何时使用验证规则。Spring ORM支持集成了JPA,Hibernate和JDO;例如使用hibernate,你可以继续使用你的映射文件和标准的hibernate会话工厂配置。表单控制器无缝集成Web层和域模型,移除了ActionForms和一些其它的向你的域模型发送支持HTTP参数和值的需要。
这里写图片描述
Sometimes circumstances do not allow you to completely switch to a different framework. The Spring Framework does not force you to use everything within it; it is not an all-or-nothing solution. Existing front-ends built with Struts, Tapestry, JSF or other UI frameworks can be integrated with a Springbased middle-tier, which allows you to use Spring transaction features. You simply need to wire up your business logic using an ApplicationContext and use a WebApplicationContext to integrate your web layer.
有时候情况不允许你完全切换到不同的框架。Spring Framework不会强制你使用它所有的东西。它不是一个全有或全无的解决方案。前端可以用Struts,Tapestry, JSF 或者其他的集成在基于Spring中间层集成的 UI框架来构建,它允许你使用Spring的事物特性,你可以简化的用应用程序上下文和web上下文写你的业务逻辑并且集成到你的web层。
这里写图片描述
When you need to access existing code through web services, you can use Spring’s Hessian-, Burlap-, Rmi- or JaxRpcProxyFactory classes. Enabling remote access to existing applications is not difficult.这里写图片描述
当你需要用web连接已有的代码,你可以使用Spring’s Hessian-,Burlap-, Rmi- or JaxRpcProxyFactory 这些类,远程控制将使你连接其它应用程序不再困难。
The Spring Framework also provides an access and abstraction layer for Enterprise JavaBeans,enabling you to reuse your existing POJOs and wrap them in stateless session beans for use in scalable,fail-safe web applications that might need declarative security.
Spring框架提供和一些企业抽象层java bean的连接,可以让你重复大量的使用POJO和包装它们到一些会话工厂Bean,可能需要声明性安全的故障安全Web应用程序。
Dependency Management and Naming Conventions
Dependency management and dependency injection are different things. To get those nice features of Spring into your application (like dependency injection) you need to assemble all the libraries needed (jar files) and get them onto your classpath at runtime, and possibly at compile time. These dependencies are not virtual components that are injected, but physical resources in a file system (typically). The
process of dependency management involves locating those resources, storing them and adding them to classpaths. Dependencies can be direct (e.g. my application depends on Spring at runtime), or indirect(e.g. my application depends on commons-dbcp which depends on commons-pool). The indirect dependencies are also known as “transitive” and it is those dependencies that are hardest to identify and manage.
If you are going to use Spring you need to get a copy of the jar libraries that comprise the pieces of Spring that you need. To make this easier Spring is packaged as a set of modules that separate the dependencies as much as possible, so for example if you don’t want to write a web application you don’t need the spring-web modules. To refer to Spring library modules in this guide we use a shorthand naming convention spring-* or spring-.jar, where represents the short name for the module(e.g. spring-core, spring-webmvc, spring-jms, etc.). The actual jar file name that you use is
normally the module name concatenated with the version number (e.g. spring-core-4.3.0.RELEASE.jar).
依赖管理和依赖注入是不同的事情。如果你想在你的应用中使用这些特性(像依赖注入),你需要将所有的类库jar包导入并在运行和编译期间期间放在classpath里面。这些依赖关系不是注入的虚拟组件,而是文件系统中的物理资源。依赖管理的步骤包括定位这些资源,存储并且把它们加到类路径里。可以是直接依赖也可以是简介依赖,间接依赖有传递性并且这种依赖很难去辨别和管理。
如果你使用Spring你将需要复制一系列的jar文件去组成你需要的spring框架。为了让这个步骤尽可能简单,Spring打包和分离了一些近可能多的模块。所以如果你想写一个web应用程序,你并不需要spring-web模块。在这个指南中我们引用spring类库用一个速记命名约定,spring-或者spring-.jar,*代表模块的简写(例如spring-core,spring-webmvc,spring-jms等等),实际上你使用的jar模块文件命名和spring版本有关联。
Each release of the Spring Framework will publish artifacts to the following places:
• Maven Central, which is the default repository that Maven queries, and does not require any special configuration to use. Many of the common libraries that Spring depends on also are available from Maven Central and a large section of the Spring community uses Maven for dependency management, so this is convenient for them. The names of the jars here are in the form spring-*-.jar and the Maven groupId is org.springframework.
• In a public Maven repository hosted specifically for Spring. In addition to the final GA releases, this repository also hosts development snapshots and milestones. The jar file names are in the same form as Maven Central, so this is a useful place to get development versions of Spring to use with other libraries deployed in Maven Central. This repository also contains a bundle distribution zip file that contains all Spring jars bundled together for easy download.So the first thing you need to decide is how to manage your dependencies: we generally recommend the use of an automated system like Maven, Gradle or Ivy, but you can also do it manually by downloading
all the jars yourself.You will find bellow the list of Spring artifacts. For a more complete description of each modules, see Section 2.2, “Modules”.
每一个spring框架的发行版本都将如下的地方发行他们的产品:
Maven中心,这是maven查询的默认仓库,使用的时候不需要任何特殊的配置。许多spring常用的类库都是可以获取的,并且Spirng社区使用maven作为依赖管理。所以这对于他们很方便。jar文件的命名在这里是spring-*-.jar并且maven的坐标是org.spring.framework.
在spring公共的maven仓库里。除了最后的发型版本,这个仓库还承载开发快照和里程碑版本。这些jar文件的名字在maven中心里,所以这是一个很有用的获得spring发行版和一些其他类库的地方。这个仓库包含了一个完整的zip文件包括所有的spring jar包可以轻松的下载下来。
所以你需要做的第一件事是决定怎样管理你的依赖,我们强烈建议您使用自动化构建工具系统如maven,Gradle或者lvy,但是你也可以完全手工下载jar包和使用它们。
你讲发现如下的spring产品,如果想看每个模块的完整描述,请看2.2章节
Table 2.1. Spring Framework Artifacts
GroupId ArtifactId Description
org.springframework spring-aop Proxy-based AOP support
org.springframework spring-aspects AspectJ based aspects
org.springframework spring-beans Beans support, including
Groovy
org.springframework spring-context Application context runtime,

原创粉丝点击