Spring Roo Tutorial

This post will take you through the steps required to build a simple Spring MVC application using Spring Roo. I’ll explain each command as we go and show you how Roo can be used to quickly build a fully functional Java application using best practices.

Before you begin you’ll need to download Roo from http://www.springsource.org/spring-roo#download and set up the ROO install directory in your environment variables. This will allow you to launch the Roo command shell from the command prompt.

1. Start up command prompt and move the directory that you’d like to build your application in. For this tutorial I’ll be building my app in C:\tutorial.

2. Once you've navigated to your chosen directory simply enter ‘roo’ to start up the Roo command shell. You should see the shell start up something like figure 1.0 below.

Figure 1.0

3. Enter the following command

          project --topLevelPackage com.tutorial.roo --projectName RooDemo
      This command will create a new Maven project skeleton using the specified package and project names. The project skeleton will contain a resources directory structure as well as basic log4J and Spring application context configuration files. See figure 1.1 below.

Figure 1.1

4. The next step is to setup our persistence tier. To do this type the following command.

     jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
This command sets up a JPA persistence tier using Hibernate as our JPA implementation. Pressing the tab  key after --provider will list all available options but for the sake of this demo I’m going to stick with  Hibernate as this is a common choice for many Spring applications right now. The --database portion of
the command tells Roo which database we want to use to persist our data. Again there are a range of
options here including Oracle, MySQL and SQLServer. For the sake of simplicity I’ve chosen a Hypersonic in Memory Database.
When you issue this command you’ll see that Roo updates the Spring application context, creates a new   database properties file and adds a persistence.xml to the existing project structure. Our project now has the basic wiring in place required for Hibernate persistence.
You’ll also notice that Roo has added a number of dependencies to the projects POM file. Roo will only add dependencies as they are needed so as to keep the deployable artefact (WAR file) as lean as possible.

Figure 1.2

5. Now that we have a basic project structure in place (including our persistence configuration) its time to add some entities to our application. These are the actual business or domain objects that form the backbone of our application. To keep this tutorial simple my application is going to have just 2 entities, a Student entity and a Subject entity. As you’ve probably already guessed I’m modelling an ultra simple Student Management application. To create a student entity issue the following command.

    entity jpa --class ~.domain.Student

You’ll see from figure 1.4 below that Roo has generated a number of new files. Student.java is a simple POJO representing a Student in our application. You’ll see that the class has a number of annotations, these allow the class to be managed by Roo as the project is modified. Aside from these annotations this is a plain Java bean that can be used to encapsulate Student related business logic. There are a number of related AspectJ files generated – Student_Roo_Jpa_Entity.aj and Student_Roo_Jpa_ActiveRecord.aj allow Student.java to behave as a JPA entity with basic CRUD functionality made available out of the box. This means that a Roo generated entity can be used to perform basic database CRUD operations without any coding from the developer. Obviously more useful applications will require custom data access functions (query by surname for example) but these can be added to the Student entity as required and will work alongside the basic CRUD functionally provided by default.

Student_Roo_Configurable.aj allows the Student bean to become a Spring managed bean and Student_Roo_ToString.aj provides a useful implementation of the toString method using reflection.

Figure 1.3

6. Next run the following command to generate a Subject entity

          entity jpa --class ~.domain.Subject

7. Next we’ll start adding attributes to our domain objects. Use the following command to create a forename instance variable and the associated getter and setter method.

    field string --fieldName forename --class ~.domain.Student

You’ll notice that Roo has generated  a new file called Student_Roo_JavaBean.aj. This file contains the setters and getters for all fields defined on an entity. 

8. Add 2 more fields to our Student entity by issuing the following commands.

    field string --fieldName forename --class ~.domain.Student
    field date --type java.util.Date --fieldName dateOfBirth --class ~.domain.Student

9. Now we’ll add 2 fields to our Subject entity by issuing the following commands.

    field string --fieldName name--class ~.domain.Subject
    field boolean --fieldName mandatory--class ~.domain.Subject

10. Next we need to create a relationship between the Student and Subject entity. This will allow us to find subjects that a particular student is registered for.  We do this by issuing the following command
     field set --type ~.domain.Subject --fieldName subjects --class ~.domain.Student --cardinality   

11. Now that we’ve defined our entity model we can start looking at the application front end. Roo can generate all necessary Spring MVC scaffolding from an entity model allowing you to quickly build a simple CRUD web front end for your application in a matter of minutes. The following command will generate Spring MVC configuration files, tag libraries, JSPs, standard images and CSS for our application front end.

     web mvc setup

12. The next step builds on top of our existing MVC skeleton by adding MVC Controllers for each entity and some top level JSPs that will provide basic CRUD functionality for each of the entities in our application. Run the following command

           web mvc all –package ~.web  
      13. At this point all the core components required by a standard Spring MVC application are in place. We can now package the application into a WAR (web archive) so that in can be deployed into a web container. We package the application with the command below.

       perform package 
Figure 1.4

      14. We can now exit the Roo shell using the ‘quit’ command – this will take us back to the command prompt. The WAR we built using the ‘perform package’ command can be deploy to any servlet container, for convenience I'm going to deploy it to Jetty using Maven. You’ll obviously need Maven installed and available on the command line. Running the following command should deploy the application to port 8080 on localhost as shown in the image below.

Figure 1.5

      In my next post I'll cover the the Roo framework architecture in a little more detail and provide an insight into how Roo works along side existing Java technologies to enable rapid application development.


  1. One question: Using Spring Roo we can easily create all CRUD operations.. more like an administration backend. But how do we add a customer facing frontend/public part of the portal. Say like a CMS, admin backend for viewing list of all content and add/delete etc.. and then the frontpage with varied content coming from the DB.

  2. Hi i am ravi,
    After executing perform package command
    i am getting error as
    [INFO] ------------------------------------------------------------------------
    [ERROR] Plugin org.codehaus.mojo:aspectj-maven-plugin:1.2 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.codehaus.mojo:aspectj-maven-plugin:jar:1.2: Could not transfer artifact org.codehaus.mojo:aspectj-maven-plugin:pom:1.2 from/to spring-maven-release (http://maven.springframework.org/release): Error transferring file: Connection timed out: connect -> [Help 1]
    [ERROR] o>
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR] o>
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

    could you please give me any idea to overcome this error

  3. hello, my name is Paz and perform all the steps to develop an application, but to perform run package I get an error = 2, "The system can not find the file specified"

    Could you give me some idea to solve this error?

  4. I found a few errors:

    - You define "forename" twice for the Student entity.

    - The Roo command for the Subject entity's "name" field is missing a space before "--class". Also the "mandatory" field needs a space before it's "--class" argument.

    - The double-dash in the "web mvc all --package ~.web" has been converted to a long dash.

  5. when running perform package nothing happens and after killing the task get error around mvn.bat not completing successfully. Is there some other way to create the war ?

    1. Run maven with the -X option for more verbose logging. For example mvn -X clean install. This should provide you with more detail about the error that you're getting.

    2. this task takes many times, so be patient

  6. Ming the command to run the app
    mvn -X clean install
    mvn jetty:run


Post a Comment

Popular posts from this blog

Spring Web Services Tutorial

Spring Boot & Amazon Web Services (EC2, RDS & S3)

Spring JMS Tutorial with ActiveMQ

Axis2 Web Service Client Tutorial

An Introduction to Wiremock

Spring Batch Tutorial

Java Concurrency - Multi Threading with ExecutorService

Spring Boot REST Tutorial

Spring Quartz Tutorial