ebook img

Spring Recipes: A Problem-Solution Approach PDF

848 Pages·2014·9.89 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Spring Recipes: A Problem-Solution Approach

C Fr S ov BOOKS FOR PROFESSIONALS BY PROFESSIONALS® ampriers e n wo g r k 4. Deinum 0 Rubio Long Mak Spring Recipes RELATED Spring Recipes: A Problem-Solution Approach, Third Edition builds upon the best-selling success of the previous editions and focuses on the latest Spring Framework features for building enterprise Java applications. This book provides code recipes for the following, found in the latest Spring: • Spring fundamentals: Spring IoC container, Spring AOP/ AspectJ, and more • Spring enterprise: Spring Java EE integration, Spring Integration, Spring Batch, Spring Remoting, messaging, transactions, and working with big data and the cloud using Hadoop and MongoDB • Spring web: Spring MVC, other dynamic scripting, integration with the popular Grails Framework (and Groovy), REST/web services, and more This book guides you step-by-step through topics using complete and real-world code examples. When you start a new project, you can consider copying the code and configuration files from this book, and then modifying them for your needs. This can save you a great deal of work over creating a project from scratch! You’ll learn: • How to develop with the core Spring Framework, aspect oriented programming, dependency injection, and inversion of control • How to create Spring web services using Spring REST, SOAP and remoting • How to build a web client using Spring MVC and how to work with other web frameworks • How to use Grails and Groovy • How to integrate Spring with social media and mobile apps • How to work with Spring Data, Batch, NoSQL and big data; then integrating with Hadoop, MongoDB, Redis and more • How to create and manage Spring transactions • How to handle Spring messaging, integration, testing, and caching • How to secure your Spring applications US $49.99 Shelve in ISBN 978-1-4302-5908-4 Programming Languages/Java 54999 User level: THIRD Intermediate EDITION SOURCE CODE ONLINE 9781430259084 www.apress.com For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. Contents at a Glance About the Authors ����������������������������������������������������������������������������������������������������������xxxiii About the Technical Reviewer ����������������������������������������������������������������������������������������xxxv Acknowledgments ��������������������������������������������������������������������������������������������������������xxxvii Introduction �������������������������������������������������������������������������������������������������������������������xxxix ■ Chapter 1: Spring Development Tools ��������������������������������������������������������������������������������1 ■ Chapter 2: Spring Core Tasks ������������������������������������������������������������������������������������������47 ■ Chapter 3: Spring Annotation Driven Core Tasks �����������������������������������������������������������135 ■ Chapter 4: Spring @MVC �����������������������������������������������������������������������������������������������217 ■ Chapter 5: Spring REST �������������������������������������������������������������������������������������������������275 ■ Chapter 6: Spring Social ������������������������������������������������������������������������������������������������303 ■ Chapter 7: Spring Security ��������������������������������������������������������������������������������������������331 ■ Chapter 8: Spring Mobile �����������������������������������������������������������������������������������������������385 ■ Chapter 9: Spring with Other Web Frameworks ������������������������������������������������������������401 ■ Chapter 10: Data Access �����������������������������������������������������������������������������������������������419 ■ Chapter 11: Spring Transaction Management ���������������������������������������������������������������475 ■ Chapter 12: Spring Batch ����������������������������������������������������������������������������������������������511 ■ Chapter 13: NoSQL and BigData ������������������������������������������������������������������������������������549 ■ Chapter 14: Spring Java Enterprise Services and Remoting Technologies �������������������591 ■ Chapter 15: Spring Messaging ��������������������������������������������������������������������������������������659 v ■ Contents at a GlanCe ■ Chapter 16: Spring Integration ��������������������������������������������������������������������������������������691 ■ Chapter 17: Spring Testing ��������������������������������������������������������������������������������������������723 ■ Chapter 18: Grails ����������������������������������������������������������������������������������������������������������757 Index ���������������������������������������������������������������������������������������������������������������������������������799 vi Introduction The Spring framework is growing. It has always been about choice. Java EE focused on a few technologies, largely to the detriment of alternative, better solutions. When the Spring framework debuted, few would have agreed that Java EE represented the best-in-breed architectures of the day. Spring debuted to great fanfare, because it sought to simplify Java EE. Each release since marks the introduction of new features designed to both simplify and enable solutions. With version 2.0 and later, the Spring framework started targeting multiple platforms. The framework provided services on top of existing platforms, as always, but was decoupled from the underlying platform wherever possible. Java EE is a still a major reference point, but it’s not the only target. Additionally, the Spring framework runs on different Cloud environments. With the introduction of Java based configuration and more XML schemas, the Spring framework created powerful configuration options. Frameworks built on top of the Spring framework have emerged supporting application integration, batch processing, messaging, and much more. This is the 3rd edition of the superb Spring Recipes and it contains mostly updated frameworks, describing the new features and explaining the different configuration (Java and/or XML) options. Additionally, new projects have been added to the Spring ecosystem like the Spring Data family of products. It was impossible to describe each and every project in the Spring ecosystem, so we had to decide what to keep, what to add, and what to update. This was a hard decision but we think we have included the most important and used content. Who This Book Is For This book is for Java developers who want to simplify their architecture and solve problems outside the scope of the Java EE platform. If you are already using Spring in your projects, the more advanced chapters present discussions of newer technologies that you might not know about already. If you are new to the framework, this book will get you started in no time. This book assumes that you have some familiarity with Java and an IDE of some sort. While it is possible, and indeed useful, to use Java exclusively with client applications, Java’s largest community lives in the enterprise space and that, too, is where you’ll see most of these technologies deliver the most benefit. Thus, some familiarity with basic enterprise programming concepts like the Servlet API is assumed. How This Book Is Structured Chapter 1, “Spring Development Tools”, gives an overview of tools supporting the Spring framework and how to use them. Chapter 2, “Spring core tasks,” gives a general overview of the Spring framework: how to set it up, what it is, and how it’s used. Chapter 3, “Spring Annotation Driven Core Task,” reviews, in addition to Chapter 2 more annotation driven concepts that are still key to fully exploiting the container. Chapter 4, “Spring @MVC,” covers web-based application development using the Spring Web MVC framework. Chapter 5, “Spring REST,” provides an introduction to Spring’s support for RESTful web services. Chapter 6, “Spring Social,” provides an introduction of Spring Social, which lets you integrate easily with social networks. Chapter 7, “Spring Security,” provides an overview of the Spring Security project, to help you better secure your application. Chapter 8, “Spring Mobile,” provides an introduction of Spring Mobile, which lets you integrate Mobile device detection and usage in your application. xxxix ■ IntroduCtIon Chapter 9, “Integrating Spring with Other Web Frameworks,” introduces the core web-tier support that Spring provides. This provides a base for all technologies that Spring provides in the web tier. Chapter 10, “Data Access,” discusses using Spring to talk to data stores using APIs like JDBC, Hibernate, and JPA. Chapter 11, “Transaction Management in Spring,” introduces the concepts behind Spring’s robust transaction management facilities. Chapter 12, “Spring Batch,” introduces the Spring Batch framework, which provides a way to model solutions traditionally considered the domain of mainframes. Chapter 13, “NoSQL and BigData,” an introduction to multiple Spring Data portfolio projects, covering different NoSQL technologies and BigData with Hadoop. Chapter 14, “Spring Java Enterprise Services and Remoting Technologies,” introduces you to the JMX support, scheduling, e-mail support, and various facilities for RPC, including the Spring Web Services project. Chapter 15, “Spring Messaging,” discusses using Spring with message-oriented middleware through JMS and RabbitMQ and the simplifying Spring abstractions. Chapter 16, “Spring Integration,” discusses using the Spring Integration framework to integrate disparate services and data. Chapter 17, “Spring Testing,” discusses unit testing with the Spring framework. Chapter 18, “Grails,” discusses the Grails framework, with which you can increase your productivity by using best- of-breed pieces and gluing them together with Groovy code. Appendix A, “Deployment to the Cloud,” shows how to deploy a Java (Web) application to the cloud using the Pivotals CloudFoundry solution. Appendix B, “Spring Caching,” introduces the Spring Caching abstraction, including how to configure it and how to transparently add caching to your application. Conventions Sometimes, when we want you to pay particular attention to a part within a code example, we will make the font bold. Please note that the bold doesn’t necessarily reflect a code change from the previous version. In cases when a code line is too long to fit the page’s width, we will break it with a code continuation character. Please note that when you try to type the code, you have to concatenate the line by yourself without any spaces. Prerequisites Because the Java programming language is platform independent, you are free to choose any supported operating system. However, some of the examples in this book use platform-specific paths. Translate them as necessary to your operating system’s format before typing the examples. To make the most of this book, install JDK version 1.7 or higher. You should have a Java IDE installed to make development easier. For this book, the sample code is Gradle based. If you’re running Eclipse and Install the Gradle plug-in, you can open the same code in Eclipse and the CLASSPATH and dependencies will be filled in the by the Gradle metadata. If you’re using Eclipse, you might prefer SpringSource’s SpringSource Tool Suite (STS), as it comes preloaded with the plug-ins you’ll need to be productive with the Spring framework in Eclipse. If you use IntelliJ IDEA, you need to enable the Gradle (and Groovy) plugins. Downloading the code The source code for this book is available from the Apress web site (www.apress.com) in the Source Code / Download section. The source code is organized by chapters, each of which includes one or more independent examples. Contacting the Authors We always welcome your questions and feedback regarding the contents of this book. You can contact Marten Deinum at [email protected] xl Chapter 1 Spring Development Tools In this chapter you’ll learn how to setup and work with the most popular development tools to create Spring applications. Like many other software frameworks, Spring has a wide array of development tools to choose from, from bare-bones command line tools to sophisticated graphical tools the software industry calls IDEs (Integrated Development Environments). Whether you already use certain Java development tools or are a first-time developer, the following recipes will guide you on how to set up different toolboxes to do the exercises in the upcoming chapters, as well as develop any Spring application. Table 1-1 describes a list of toolboxes and the corresponding recipes you need to follow to get the right tools to start a Spring application. Table 1-1. Toolboxes to develop Spring applications Toolbox A Toolbox B Toolbox C Toolbox D Spring Tool Suite Eclipse IDE IntelliJ IDE Text editor Recipe 1-1 Recipe 1-2 Recipe 1-3 w/Maven CLI Recipe 1-4 w/Maven CLI Recipe 1-4 w/Gradle CLI Recipe 1-5 w/Gradle CLI Recipe 1-5 Bear in mind you don’t need to install every toolbox described in Table 1-1 to work with Spring. It can be helpful to try them all out, so you can use the toolbox you feel most comfortable with. 1-1. Build a Spring application with the Spring Tool Suite Problem You want to use the Spring Tool Suite (STS) to build a Spring application. Solution Install STS on your workstation. Open STS and click the ‘Open Dashboard’ link. To create a new Spring application, click on the ‘Spring project’ link on the dashboard window inside the ‘Create’ table. To open a Spring application that uses Maven, from the top level ‘File’ menu select the ‘Import...’ option, click on the ‘Maven’ icon and select the ‘Existing Maven projects’. Next, select the Spring application based on Maven from your workstation. 1 Chapter 1 ■ Spring Development toolS To install Gradle on STS, click on the ‘Extensions’ tab at the bottom of the dashboard window. Click on the ‘Gradle Support’ checkbox. Proceed with the Gradle extension installation and restart STS once the installation is complete. To open a Spring application that uses Gradle, from the top level ‘File’ menu select the ‘Import...’ option, click on the ‘Gradle’ icon and select the ‘Gradle project’. Next, select the Spring application based on Gradle from your workstation. Click on the ‘Build Model’ button and last click ‘Finish’ to start working on the project. How It Works STS is the IDE developed by SpringSource -- a division of Pivotal -- creators of the Spring framework. STS is specifically designed to develop Spring applications, making it one of the most complete tools for this purpose. STS is an Eclipse-powered tool, so it has the same ‘look and feel’ as the Eclipse open source IDE. STS can be downloaded for free from http://spring.io/tools/sts. STS is available for all six major operating system (OS) versions: Windows, Windows (64bit), Mac OS X (Cocoa), Mac OS X (Cocoa, 64bit), Linux (GTK), and Linux (GTK, 64bit). STS is also distributed in two versions of Eclipse, the Eclipse 3.x branch and the Eclipse 4.x branch. In addition, STS is itself versioned, so you have the option to download the latest stable release or a milestone/development version. Download the version suited to your OS and I recommend you chose the Eclipse 4.x branch because it’s newer. At the time of this writing the latest stable release of the Spring Tool Suite is the 3.5 version. Therefore the download link you chose should have the title ‘SPRING TOOL SUITE 3.5.1.RELEASE - BASED ON ECLIPSE 4.3.2’ or you can choose a newer release if you prefer. Once you download STS, ensure you have a Java SDK installed on your system since this is an STS installation requirement. Proceed to install STS. Follow the installation wizard and you should have STS setup in 5 to 10 minutes. Upon termination, a folder with the name STS_<VERSION> is created under the home folder of the user making the installation or where the user chooses to place the installation-based folder. If you inspect this folder, you’ll see the STS executable which is used to start STS. Start STS. At startup, STS asks you to define a workspace location. A workspace is where STS places all project information. You can keep the default directory which is under the main STS installation directory or define a different directory to your liking. After startup is complete you’ll see a screen like the one in Figure 1-1. Figure 1-1. STS startup screen 2 Chapter 1 ■ Spring Development toolS Click on the ‘Open Dashboard’ link. Then you’ll see the STS Dashboard illustrated in Figure 1-2. Figure 1-2. STS Dashboard On the STS Dashboard, in the center column inside the ‘Create’ box there’s a link called ‘Spring project’. You can click on this link to create a new Spring application. You can go ahead and create an empty application if you like. You’ll be asked for a name and to define a series of parameters which you can leave with default values. A more common case than creating a Spring application from scratch is to continue development on a pre-existing Spring application. Under such circumstances, the owner of an application generally distributes the application’s source code with a build script to facilitate its ongoing development. The build script of choice for most Java application is a pom.xml file designed around the build tool called Maven and more recently a build.gradle file designed around the build tool called Gradle. The book’s source code and its applications are provided with Gradle build files, in addition to a single application with a Maven build file. JaVa BUILD tOOLS, a MeaNS tO aN eND: aNt, MaVeN, IVY, GraDLe in a Java application there can be dozens or hundreds of menial tasks required to put together an application (e.g., Copying Jars or configuration files, setting up Java’s classpath to perform compilation, downloading Jar dependencies, etc.). Java build tools emerged to perform such tasks in Java applications. Java build tools continue to have their place because applications distributed with build files ensure that all menial tasks intended by the creator of an application are replicated exactly by anyone else using the application. if an application is distributed with an ant build.xml file, a maven pom.xml file, an ivy ivy.xml file or a gradle build. gradle file, each of these build files guarantees build consistency across users and different systems. 3 Chapter 1 ■ Spring Development toolS Some of the newer Java build tools are more powerful and enhance the way their earlier counterparts work and each build file uses its own syntax to define actions, dependencies, and practically any other task required to build an application. however, you should never lose sight of the fact that a Java build tool is just a means to an end. it’s a choice made by the creator of an application to streamline the build process. Don’t panic if you see an application distributed with a build file from the oldest ant version or the newest gradle version, from an end user perspective all you need to do is download and install the build tool to create the application as its creator intended. Since many Spring applications continue to use Maven and some of the newer Spring applications use Gradle, I’ll describe the import process into STS for both types of projects. Once you download the book’s source and unpack it to a local directory, click on the STS top level ‘File’ menu and select the ‘Import...’ option. A pop-up window appears. In the pop-up window, click on the ‘Maven’ icon and select the ‘Existing Maven Projects’ option as illustrated in Figure 1-3. Figure 1-3. STS maven import 4

Description:
Spring Recipes: A Problem-Solution Approach, Third Edition builds upon the best-selling success of the previous editions and focuses on the latest Spring Framework features for building enterprise Java applications. This book provides code recipes for the following, found in the latest Spring:Spring
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.