Sunday, 20 May 2012

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 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. 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 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.

Thursday, 17 May 2012

Introduction to Spring Roo

Anyone who has worked with enterpeise Java in the past will know that it can suffer from productivity issues when compared with platforms like .NET. The Spring framework has gone a long way toward addressing these issues and as a result things has improved dramatically since the early days of J2EE. However, enterprise Java still has a long way to go in terms of productivity - The Spring community has recognised this and in an attempt to address the issue they've developed Spring Roo.
What is Spring Roo?
Spring Roo is an easy-to-use productivity tool for rapidly building enterprise applications in Java. It leverages existing enterprise Java technologies such as Spring MVC, Hibernate, JSP, tiles, Spring security and Maven. Roo provides automatic setup of these technologies into a best-practice application architecture and, if you wish, automatic maintenance of all files required by those technologies (such as XML, JSP, Java etc).  This use of standard technologies allow developers to start using Roo alongside their existing Java knowledge, skills and experience.
Roo is not a runtime, so Roo is not involved with your project when it runs in production. You won't find any Roo JARs in your runtime classpath or Roo annotations compiled into your classes. This is actually a key benefit, it means you have no lock-in to worry about and you can remove Roo from your project in just a couple of minutes if the need arises. It also means it should be much easier to get approval to use Roo as its  more like a command line tool than a critical runtime library like the Spring or Hibernate. It also means there is no way for Roo to slow your project down at runtime, waste memory or bloat deployment artefacts with JARs. Roo was developed so as to impose no engineering trade-offs - this was one of the central design objectives.
How do I use Spring Roo?
Roo is loaded up in a "shell" window and can be left running in the background. You can interact with Roo via commands typed into the shell if you like, but most of the time you'll just go about programming in your text editor or IDE as usual. As you make changes to your project, Roo intelligently determines what you're trying to do and takes care of doing it for you automatically. This usually involves automatically detecting file system changes you've made and then maintaining files in response.
Why use Spring Roo?
  • Higher Productivity
Roo has been developed to so that enterprise Java developers can enjoy the same kind of productivity that developers on other platforms take for granted. 
  • Standard Java Technologies
Roo projects use standard Java frameworks such as the Spring Framework, Spring Security, Spring Web Flow, Maven, JSP, Hibernate, Tiles and AspectJ. These technologies are commonly used in enterprise Java projects, so it’s likely that developers are already familiar with them.
  • Easy to Use
Roo’s command shell uses tab completion for completing all mandatory arguments step-by-step. There is also the ‘hint’ command which suggests what you may wish to do next based on your present project's state. It's pretty easy to build an entire Roo project simply by typing "hint", pressing enter, and following the instructions Roo presents.
  • No Engineering Trade Offs
Roo doesn't impose any engineering trade-offs on your project. Roo applications will typically have a smaller deployment artefact because it uses an incremental dependency addition approach, where dependencies are only added to your project as they are needed. This means the deployed application should operate more efficiently in terms of CPU and memory consumption. 
  • Easy To Remove
Architects and developers who introduce new technologies into a solution should be aware of the associated technology risk. With this in mind Roo was developed so that it can be easily removed if needs be. One of the biggest risks when adopting a new tool like Roo is the ease at which you can change your mind in the future. There are a number of reasons why you may want to remove a tool from your development ecosystem, such as changingrequirements, a better alternative emerging, the tool having an unacceptable number of bugs, or the tool not adequately supporting the versions of other software you'd like to use. In commercial software development it's important to take the appropriate steps to mitigate these risks.
In my next post I’ll detail a step by step guide to building a fully functional Spring MVC application with Spring Roo.