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

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

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