Book Image

Spring Roo 1.1 Cookbook

Book Image

Spring Roo 1.1 Cookbook

Overview of this book

Spring Roo is an easy-to-use productivity tool for rapidly developing Java enterprise applications using well-recognized frameworks such as Spring, Hibernate, AspectJ, Spring Web Flow, Spring Security, GWT, and so on. Spring Roo takes care of creating maven-enabled projects, enterprise application architecture based on your choice of technologies, unit/integration tests based on your choice of testing framework, and so on. The bottom line is that if you're using Spring, then you must consider using Spring Roo for increased productivity. Spring Roo 1.1 Cookbook brings together a collection of recipes that demonstrate how the Spring Roo developer tool simplifies rapidly developing enterprise applications using standard technologies/frameworks such as JPA, GWT, Spring, Flex, Spring Web Flow, Spring Security, and so on. It introduces readers to developing enterprise applications for the real world using Spring Roo tool. The book starts off with basic recipes to make readers comfortable with using Spring Roo tool. As the book progresses, readers are introduced to more sophisticated features supported by Spring Roo in the context of a Flight Booking application. In a step-by-step by fashion, each recipe shows how a particular activity is performed, what Spring Roo does when a command is executed, and why it is important in the context of the application being developed. Initially, you make a quick start with using Spring Roo through some simple recipes. Then you learn how Spring Roo simplifies creating the persistence layer of an enterprise application using JPA. You are introduced to the various roo commands to create JPA entities, create relationships between JPA entities, create integration tests using Spring TestContext framework, and so on. Following this, the book shows you how Spring Roo simplifies creating the web layer of an enterprise application using Spring Web MVC, Spring Web Flow, and how to create selenium tests for controller objects. Subsequently, we focus on using Spring-BlazeDS, GWT, JSON, and so on. Spring Roo commands that are used to incorporate e-mail/messaging features into an enterprise application are demonstrated next. Finally, we wrap it up with some miscellaneous recipes that show how to extend Spring Roo via add-ons, incorporate security, create cloud-ready applications, remove Spring Roo from your enterprise application, and so on.
Table of Contents (14 chapters)
Spring Roo 1.1 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a Java class


You can create Java classes in your Roo project, either by using the IDE of your choice or by using the class command. If you create a class using Roo, boilerplate code (which includes toString , and get and setter methods for attributes) is generated automatically and managed by Spring Roo, and is kept in a separate AspectJ ITD file.

Getting ready

Start the Roo shell from the C:\roo-cookbook\ch01-recipe directory, which contains the flight-app Roo project.

How to do it...

You can create a Java class using the class command, as shown here:

roo> class --class sample.roo.flightapp.service.FlightService --rooAnnotations

Created SRC_MAIN_JAVA\sample\roo\flightapp\service
Created SRC_MAIN_JAVA\sample\roo\flightapp\service\FlightService.java
Created SRC_MAIN_JAVA\sample\roo\flightapp\service\FlightService_Roo_Serializable.aj

~.service.FlightService roo>

When the class command is executed, notice that the Roo prompt changes to refer to the newly created Java class. In the next recipe, titled Adding fields to a Java class, we will see how the changed Roo prompt simplifies performing commands on the referred class. Also, notice that the service directory is automatically created by Spring Roo, if it doesn't exist.

Tip

Some command arguments, like rooAnnotations, act as a flag for the command processor, and you don't need to specify their value. Simply specifying them as part of the command means that the value of the argument is true or yes.

How it works...

The class command accepts the arguments listed in the following table:

Argument

Purpose

class

It is a mandatory argument that identifies the fully-qualified name of the Java class that you want to create. You can either specify the fully-qualified class name using the tilde symbol '~' or you can use the TAB (or CTRL + SPACE) multiple times to let Spring Roo complete the package name for you.

The '~' symbol refers to the top-level package of the Roo project. For instance, in flight-app project, it refers to sample.roo.flightapp package. You can use this symbol to specify the package (relative to top-level package) in which you want to create your Java class.

rooAnnotations

It is an optional argument that specifies the common Roo annotations, such as @RooJavaBean, @RooToString, and @RooSerializable, which are added to the generated Java class. If unspecified, these annotations are not added to the generated Java class.

path

It is an optional argument that specifies the path to the source directory in which the class is created. By default, the path is SRC_MAIN_JAVA.

extends

It is an optional argument that specifies the fully-qualified name of the class, which the Java class extends. You can use this argument to create a class which extends from a superclass.

abstract

It is an optional argument that indicates whether the class is an abstract or concrete class. You can use this argument to create an abstract class.

permitReservedWords

It is an optional argument that indicates whether Roo should allow creating a class whose name is a reserved word. By default, Roo doesn't allow creating Java classes whose name uses reserved words. For instance, by default you cannot create a class named New.

As evident from the list of arguments accepted by the class command, Spring Roo doesn't provide any argument to let you specify the interface(s) that the generated Java class implements. If you want your Java class to implement one or more interfaces, you need to manually modify your class definition.

