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:
Remove the
toString
method and add the@RooToString
annotation to all your existing classes.Remove the implementation of
Serializable
interfaces from classes and instead annotate the classes with the@RooSerializable
annotation.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