Spring 简明教程

Spring Framework - Overview

Spring 是一个最受欢迎的应用程序开发框架,用于企业级 Java。全世界数百万开发人员使用 Spring Framework 创建高性能、易于测试的代码,可以重复使用。

Spring is the most popular application development framework for enterprise Java. Millions of developers around the world use Spring Framework to create high performing, easily testable, and reusable code.

Spring Framework 是一个开源 Java 平台。它最初由 Rod Johnson 编写,并于 2003 年 6 月首次在 Apache 2.0 许可证下发布。

Spring framework is an open source Java platform. It was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.

在大小和透明度方面,Spring 是轻量级的。Spring Framework 的基本版本大约为 2MB。

Spring is lightweight when it comes to size and transparency. The basic version of Spring framework is around 2MB.

Spring Framework 的核心特性可以用在 Java 应用程序的开发中,不过有一些扩展是在 Java EE 平台基础之上构建 Web 应用程序的。Spring 框架的目标是让 J2EE 开发用起来更轻松,并通过启用基于 POJO 的编程模型来提升良好的编程实践。

The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make J2EE development easier to use and promotes good programming practices by enabling a POJO-based programming model.

Benefits of Using the Spring Framework

以下是使用 Spring Framework 的若干重要优势清单

Following is the list of few of the great benefits of using Spring Framework −

  1. Spring enables developers to develop enterprise-class applications using POJOs. The benefit of using only POJOs is that you do not need an EJB container product such as an application server but you have the option of using only a robust servlet container such as Tomcat or some commercial product.

  2. Spring is organized in a modular fashion. Even though the number of packages and classes are substantial, you have to worry only about the ones you need and ignore the rest.

  3. Spring does not reinvent the wheel, instead it truly makes use of some of the existing technologies like several ORM frameworks, logging frameworks, JEE, Quartz and JDK timers, and other view technologies.

  4. Testing an application written with Spring is simple because environment-dependent code is moved into this framework. Furthermore, by using JavaBeanstyle POJOs, it becomes easier to use dependency injection for injecting test data.

  5. Spring’s web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks such as Struts or other over-engineered or less popular web frameworks.

  6. Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO, for example) into consistent, unchecked exceptions.

  7. Lightweight IoC containers tend to be lightweight, especially when compared to EJB containers, for example. This is beneficial for developing and deploying applications on computers with limited memory and CPU resources.

  8. Spring provides a consistent transaction management interface that can scale down to a local transaction (using a single database, for example) and scale up to global transactions (using JTA, for example).

Dependency Injection (DI)

最能与 Spring 联系起来的的技术是反转控制 Dependency Injection (DI) 的特性。 Inversion of Control (IoC) 是一个通用概念,可以使用多种不同的方式来表达。依赖关系注入只是反转控制的一个具体示例。

The technology that Spring is most identified with is the Dependency Injection (DI) flavor of Inversion of Control. The Inversion of Control (IoC) is a general concept, and it can be expressed in many different ways. Dependency Injection is merely one concrete example of Inversion of Control.

在编写复杂的 Java 应用程序时,应用程序类与其它的 Java 类应该尽可能独立,以提高在单元测试时独立于其他类来重复利用和测试这些类的可能性。依赖关系注入有助于将这些类粘合在一起,并同时保持它们的独立性。

When writing a complex Java application, application classes should be as independent as possible of other Java classes to increase the possibility to reuse these classes and to test them independently of other classes while unit testing. Dependency Injection helps in gluing these classes together and at the same time keeping them independent.

依赖关系注入到底是什么?让我们分别看一下这两个词。这里的依赖部分转换成了两个类之间的关联。例如,类 A 依赖于类 B。现在,让我们看一下第二部分,注入。它的全部含义是,类 B 将由 IoC 注入到类 A 中。

What is dependency injection exactly? Let’s look at these two words separately. Here the dependency part translates into an association between two classes. For example, class A is dependent of class B. Now, let’s look at the second part, injection. All this means is, class B will get injected into class A by the IoC.

依赖关系注入可以通过向构造函数传递参数或通过使用 setter 方法而在构建后执行。由于依赖关系注入是 Spring 框架的核心,因此我们将在一个单独的章节中使用相关的示例来解释这个概念。

Dependency injection can happen in the way of passing parameters to the constructor or by post-construction using setter methods. As Dependency Injection is the heart of Spring Framework, we will explain this concept in a separate chapter with relevant example.

Aspect Oriented Programming (AOP)

Spring 的一个关键组件是 Aspect Oriented Programming (AOP) 框架。跨越应用程序多个点的功能被称为 cross-cutting concerns ,这些交叉关注点在概念上与应用程序的业务逻辑是分开的。包括日志记录、声明式事务、安全、缓存等在内,有各种常见良好的方面示例。

One of the key components of Spring is the Aspect Oriented Programming (AOP) framework. The functions that span multiple points of an application are called cross-cutting concerns and these cross-cutting concerns are conceptually separate from the application’s business logic. There are various common good examples of aspects including logging, declarative transactions, security, caching, etc.

面向对象编程中的模块化关键单元是类,而在面向方面编程中模块化的单元是方面。依赖关系注入有助于将应用程序对象相互解耦,而面向方面编程有助于将交叉关注点与它们影响的对象解耦。

The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. DI helps you decouple your application objects from each other, while AOP helps you decouple cross-cutting concerns from the objects that they affect.

Spring 框架的 AOP 模块提供面向切面的编程实现,使你可以定义方法拦截器和切入点,以便按部就班地解耦应该分离的功能的实现代码。我们将在一个单独的章节中进一步讨论 Spring AOP 的概念。

The AOP module of Spring Framework provides an aspect-oriented programming implementation allowing you to define method-interceptors and pointcuts to cleanly decouple code that implements functionality that should be separated. We will discuss more about Spring AOP concepts in a separate chapter.