Introduction to Spring Roo in this series "Spring Roo", part 3rd: The add-on component of the development spring Roo discusses the Spring Roo add-on architecture, and how to create an internationalized, simple add-on with the addon create command. This article focuses on the remaining two types of additional components supported by Spring Roo, namely, advanced add-on components and wrapper add-ons. It is recommended that you read the articles in part 3rd before you begin reading this article.
Introduction to Advanced add-on components
The advanced add-on allows Spring Roo to perform everything that simple add-ons can do, such as updating Maven POM files with dependencies or plug-ins, updating or adding configuration files, enhancing existing Java types, and introducing new Java types using AspectJ ITD. The ability to add source code makes advanced add-on components more powerful than all other add-ons. Before you create a spring Roo advanced add-on, look at the existing advanced add-on components that spring Roo provides.
Advanced add-on components in use
One of the advanced add-on components currently in use is JPA, which can perform work related to persistence by adding support to the database and creating new entities. To view the operation of this component, open the Roo shell and execute this command in Listing 1. In this article, I'm using the Spring Roo v1.2.0.m1.
Listing 1. JPA Example
Project--
toplevelpackage com.dw.demo--projectname entity-demo
JPA setup--database Firebird HIBERNATE
entity--class ~.domain. Book
Both the JPA Setup and Entity commands are equivalent to a high-level add-on called ORG.SPRINGFRAMEWORK.ROO.ADDON.JPA. The output of the JPA Setup and Entity commands on the Roo shell allows for explicit partitioning of simple additional components and advanced add-ons. Listing 2 shows the output of the JPA Setup command.
Listing 2. Output of the JPA Setup command
Created src_main_resources/meta-inf/spring/database.properties
Updated root/pom.xml [added dependencies ...]
Updated src_main_resources/meta-inf/spring/applicationcontext.xml
Created src_main_resources/meta-inf/ Persistence.xml
The output of the JPA Setup command shows that it is performing configuration functions, such as adding dependencies in Pom.xml, updating the Spring applicationcontext.xml, and creating persistence.xml files that are specific to persistence. Suppose the JPA Setup command is equivalent to a simple add-on because it does not create or update Java source code. Use a simple add-on component in a scenario similar to the settings shown above.
Listing 3 shows the output of the entity command.
Listing 3. Output of the Entity command
Created src_main_java/com/dw/demo/domain
Created src_main_java/com/dw/demo/domain/book.java
Created src_ Main_java/com/dw/demo/domain/book_roo_configurable.aj
Created SRC_MAIN_JAVA/COM/DW/DEMO/DOMAIN/BOOK_ROO_JPA _entity.aj
Created src_main_java/com/dw/demo/domain/book_roo_entity.aj
Created Src_main_java/com/dw/demo /domain/book_roo_tostring.aj
This output shows the creation of a Java file named Book.java and four *.aj files. The golden rule for identifying advanced add-ons is to generate Java files and/or *.aj files, just like the entity command. These *roo_*.aj files are called Inter-type declarations (Inter-type Declarations, ITD). ITD allows one type (one aspect) to declare another type, that is, you can modify any type of static structure by adding methods and fields or by changing their type hierarchies. Roo uses ITD as the code Generation widget and manages its entire lifecycle. ITD allows Roo to generate code in a separate compilation unit, but cannot combine them into the same compilation class.
After viewing the output of the entity command, consider how you can generate these artifacts (. java and. aj files) through the Spring Roo. See the example of a Book.java file in Listing 4.
Listing 4. Book.java file
Package com.dw.demo.domain;
Import org.springframework.roo.addon.entity.RooEntity;
Import Org.springframework.roo.addon.javabean.RooJavaBean;
Import org.springframework.roo.addon.tostring.RooToString;
@RooJavaBean
@RooToString
@RooEntity Public
class book {
}
In addition to the annotations in the class, the Java file looks normal. Look at the names of the notes and the. aj file, and obviously some of the comments are equivalent to the functions added to the. aj file. For example, rootostring corresponds to a Book_roo_tostring.aj file and adds the ToString () method. Rooentity is equivalent to book_roo_entity AJ, Book_roo_jpa_entity, and some methods associated with persistence. Let's put the rest of the stuff aside for a while. To learn how to generate ITD with annotations, first understand how Spring Roo provides advanced add-on functionality.
When the Roo shell starts, it scans all classes and registers all classes that implement the Commandmarker interface. The Commandmarker interface tells the Roo that these classes will define the commands that the additional component can execute.
All of these advanced add-ons will register their services with the OSGi runtime provided by Spring Roo. These services specify the conditions that trigger code generation. For all advanced add-on components, the trigger point is a comment. For example, if the Java type has rootostring annotations, only the advanced additional components generated for the toString () method will be triggered. This situation also applies to other annotations.
Once the entity--class ~.domain is used. Book, the additional component creates a annotated Java file named Book.java. Additional components are triggered when Java classes have these annotations or when they have a. aj file written for them.
When you create your own advanced add-on components, you will see more relevant instructions.
The ORG.SPRINGFRAMEWORK.ROO.ADDON.JPA add-on is just an example of the advanced add-on provided by the Spring Roo. Other advanced add-on components include GWT, controller, JSON, and more. The Spring Roo 1.2.0 release also contains two more advanced add-on components, namely, addon-equals and ADDON-JSF. The Addon-equals add-on provides the implementation of an entity's equals and Hashcode methods, and ADDON-JSF provides JSF support in the Spring Roo application. To play the latest spring Roo snapshot, build the spring Roo code or download the daily snapshot from the Spring Roo repository.
Include the CompareTo () method in my Entity Class
Value objects or entities are usually necessary to implement the Java.lang.Comparable interface, and they also provide implementations of the CompareTo () method. The comparable interface is completely sorted on the objects of each class that implements it. When you implement comparable, you can perform the following actions:
Call Java.util.Collections.sort and Java.util.Collections.binarySearch
Call Java.util.Arrays.sort and Java.util.Arrays.binarySearch
Use an object as a key in Java.util.TreeMap
To use an object as an element in Java.util.TreeSet
In this article, you will build an advanced additional component that provides an implementation of CompareTo () for the entities you create in your application. Because you want to add Java code to your application, you must create an advanced add-on component.