Spring 简明教程

Spring Tutorial

Spring Framework 是一个开源 Java 平台,它为非常轻松而且非常快速地开发健壮的 Java 应用程序提供全面的基础设施支持。Spring Framework 最初是由罗德·约翰逊编写的,并于 2003 年 6 月在 Apache 2.0 许可证下首次发布。本教程基于 2015 年 3 月发布的 Spring Framework 4.1.6 版本编写。

Spring framework is an open source Java platform that provides comprehensive infrastructure support for developing robust Java applications very easily and very rapidly. Spring framework was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003. This tutorial has been written based on Spring Framework version 4.1.6 released in Mar 2015.

Why to Learn Spring?

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.

Applications of Spring

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

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

  1. POJO Based - 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. Modular - 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. Integration with existing frameworks - 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. Testablity - 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. Web MVC - 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. Central Exception Handling - Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO, for example) into consistent, unchecked exceptions.

  7. Lightweight - 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. Transaction management - 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).

Audience

本教程专为 Java 程序员设计,他们需要详细了解 Spring 框架及其架构和实际用法。本教程将带你达到中级专业水平,从此你可以向更高的专业水平迈进。

This tutorial is designed for Java programmers with a need to understand the Spring framework in detail along with its architecture and actual usage. This tutorial will bring you at an intermediate level of expertise, from where you can take yourself to higher levels of expertise.

Prerequisites

在继续本教程之前,你应该对 Java 编程语言有深入的了解。还需要对 Eclipse IDE 有基本的了解,因为所有示例均使用 Eclipse IDE 编译。

Before proceeding with this tutorial, you should have a good understanding of Java programming language. A basic understanding of Eclipse IDE is also required because all the examples have been compiled using Eclipse IDE.