About this Book

Welcome to Spring Roo in Action! If you’re reading this book, you’re looking for ways to improve your Spring development productivity.

When we started writing this book, nobody had even considered a book on Roo. The tool had been out in the public sphere for only a few months, and, after all, writing a book on any emerging technology is a crazy thing to do. But crazy things are usually tried by crazy people, and once we got started there was no turning back.

This book is your guide to juicing your Spring development productivity, using a tiny, 8-megabyte project known as Spring Roo. We start by laying the groundwork for why such a tool is important, and how Roo fills the gap between the developer productivity of Spring and the configuration morass you can get into while writing enterprise applications. The writers of this book are Spring developers, trainers, mentors, and hobbyists. We develop, train, mentor, and tinker with Spring every day, so when we saw what Roo brought to the table we realized the power it represented to the everyday developer.

Craig Walls’s Spring in Action, also published by Manning, is an excellent companion book for the new Spring developer, and is a good reference to keep nearby when you want additional information about a topic in our book.

Other good references on these topics are Spring Integration in Action and ActiveMQ in Action, also from Manning Publications.

Learning by experimenting

It’s our hope that you can read this book and get a taste of how to build a Roo application, even without running a single sample. That said, the concepts are relatively easy to grasp—using Roo’s TAB completion you can test the various commands and generate a working project with the features you’re interested in. Then you can use your editor to review the code and test it. Soon you’ll find it easy to try out new frameworks, because the feedback loop is so short.

Above all, Roo enables experimentation. Combine it with Git for version control, and you can create a branch for your new idea, try it out, and merge it back in if you like it. Of course, because branches are cheap, you can remove the branch and forget it ever happened. We encourage you to create a lot of throw-away projects with Roo.

Roadmap

Chapter 1 is a quick introduction to the Roo tool, and we get started creating applications right out of the gate. We begin by making the case for Roo and RAD on Java—how Spring makes things better, but how Roo really knocks it out of the park. We create a sample project, the Roo Pizza Shop, as a way to get you to kick the tires early, and you’ll see how little you need to do to build a full-featured database-backed web application.

Chapter 2 covers the basics of using the Roo shell, and we walk through configuring a Task Manager project, installing persistence, creating an entity, and scaffolding a web application. We then dig into the code behind the application, inter-type declarations (ITDs), the various ways to structure your projects, and using an IDE such as SpringSource Tool Suite. We then discuss how to use refactoring to push-in or pull-out code, and how to remove Roo entirely if you need to.

Chapter 3 is an introduction to database persistence in Roo. We detail the options for setting up persistence using JPA, setting up a JPA entity, using the Bean Validation framework to provide annotation-driven validations, how to use finders to write simple JPA queries, and how to create repositories using the repository command and the Spring Data API.

Chapter 4 continues the discussion of database persistence and covers relationship mappings, how to write your own JPA persistence methods, reverse engineering database tables from an existing database, adding a Spring service layer with the service command, and using MongoDB, a NoSQL database supported by Roo 1.2.

Chapter 5 introduces Spring MVC, which is the base of Roo’s primary web framework. We show you how to install the web framework and how to use scaffolding to automatically generate a simple CRUD application with only two commands. We also discuss accessing other Spring beans, and how to scaffold in a multimodule project.

Chapter 6 digs deeply into the scaffolding engine and Roo’s tag libraries. We show you how you can customize the scaffolded web views, and how to modify the way fields are displayed. We outline how to display reference data in drop-down lists, customize date fields, deal with localization and theming, and we show you how Roo uses Apache Tiles to lay out your user interfaces.

Chapter 7 switches gears to more advanced web frameworks. We start by showing you how to use Spring MVC and Dojo to provide Ajax support for your forms. We then show you how to install two other web frameworks, Google Web Toolkit and JavaServer Faces. We end by listing a few other web frameworks and the support that Roo had for them at the time we wrote the book.

Chapter 8 covers Spring Security, including how to install it, configure it against both a database data store and LDAP, set up a login page, test security, and add event logging.

Chapter 9 is our testing chapter. We cover unit testing and Mockito, mocking the persistence tier, integration testing in-container against entities, repositories and services, and how to write functional, black box tests with Selenium, both using Roo’s support for HTML table-based tests as well as using the JUnit API.

Chapter 10 discusses email and JMS, two external integration points that most developers have to work with at some point in their careers. We begin by outlining a course management system, and then lay down the JMS and email features required to support that system. We cover JMS installation, the JMS template, building a POJO listener, and testing the listener. Then we cover building email messages with an email sender, configuring SMTP support, building an email template, and hosting it behind a Spring service.

Chapter 11 is the introduction to Roo add-ons. We start by showing you how to search for publicly available add-ons and how to install and remove them. Because add-ons are OSGi components, we spend time detailing enough of OSGi to be dangerous, and then we dive right in and create three add-ons: a Norwegian language addon, a Roo wrapper add-on to expose a non-OSGi JAR to the Roo system, and a “Simple” add-on to provide jQuery support.

Chapter 12 continues our add-on discussion and provides support for CoffeeScript by creating an advanced add-on. We install the Maven plug-in for CoffeeScript compilation, build and test it, and show you how to detect the availability of both adding and removing the feature from your project. We then wrap up the discussion by detailing how to publish and submit your add-on to the add-on community.

