Wednesday, August 13, 2014

Spring Roo

Tutorial build on Spring Roo version 1.0.2

Spring Roo is not a new language or a framework. It's an environment which makes developers productive. Roo sounds similar to Rooh in Hindi, which means soul. The soul is not visible but has a bearing on, every action and thought of an individual. Similarly Spring Roo transparently takes care of development environment and makes the development faster. Roo does leads to some artifacts generation, but that has no bearing on the run time behavior of the application.

Let's walk through a typical process of using Roo and the things will be more clear. First of all download Roo from one of the download sites (of Spring source) and unzip Roo at a location. Put the path of bin of Roo to the path of your OS. For example in Windows, if Roo is downloaded and unzipped at C:\\ProgramFiles\Roo than put C:\\ProgramFiles\Roo\bin in the PATH variable of the environment. Linux users can do the similar thing for their environment. Also make sure that maven is present and configured in your environment.

Let's now make a project using roo. Let's say we want to make a simple application of managing user details. Roo will help us in building a simple CRUD application. We will also see how roo can integrated with eclipse IDE, so we will make the project in an eclipse workspace and will ask roo to add details so that the project can be identified by eclipse. In the workspace, make a directory userManagement, and navigate in it via a command prompt (the equivalent in Linux is shell) and issue the command


You will be greeted with a roo prompt on the command prompt (hence forth we will call it as cmd).

Just for completeness, let's assume the table for UserDetails that we will work with



Now to create the project issue the following command

project --topLevelPackage com.lalit.userMgmt

Navigate to the folder of userManagement from explorer and you will see a lot of directories created. roo has created a project and follows the maven convention. In fact it uses Maven internally to manage the project. We will now convert this application into a full fledged application using roo. Roo uses the following technologies to build the web application

Spring core with MVC
JPA (There is a choice of provider between Hibernate, EclipseLink and OpenJPA)
Spring Security
Spring JS
Testing infrastructure using JUnit and Selenium

This stack is a proven stack for building applications in the industry. roo uses the best of breed stack and applies best of the patterns to do the job.

Coming back to our application, let's now build the domain model of our application. As we are using just one table, we will create an entity representing that table. Though this example will not cover, roo can handle the relationships between the tables also.

Back to our roo cmd, issue the following, let's first setup hibernate as persistence provider.

persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY

Let's create the UserDetails entity now. (I remember that table name as User gives some conflict in some database, so running safer by naming entity as UserDetails. We can create the User Entity and map the table to User_Details, but let's go with convention over configuration now). To make the entity, get back to cmd and issue

entity --class ~.domain.UserDetails --testAutomatically

Inspect the *.domain folder where the entity is generated. There are more number of artifacts generated than one expects. This is something very roo specific. roo generates a Java class but for writing setters/getters and toString method it creates aspectJ classes. Also in the java class you would see roo specific annotation.

package com.lalit.usermgmt.domain;

import javax.persistence.Entity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.entity.RooEntity;

public class UserDetails {

The other three aspectJ classes are:

UserDetails_Roo_Entity.aj - contains id, version and database operations.
UserDetails_Roo_ToString.aj - contains the toString method

roo takes these aspectJ classes and weaves them into java class after compilation. No traces of roo is promoted to run time artifacts.

Another important things to note is that roo promotes the layering of application using rich entities. The entities themselves contain all the business logic inside it. They are not anemic. Also as we will see later, roo does not promotes service and dao layer. The controller will be directly calling methods on entities. In other words, all the service, dao and entity layer code is collapsed into the entity class itself. This is the philosophy that generally Spring community promotes, whereas hibernate community promotes entities as plain POJO's. So be careful about the design choices roo is making. There is some discussion right now to support the rich layering in roo in the future.

Let's add two fields of name and birthDate in the entity.

field string --fieldName name --notNull --sizeMin 50

This will generate one more aspectJ file UserDetails_Roo_JavaBean.aj, which contains the getters and setters in it.

Similarly introduce the birthDate field

field date --type  java.util.Date --fieldName  birthDate

The finders can also be created in a simple ways. Use "finder list" to list all the possible finders and "finder add" to add a finder. The finder generated return the Query object and you will have to execute yourself to get the result set.

Let's now create the web tier. For that we can ask roo to create the controller having CRUD operations for all entities in one go.

controller all --package ~.web

This will generate all the artifacts for web layer and will also create the basic CRUD screens. The mappings created by roo in controller are Rest style mappings.

Let's run the application using Tomcat server. Quit roo shell and issue a maven command

mvn tomcat:run

Hit the application url through a browser at http://localhost:8080/usermgmt. You may have a different url, if you have changed the project name above.

Spring security can be enabled by issuing a simple command at roo cmd

security setup

To make the project identifiable by eclipse, in the roo cmd

perform eclipse

From the eclipse import the project by following File->Import->Existing Project Into Workspace

Removing ROO

The pretty good thing about Roo is that it can be removed from the project at any point of time at a later stage. To remove roo, three things need to be done:
  • Make sure eclipse has AJDT plugin installed. Right click in project name and issue Refactor -> Push In Refactor. This will push all the code in aspectJ files into Java file.
  • To remove the @Roo annotation from Java file: Click on Search -> Search -> File Search. Write on Containing text "\n.*Roo.*\n" (without the quotes), file name pattern "*.java" (without the quotes), ticking the "Regular expression" check-box and clicking "Replace". When the following window appears and asks you for a replacement pattern, type "\n" and click on continue.
  • Remove the org.springframework.roo.annotations-*.jar artifact from pom.xml and refresh the eclipse project using Maven. Issue "mvn eclipse:clean" and than "mvn eclipse:eclipse"
A video tutorial explaining roo

More Articles on Spring

No comments:

Post a Comment