Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.49 MB, 753 trang )
9799ch02.qxd
22
5/5/08
4:42 PM
Page 22
CHAPTER 2 ■ INTRODUCTION TO SPRING
Introducing Spring’s Modules
The architecture of the Spring framework is divided into modules, as shown in Figure 2-1.
Spring’s module assembling is so flexible that your applications can build on top of their different subsets in different usage scenarios.
Figure 2-1. An overview of the Spring framework’s modules
In Figure 2-1, the modules are organized in hierarchical fashion, with the upper modules
depending on the lower ones. As you can see, the Core module lies at the very bottom since it’s
the foundation of the Spring framework.
Core: This module provides core functions of the Spring framework. It provides a basic
Spring IoC container implementation called BeanFactory. The basic IoC container features will be introduced in Chapter 3.
Context: This module builds on top of the Core module. It extends the Core module’s
functions and provides an advanced Spring IoC container implementation called
ApplicationContext, which adds features such as internationalization (I18N) support,
event-based communication, and resource loading. These advanced IoC container features will be covered in Chapter 4.
AOP: This module establishes an aspect-oriented programming framework, which is
referred to as Spring AOP. AOP is another of Spring’s fundamental concepts besides IoC.
Chapters 5 and 6 will cover both the classic and new Spring AOP approaches, and also
integrating AspectJ with Spring.
9799ch02.qxd
5/5/08
4:42 PM
Page 23
CHAPTER 2 ■ INTRODUCTION TO SPRING
JDBC: This module defines an abstract layer on top of the native JDBC API, which supports programming against JDBC in a template-based approach that dispenses with
boilerplate code. It also parses database vendor–specific error codes into Spring’s
DataAccessException hierarchy. The details of Spring’s JDBC support will be covered in
Chapter 7.
TX: This module supports both programmatic and declarative approaches for you to
manage transactions. These can be used to add transaction capability to your simple Java
objects. Transaction management will be discussed thoroughly in Chapter 8.
ORM: This module integrates popular object/relational mapping frameworks such as
Hibernate, JDO, TopLink, iBATIS, and JPA into Spring. The details of Spring’s ORM integration will be covered in Chapter 9.
Web MVC: This module establishes a web application framework conforming to the
model-view-controller (MVC) design pattern. This framework is built with the Spring
framework’s facilities so that you can use all Spring’s features in web application development. The Spring MVC framework will be introduced in Chapter 10.
Web Framework Integration: This module makes it easier for you to use Spring as a backend implementation for other popular web frameworks such as Struts, JSF, WebWork, and
Tapestry. Chapter 11 will introduce integrating Spring with several popular web frameworks.
Testing: This module provides support for both unit testing and integration testing. It
establishes the Spring TestContext framework, which abstracts underlying testing frameworks such as JUnit 3.8, JUnit 4.4, and TestNG. Testing Spring applications will be
discussed in Chapter 12.
Portlet MVC: This module establishes a portlet framework, also conforming to the MVC
design pattern. The Portlet MVC framework will be introduced in Chapter 14.
Enterprise Integration: This module integrates popular enterprise services, including several remoting technologies, EJB, JMS, JMX, e-mail, and scheduling, into Spring to make
them easier to use. Spring’s enterprise integration will be covered in Chapters 16, 17,
and 18.
Introducing Spring’s Releases
Two and a half years after the Spring framework 1.0 was released in March 2004, in October
2006, the first major upgrade—Spring 2.0—was released with the following major improvements and new features:
XML schema–based configuration: In Spring 1.x, XML bean configuration files only support DTDs, and everything you can define within them must be through the
element. Spring 2.0 supports XML schema–based configuration, which allows you to use
Spring’s new tags. As a result, your bean configuration files can be much simpler and
clearer. You will take advantage of Spring’s XML schema–based configuration throughout
this book. For a look at its basis, please see Chapter 3.
23
9799ch02.qxd
24
5/5/08
4:42 PM
Page 24
CHAPTER 2 ■ INTRODUCTION TO SPRING
Annotation-driven configuration: As a complement to XML-based configuration,
Spring 2.0 supports annotation-driven configuration in certain modules, such as
@Required, @Transactional, @PersistenceContext, and @PersistenceUnit. For usage
of these annotations, please refer to Chapters 3, 8, and 9.
New Spring AOP approach: The classic AOP usage in Spring 1.x is through a set of proprietary Spring AOP APIs. Spring 2.0 introduces a brand-new AOP approach through writing
POJOs with either AspectJ annotations or XML schema–based configuration. Chapter 6
discusses this new AOP approach in detail.
Easier transaction declaration: Declaring transactions in Spring 2.0 is much easier. You
can take advantage of the new Spring AOP approach to declare transaction advices, or
apply the @Transactional annotations with the
discusses transaction management in detail.
JPA support: Spring 2.0 introduces support for the Java Persistence API in its ORM module. JPA support is covered in Chapter 9.
Form tag library: Spring 2.0 introduces a new form tag library to make developing forms
in Spring MVC easier. Spring’s form tag library usage will be introduced in Chapter 10.
Asynchronous JMS support: Spring 1.x only supports receiving JMS messages synchronously through JmsTemplate. Spring 2.0 adds support for asynchronous JMS reception
through message-driven POJOs. This will be covered in Chapter 17.
Scripting language support: Spring 2.0 supports implementing beans with the scripting
languages JRuby, Groovy, and BeanShell. Chapter 19 introduces Spring’s scripting language support.
In November 2007, Spring 2.5 was released to enhance Spring 2.0 with the following new
features. Spring 2.5 is the current release at the time of writing.
Annotation-driven configuration: Spring 2.0 added support for several annotations to
simplify bean configuration. Spring 2.5 supports more, including @Autowired and the
JSR-250 annotations @Resource, @PostConstruct, and @PreDestroy. For usage of these
annotations, please refer to Chapters 3 and 4.
Component scanning: Spring 2.5 can automatically detect your components with particular stereotype annotations from the classpath without manual configuration. The
component-scanning feature will be discussed in Chapter 3.
AspectJ load-time weaving support: Spring 2.5 supports weaving AspectJ aspects into the
Spring IoC container at load time, letting you use AspectJ aspects beyond the reach of
Spring AOP’s support. AspectJ load-time weaving support will be discussed in Chapter 6.
Annotation-based web controllers: Spring 2.5 supports a new annotation-based approach
to web controller development. It can auto-detect your controller classes with the
@Controller annotation, and also the information you configure in the @RequestMapping,
@RequestParam, and @ModelAttribute annotations. Chapter 10 will cover this feature in
detail.
9799ch02.qxd
5/5/08
4:42 PM
Page 25
CHAPTER 2 ■ INTRODUCTION TO SPRING
Enhanced testing support: Spring 2.5 establishes a new testing framework called Spring
TestContext framework, which provides annotation-driven testing support and abstracts
the underlying testing frameworks. This framework will be discussed in Chapter 12.
Finally, note that the Spring framework is designed to be backward compatible, so it is
very easy for you to migrate your Spring 1.x applications to Spring 2.0, and also from 2.0 to 2.5.
Introducing Spring’s Projects
Spring is not only an application framework. It also serves as a platform for several open
source projects that are based on the core Spring Framework project. At the time of writing
this book, there are the following Spring Portfolio projects:
Spring IDE: This project provides Eclipse plug-ins to increase your productivity in developing Spring’s bean configuration files. Since version 2.0, Spring IDE also supports Spring
AOP and Spring Web Flow. You will learn how to install Spring IDE in this chapter.
Spring Security: This project, previously known as Acegi Security, defines a security framework for enterprise applications, especially those developed with Spring. It provides
security options for authentication, authorization, and access control for you to apply in
your applications. This framework will be discussed in Chapter 13.
Spring Web Flow: This project allows you to model complex user actions within a web
application as flows. You can develop and reuse these web flows easily with Spring Web
Flow. This will be introduced in Chapter 15.
Spring Web Services: This project focuses on the development of contract-first and
document-driven web services. It integrates many methods of manipulating XML.
Spring Web Services will be introduced in Chapter 16.
Spring Rich Client: This project establishes a framework, constructed on top of the Spring
framework, for rich GUI applications developed with Swing.
Spring Batch: This project offers a framework for batch processing in enterprise applications, focusing on processing large volumes of information.
Spring Modules: This project integrates other tools and projects as modules to extend the
Spring framework, without expanding the core Spring module.
Spring Dynamic Modules: This project supports creating Spring applications to run on the
OSGi (Open Services Gateway initiative) service platform, which allows application modules to be installed, updated, and removed dynamically.
Spring Integration: This project provides an extension to the Spring framework that supports enterprise integration with external systems via high-level adapters.
Spring LDAP: This project provides a library that simplifies LDAP operations and handles
LDAP exceptions through a template-based approach.
Spring JavaConfig: This project provides a Java-based alternative to configuring components in the Spring IoC container.
25
9799ch02.qxd
26
5/5/08
4:42 PM
Page 26
CHAPTER 2 ■ INTRODUCTION TO SPRING
Spring BeanDoc: This project helps you to generate documentation and diagrams based
on Spring’s bean configuration file.
Spring .NET: This project, as its name indicates, is a .NET version of the Spring framework
that makes .NET application development easier.
2-2. Installing the Spring Framework
Problem
You would like to develop a Java/Java EE application using the Spring framework. First of all,
you will have to install it on your local machine.
Solution
The Spring framework’s installation is very simple. You can simply download the Spring framework 2.5 release in ZIP format and extract it to a directory of your choice to complete the
installation.
How It Works
Installing JDK
Before you install the Spring framework, you should have JDK installed on your machine.
Spring 2.5 requires JDK 1.4 or higher. For this book, I strongly recommend that you install
JDK 1.5 or higher so that you can use features such as annotations, autoboxing/unboxing,
varargs, type-safe collections, and for-each loops.
Installing a Java IDE
Although an IDE is not absolutely required for developing Java applications, you should probably install one to make development easier. As Spring’s configuration is mostly based on
XML, you should choose an IDE with features that support XML editing, such as XML validation and XML auto-completion. If you do not have a favorite Java IDE or would like to choose
another one, I recommend installing Eclipse Web Tools Platform (WTP), which can be downloaded from http://www.eclipse.org/webtools/. Eclipse WTP is very easy to use and includes
many powerful Java-, web-, and XML-supporting features. Also, Spring provides a plug-in for
Eclipse called Spring IDE.
Downloading and Installing Spring
From Spring’s web site, you can choose to download the Spring framework 2.5 release. Then
you will be directed to http://sourceforge.net/ to download a Spring distribution, as shown
in Figure 2-2.
9799ch02.qxd
5/5/08
4:42 PM
Page 27
CHAPTER 2 ■ INTRODUCTION TO SPRING
Figure 2-2. Downloading a Spring distribution
As you can see from Figure 2-2, there are three distributions of the Spring framework 2.5
release that you can download. The first contains the entire Spring framework, including
Spring’s JAR files, dependent libraries, documentation, complete source code, and sample
applications. The second only contains Spring’s JAR files and documentation. The last only
contains Spring’s JAR files. Of them, I strongly recommend you download the distribution with
all dependencies—that is, spring-framework-2.5-with-dependencies.zip. Although the size of
this distribution is greater, it contains most of the libraries that you need for developing Spring
applications.
After downloading the Spring distribution, extract it to a directory of your choice. In this
book, I assume that you are using the Windows platform for development and have installed
Spring into c:\spring-framework-2.5.
Exploring Spring’s Installation Directory
If you have downloaded and installed the Spring distribution with dependencies, your Spring
installation directory should have the subdirectories listed in Table 2-1.
Table 2-1. Subdirectories in Spring’s Installation Directory
Directory
What It Contains
aspectj
Source code and test cases for Spring aspects used in AspectJ
dist
JAR files for Spring modules and weaving support, and resource files such as DTD
and XSD
docs
Spring’s API javadoc, reference documentation in PDF and HTML format, and a
Spring MVC step-by-step tutorial
lib
JAR files for Spring’s dependent libraries, organized by project
mock
Source code for Spring’s mock objects and testing support
samples
Several sample applications for demonstrating how to use Spring
src
Source code for most parts of the Spring framework
test
Test cases for testing the Spring framework
tiger
Source code, test cases, and mock objects for Spring that are specific to JDK 1.5 and
higher
27