As the output from class command suggests, apart from FlightService.java, Roo creates a FlightService_Roo_Serializable.aj file—an AspectJ ITD that makes the FlightService class implement java.io.Serializable interface.

The AspectJ ITDs generated by Roo have the following naming convention:

<java-class-name>_Roo_<add-on-name>.aj

Where <java-class-name> is the name of the Java class to which the AspectJ ITD applies.

<add-on-name> is the name of Spring Roo add-on responsible for managing the AspectJ ITD

The *_Roo_*.aj files are managed by Roo and you should not directly modify or delete them.

The following code shows how the FlightService.java file generates the FlightService class using the class command:

package sample.roo.flightapp.service;

import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.serializable.RooSerializable;

@RooJavaBean
@RooToString
@RooSerializable
public class FlightService { }

In the given code, Roo annotations were added to the generated FlightService class because we specified the rooAnnotations argument in the class command.

To simplify debugging, developers commonly override the toString method of the java.lang.Object class to output a string containing the value of all the attributes of the class. With Spring Roo, you are relieved of this task because if your class is annotated with @RooToString annotation, Spring Roo takes care of creating and updating the toString method as you add, modify, or remove attributes from your Java class.

When you add an attribute to your FlightService class, Roo creates a FlightService_Roo_ToString.aj—an AspectJ ITD that adds the toString method to the FlightService class, and a FlightService_Roo_JavaBean.aj—an AspectJ ITD that adds getters and setters methods for the attributes defined in the FlightService class. The creation of these aspects is triggered by the presence of @RooToString and @RooJavaBean annotations in the FlightService class.

To see these two ITD files, add the following attribute to FlightService class:

private String origin;

If your Roo shell is running, as soon as you save the FlightService class, Roo will generate a FlightService_Roo_ToString.aj file and a FlightService_Roo_JavaBean.aj file in the same package as the FlightService class. If you observe the Roo shell, you will find that Roo reports that it has created a FlightService_Roo_ToString.aj and FlightService_Roo_JavaBean.aj files, as shown here:

Created SRC_MAIN_JAVA\sample\roo\flightapp\service\
FlightService_Roo_ToString.aj

Created SRC_MAIN_JAVA\sample\roo\flightapp\service\
FlightService_Roo_JavaBean.aj

The following code shows how FlightService_Roo_ToString.aj AspectJ ITD adds the toString method to the FlightService class:

package sample.roo.flightapp.service;

privileged aspect FlightService_Roo_ToString
{
    
    public String FlightService.toString()
   {
      StringBuilder sb = new StringBuilder();
      sb.append("Origin: ")
      .append(getOrigin());
      return sb.toString();
   }
}

The given code shows that FlightService_Roo_ToString is a privileged aspect, that is, it can access even private members of other aspects and classes. The declaration, public String FlightSerivce.toString(), adds a public toString method to the FlightService class that accepts no arguments and returns a String. Everything inside the curly-braces is the implementation of the toString method. Each declaration in an AspectJ ITD file identifies the target of that declaration. In the code, FlightService in the declaration means that the FlightService class is the target; therefore, it will add the toString method to the FlightService class. In the Adding fields to a Java class recipe, we will see how the toString method is automatically updated by Spring Roo when you add more attributes to the FlightService class.

The following figure summarizes how the FlightService_Roo_ToString.aj file in the previous listing declares adding the toString method to the Flight class:

Note

In Spring Roo, AspectJ ITDs are responsible for adding fields, methods, and constructors to Java classes and to make them implement interfaces or extend from a superclass. Spring Roo is responsible for managing these ITDs and you should not directly modify or delete them.

The following code shows the FlightService_Roo_JavaBean.aj AspectJ ITD file:

privileged aspect FlightService_Roo_JavaBean
{
      
  public String FlightService.getOrigin()
   {
      return this.origin;
   }
    
  public void FlightService.setOrigin
   (String origin)
   {
      this.origin = origin;
   }
}

The given code shows that FlightService_Roo_JavaBean.aj is also a privileged aspect and it introduces two methods into the FlightService class: getOrigin and setOrigin, to get and set the value of the origin attribute.

The FlightService_Roo_Serializable.aj AspectJ ITD defines that the FlightService class implements the java.io.Serializable interface, as shown here:

package sample.roo.flightapp.service;

import java.io.Serializable;

privileged aspect FlightService_Roo_Serializable
{
    
   declare parents: FlightService implements Serializable;
    
  private static final long FlightService.serialVersionUID 
   = 5059552858884348572L
}

In the given code, the declare parents: FlightService implements Serializable statement declares that the FlightService class implements the java.io.Serializable interface. The following figure summarizes what this declaration means:

The statement private static final long FlightService.serialVersionUID = 5059552858884348572L, adds a serialVersionUID field (it's the field which you define if your class implements the Serializable interface) to the FlightService class that contains it.

There's more...

If you want Roo to manage the creation of the toString method and getter and setter methods for attributes of the class, it is recommended that you use the rooAnnotations argument in the class command.

Note

Roo annotations have source-level retention, which means that your application is not dependent on Roo annotations at runtime.