Chapter 13 shows you how to use cloud computing to host your Roo applications. We discuss some of the platforms, including CloudBees and Heroku, and then focus on using Cloud Foundry, a VMware hosting offering. We deploy the Course Manager application to the cloud and show how to fetch application statistics, as well as how to bind cloud resources to the application.

Chapter 14 details how to use Spring Integration from a Roo project. We discuss event-driven application architectures, how to add a workflow to handle course registration, and how to build and install the Roo integration add-on from source, because it’s not yet released for Roo 1.2.

Things you’ll need

To follow along with the book, you’ll need to download and install Spring Roo, version 1.2.1, from http://springsource.org/spring-roo. We cover installation in chapter 1.

You’ll also need an IDE; for the new Spring developer, we suggest using Spring-Source Tool Suite. Gordon has written an STS RefCard that can be downloaded free (note: registration required) from http://refcardz.dzone.com/refcardz/eclipse-toolsspring. This special version of Eclipse is fully configured to develop Spring-based applications, and can be configured to use your Roo shell.

If you’re partial to IntelliJ IDEA, you can download version 10.5 or higher, though we recommend at least version 11. IntelliJ is an excellent alternative IDE, and provides support for many of the same features as SpringSource Tool Suite, the key omission being an integrated copy of the Spring tc Server web application server, which comes bundled with STS.

You’ll also need to install Maven 3.0.3 or higher, because Roo projects are Maven projects. If you’re going to write your own add-ons, you’ll need to install GPG, an open source encryption provider. To make these add-ons available to the public, you’ll want to install Git and/or Subversion (SVN) to deliver your add-ons to public repositories hosted by Google Code, GitHub, or other places where the Roo team can access and index your add-on.

Notes on earlier versions of Roo

Users of earlier versions of Roo will need to make some adjustments in their shell commands, and the classes will look notably different.

In earlier versions of Roo, the only persistence mechanism is via the Roo Active Record pattern. Only Roo 1.2 and later will provide the service and repository commands, which set up layered Spring application objects. This is a topic which we discuss in chapters 3 and 4. Also, earlier versions of the persistence framework configuration use a persistence setup command, which has changed to the newer jpa setup in light of support for configuring non-SQL databases.

Roo 1.2 introduces the concept of multimodule projects. Roo 1.1 and below have no such features.

The add-ons chapters are compile-time incompatible with versions of Roo earlier than 1.2.1, because the framework has undergone significant refactoring between versions 1.0, 1.1, 1.2, and 1.2.1. Expect additional changes for the better in future versions. Concept-wise, the chapters hold up—the concept behind simple and advanced add-ons is the same; but the individual beans, interfaces, and techniques will vary.

NoSQL database support is new in 1.2, and database reverse engineering is new as of Roo 1.1.

The official Roo documentation discussed upgrading a Roo project. We’ve found the best course of action is to perform the upgrade, but then create a brand new scratch Roo project with the features you’re using, and diff the pom.xml file to make sure that you’ve been properly upgraded to the most recent version. Refer to the Roo documentation for details for each official release.

Code conventions

We use specially formatted code in non-proportional type to convey symbols, commands, and fragments of source code. Roo (and Spring) make it hard to fit code on single lines, due to the fact that Spring developers are long-name happy (consider one of the longer class names, ClassPathXmlApplicationContext, to see what gave us many headaches when formatting our listings).

If you see the line continuation character, it means that the command you’re typing is required to fit on a single line, or that the code we’ve reformatted was meant to exist on a single line. For example:

roo> project --topLevelPackage org.foo.bar.long.project.package --projectName thebigprojectname

We occasionally use the continuation character to show a long line in a generated artifact as well. This is shown for completeness. Other conventions:

  • All code is listed in a Courier font.
  • We use Courier to highlight various commands, such as web mvc setup.
  • We skip long lists of Java import statements and nonessential source code fragments to illustrate key features.
  • We use bold code font to emphasize some areas of code examples to show important points.
  • We use italic font for emphasis and to detail new terms.
  • Code annotations are used instead of comments in code samples. Where comments are used, they appear in the code sample as a numbered bullet, and may have corresponding discussion points in the manuscript below the sample.

Source code

The source code for Roo in Action is available at http://github.com/krimple/springroo-in-action-examples. You can also find links to the source code repository and a post-publication errata list on the Manning page for this book, http://manning.com/SpringRooinAction.

As the Roo project progresses rapidly, we’ve constantly been reworking our examples and upgrading them before publication of the book. If you find a problem with the samples, please log a bug with the project by creating a GitHub account and clicking on the Issues tab. All samples are tested with Roo 1.2.1.

We’ll also be taking contributions of example code to share with our readers—contact us via GitHub with pull requests to the user-contrib directory and we’ll review them. Assume that your samples will be available for use by the public Roo user community, and that the code should be freely contributed without additional restrictive source licenses. Any contributions are welcomed by the reader community, so feel free to lend your expertise.

Author Online

The purchase of Spring Roo in Action includes free access to a private forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the authors and other users. You can access and subscribe to the forum at www.manning.com/SpringRooinAction. This page provides information on how to get on the forum after you’re registered, what kind of help is available, and the rules of conduct in the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue among individual readers and between readers and authors can take place. It’s not a commitment to any specific amount of participation on the part of the authors, whose contribution to the book’s forum remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions, lest their interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.131.212