Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Spring Roo 1.1 Cookbook

You're reading from   Spring Roo 1.1 Cookbook Over 60 recipes to help you speed up the development of your Java web applications using the Spring Roo development tool

Arrow left icon
Product type Paperback
Published in Sep 2011
Publisher Packt
ISBN-13 9781849514583
Length 460 pages
Edition 1st Edition
Arrow right icon
Toc

Table of Contents (14) Chapters Close

Spring Roo 1.1 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Getting Started with Spring Roo 2. Persisting Objects Using JPA FREE CHAPTER 3. Advanced JPA Support in Spring Roo 4. Web Application Development with Spring Web MVC 5. Web Application Development with GWT, Flex, and Spring Web Flow 6. Emailing, Messaging, Spring Security, Solr, and GAE 7. Developing Add-ons and Removing Roo from Projects 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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image