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:
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.
.png)
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:
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.
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.
And add a few fields to it.
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.
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.
First let's take a look at our 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:
and here:
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.
.png)
And toString()?
Spring Roo created files Person_Roo_ToString.aj
Last but not least the Person_Roo_Configurable.aj - File
.png)
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: spring roo

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