February 16, 2010

Spring Roo Tutorial

« Configure Log4j (log4j.xml) | Main | Configuring Beans in the Spring IoC Container »
Spring Roo is a tool made by the Spring developers to help you speed up the development of your (web-)applications.

I tried Spring Roo 1.0.0.RC2 [rev 321] some time ago and now I got the time to document the experience I made with it.

It is just great I love tools that generate (error-free) code like that. It saves a lot of time for both developing and debugging.

First of all I downloaded Spring Roo from http://www.springsource.org/roo, extracted it in my Home-Directory and added the Spring Roo bin/roo.sh (on Linux) to my PATH-variable so that I could access Spring Roo from whatever directory.

Secondly I created a directory for my Spring Roo example project. In this case "~/workspace/rooExample". After that I executed roo.sh.

The Spring Roo console presented itself like this:

Welcome-Screen Spring Roo Console

So the first thing we want to do is to create a project. So we type in "project". Spring roo now automatically asks us for the name of the top level package. In this case I typed in: net.localh0rst.forrest.roo.example. Spring Roo creates now all the necessary files and folders matching the maven folder hierarchy.


I let Spring Roo guide me through the process of creating a project so I type in "hint" so that roo helps me what to do next.

Spring Roo Hint

It told me to create a persistence layer to access a database. That is what we want. So typing "persistence brings me to this options:

Spring Roo Persistence Setup

In this case I created a persistence layer based on Hibernate accessing a MYSQL database. But there are a lot of varieties as you can see. Spring Roo also tells me to edit my database properties (i.e. username, password, mysql connection string) in my properties file.
As you can see we created an entity called person since we want to store multiple persons in our MYSQL database. You can create an entity by simply typing entity. As printed above the "~" tells Roo to use the top-level packagename we created earlier. I used the sub-package "domain" in this case. You can choose any package-conform name here or just use the top-level-package.
Now it's time to transfer our ER-model (we hopefully planned earlier ;-) ) into java code. So let's add some fields to our entity like id, firstname and lastname.

Spring Roo insertion of fields into entity
Note that besides from the java class "Person.java" Spring Roo created some more files (.aj). These are aspectJ-Files which contain several methods and are injected later into the Person class. I will cover that topic later on.

After we finished creating Person with all its attributes we also want to cover the addresses where persons of our application live. So we create an extra entity address because we do not want to store every address in the Person table.

Spring Roo creation of entity Address
And add a few fields to it.

Spring Roo Address field adding
Note that we switched the entity we want to add fields to by just creating a new one. We did not specify the entity we are working on at the field-command because Spring Roo knows the last entity we created.
Lastly we want to connect Address to Person. So we add the entity Address to the entity Person by using a Join Column. But we don't have to think about the syntax here because Spring Roo covers that for us so we just switch to the entity Person. We would add address to itself if we didn't switch. So we just call "entity" again and tab to "Person" to select Person.

Spring Roo add Address to Person
We used the field "reference" to tell Spring Roo that we want to create a reference to "Address" here and tab all our way to the entity of our choice. Like this we created a hibernate persistence layer with 2 tables in 2 minutes. That was quick, wasn't it?

So let's finally take a look at all these files Spring Roo created for us. Just type "quit" to quit the Spring Roo console and return to our shell. A ls/dir/whatever shows us a lot of files at our "domain" directory/package.
Spring Roo created files
First let's take a look at our Person.java.

Roo created Person.java
All you see is a normal java file with attributes with some additional annotations on the class and on the attribute "Address" for hibernate to join these to tables later on.
We do not see any methods here. This is kind of good because like that you have an excellent overview of the data and how it is represented.

So where are all the methods we want to use?
Here:

Roo created Files Roo_Entity.aj
and here:
Roo created files Roo_Entity.aj part2
Spring Roo created every method that we will use later on and even used the annotation "@Transactional" for error-handling. In case an error occurrs while persisting an Person object Hibernate will automatically roll back the insert statement. That's comfortable! :-)
It also provides a countMethod and and and....

But what about all these attribute-related methods? Like getters and setters? Well.. let's look at the Person_Roo_JavaBean.aj-file.
Roo created file Person_Roo_JavaBean.aj
And toString()?
Spring Roo created files Person_Roo_ToString.aj
Roo created files Person_Roo_ToString.aj
Last but not least the Person_Roo_Configurable.aj - File
Roo created files Person_Roo_Configurable.aj
In this file is the configuration of the Person.class. Person is annotated with "@Configurable" which only means that this bean is eligible for Spring-driven configuration.

Hope that helped. =)
Guido Herrmann

Technorati Tags:

Posted by guido.herrmann at 6:22 PM in Spring

 

[Trackback URL for this entry]

Comment: Ben Alex at Do, 18 Feb 11:28 PM

Thanks for sharing the above tutorial with the Spring Roo community! I've added it to our list of useful Roo project resources at http://forum.springsource.org/showthread.php?t=71985.

Cheers
Ben Alex
Project lead, Spring Roo

Comment: S.T. at Fr, 19 Feb 10:31 AM

Thank you very much for taking your time and creating this very useful information.

Comment: Mike Miller at Fr, 19 Feb 2:49 PM

I've seen and played w/Roo a little bit and it is really cool. But how is helping with your debugging?

Comment: Guido Herrmann at Fr, 19 Feb 11:28 PM

What I meant is that generated code is a less buggy than hand-written code. Less bugs means less time for debugging. Another thing is the separation of code into different .java and .aj files which makes it a lot faster to read the code and to identify where the bug is you're looking for.

Comment: Eduardo Guillen at Mo, 8 Mrz 8:11 PM

I just don't see how this is intuitive. I've tried Roo, and I got completely frustrated after a few hours of tabbing, making errors, switching between console/Java editor. I'm sure Spring Roo is doing a lot, but it's anyone's guess what that is.
How is "type X and tab Y times" an improvement?

Comment: jquery at Di, 9 Mrz 6:06 PM

It's really interesting article. Thanks for good information.

Comment: Sakuraba at Fr, 12 Mrz 3:49 PM

>> How is "type X and tab Y times" an improvement?

It is an automation of a repetitive task. But it is not mandatory for using Roo. If you dont like using the Shell, just edit the source files as you would do if you would not use Roo at all. Roo wont mind, basically it will just contintue to work in the background just like your other development tools like IDEs or SCM-programs.

Comment: vinod garg at Mi, 5 Mai 5:11 AM

It's really good. I need some more information about how we can create primary key using ROO.

Comment: ziv livne at So, 6 Jun 7:37 PM

How can I add a method?
I have tried adding this one to the .java file:
public double Tax()
{
this.Tax=this.total*0.13;
return this.Tax;
}
but roo didnt updated all the resources and created any notation of the function?

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« February »
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28