Part 1. Starting Spring apps rapidly with Roo
Spring Roo is an excellent framework for the rapid development of Spring-based Java applications. With a simple command-line
shell, it can create and manage Spring applications, adding and configuring components in all of the application architecture
layers from SQL to URL, so to say.
We start exploring Roo with chapter 1, “What is Spring Roo?” explaining how Roo works and how it helps with creating the various configuration files required for
a typical Spring application. You’ll also learn how to install and launch the Roo shell. We’ll look at a simple application
by running one of the sample scripts provided in the Roo installation package.
Chapter 2, “Getting started with Roo,” will show you how to create applications from scratch with the Roo shell. You’ll also learn
the details of Roo project layout and architecture. We discuss one of the new concepts Roo introduces, called AspectJ ITDs that plays an important role in the overall Roo architecture. As developers, you need tools to take advantage of new technologies
and frameworks. Roo comes with integration in the form of the SpringSource Tool Suite (STS) IDE tool, which is also discussed
in this chapter. We wrap up the chapter with a discussion on refactoring Roo code and leaving Roo behind if, for some reason,
you want to remove Roo from your project.
Chapter 1. What is Spring Roo?
This chapter covers - The challenges of Enterprise Java
- The Spring Framework
- Roo simplifies it all
- A sample Roo project
You’re about to be introduced to a powerful new tool that makes your life as a Java application developer less stressful and more productive. That tool is Spring Roo. With a simple command-line shell, Roo can create and manage Spring-based applications, adding and configuring features such as the Java Persistence API (JPA), the Java Message Service (JMS), email, and Spring Security. Roo generates Spring MVC web applications. These allow you to manage and edit your database data, configure tests using frameworks such as JUnit and Selenium, and choose from a variety of ORM APIs and databases. At the same time, Roo reduces the amount of code written by you and rolls out efficient, customizable generated code.
In this chapter, we discuss the challenges of working with Enterprise Java applications, create a sample application with the Roo shell, and take a quick tour of Roo’s features. We then review the Roo sample script file and discuss the architectural models available to you when crafting your application.
By the end of this chapter, you’ll see how Roo helps you get rid of much of the tedium Java EE application development demands. You’ll see that you can gain much of the productivity available in dynamic-language, convention-over-configuration platforms such as Ruby on Rails and Grails. This all comes without sacrificing the benefits of Java compile-time type safety, compiled code, and debuggability.
Let’s begin our journey by discussing one of the major reasons why Java-based development projects sputter: the complexity of configuring an application architecture.
1.1. Configuration is a burden
Putting together a Java-based application can be a difficult task these days. Where do you start? There are many things to consider: build scripts, dependency management, architectural patterns, framework selections, database mapping strategies, and much more.
In traditional Enterprise Java development efforts, architects pull together a hodgepodge of open source technologies and standards-driven platforms such as JDBC, the Servlet and JavaServer Pages (JSP) APIs, and Enterprise JavaBeans (EJB) using a build tool such as Ant or Maven. If they’re more advanced, they’ll use an application framework such as Struts and EJB, Seam, or Spring. Many starting templates, IDE shortcuts, and architectural and/or design patterns can be used to make development more efficient, but most of the work involves a lot of coding, physical build configuration, and design work.
Contrast this with your friends programming in dynamic language platforms, such as Ruby on Rails or Grails, who take about 15 minutes to get a basic application shell up and running. That shell usually includes a web application, database data and validation, and some basic business and navigation logic. Unlike a lot of Java application prototypes, theirs is usually the beginning of the final project, whereas Java developers have to try and throw away a fair number of APIs and platforms manually until they get to a point where they’re comfortable assigning a team to the project. The bottom line is that it just takes a long time to write a Java-based application.
1.1.1. Spring reduces the pain
The Spring Framework, a development platform that uses interface-driven development, dependency injection, aspect-oriented programming, and a number of helper APIs and services, significantly reduces the complexity of your Enterprise Java code. If you haven’t heard of Spring by now, we strongly suggest you put this book down and read up on the framework before you dig deeply into Roo. Craig Walls’ excellent Spring in Action is a great companion to this book.
Spring operates on the principle of making your development less about busy work and more about writing business logic. Spring application developers define interface-driven beans that are then implemented as Plain Old Java Objects (POJOs) and mounted in a Spring container using XML, annotations, or Java-based configuration directives.
Here’s an example business interface:
package org.rooinaction.coursemanager.services; ... public interface CourseManager { void addCourse(Course c); List<Course> getAllCourses(); }
Spring developers then create an implementation class:
package org.rooinaction.coursemanager.services; ... public class CourseManagerDefaultImpl { public void addCourse(Course c) { // some logic here } public List<Course> getAllCourses() { // retrieve logic here } }
One way you can use this bean is to autowire it as shown next:
package org.rooinaction.coursemanager.web; ... public class CourseInputComponent { @Autowired private CourseManager courseManager; public void createACourse(String name) { Course c = new Course(); c.setName(name); courseManager.addCourse(c); } }
We’re leaving out the configuration here, but the basic idea is a simple one: let Spring find and mount your component, deal with it at the interface level, and just write a POJO both to expose and use your component.
You can use any Java API you can think of inside a Spring application, but you’re still required to add dependent JAR files and configuration to simplify the programming tasks later.
1.1.2. Shifting from code to configuration
Even Spring can’t save you from all of the tedium involved in building an application. There are many decisions that you need to make, as shown in figure 1.1.
Figure 1....