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

Moving existing Spring projects to use Spring Roo


If you are moving your existing Spring-based project to Roo, you can make out from this recipe that you should do the following:

  1. Remove the toString method and add the @RooToString annotation to all your existing classes.

  2. Remove the implementation of Serializable interfaces from classes and instead annotate the classes with the @RooSerializable annotation.

  3. Remove getters and setters methods from your Java classes and instead annotate the classes with the @RooJavaBean annotation.

Tip

Registering the service class with Spring's application context

Using Spring Roo you can't create a service class, which is automatically registered with Spring's application context; therefore, if you want your service class to be auto-registered, then annotate it with the @Service annotation. The service class will be registered with Spring's application context as long as it is inside the top-level directory (for more information refer to the <component-scan> element, described in the Creating a Roo project recipe).

@RooToString—customizing the name of the toString method

We saw that using the @RooToString annotation creates a method named toString in the corresponding AspectJ ITD file. You can use the toStringMethod attribute of the @RooToString annotation to specify a custom name for the toString method, as shown here:

@RooToString(toStringMethod = "myTostring")
public class MyCustomClass { private String myAttr; }

In the given code, the toStringMethod attribute specifies myToString as the name of the method to act as the toString method for the MyCustomClass. The ITD file corresponding to the @RooToString annotation: MyCustomClass_Roo_ToString.aj will now create a method similar to toString but with the name myToString, as shown here:

privileged aspect MyCustomClass_Roo_ToString
{
   public String MyCustomClass.mytostring ()
   {
      StringBuilder sb = new StringBuilder();
      sb.append("MyAttr: " ).append(getMyAttr());
      return sb.toString();
   }
}

@RooToString—excluding properties from the toString method

In some cases, you may want to restrict properties from being part of the auto-generated toString method. The @RooToString annotation provides an excludeFields attribute, which lets you specify an array of attributes that should be excluded from the auto-generated toString method, as shown here:

@RooToString(excludeFields={"someAttribute"})
public class MyCustomClass { .. }

In this code, the @RooToString annotation instructs that the toString method of the MyCustomClass class must not include the someAttribute property.

See also

  • The Adding attributes to a Java class recipe explains how you can add attributes to a Java class using roo

  • The Creating a Java interface recipe explains how you can create a Java interface from the Roo shell