Spring Roo Startup & First application

hello everybody, i will show you how to start development with spring roo.

what is that Spring Roo?

Spring Roo is a next-generation rapid application development tool for Java developers. With Roo you can easily build full Java applications in minutes. It differs from other productivity tools by focusing on:

  • Rapid results
  • 100% develop in Java
  • Easy-to-use
  • Fast and simple to remove
  • Totally compromise free
  • Active, helpful community
  • Comprehensive technology integrations
  • Extension points via Roo add-ons
  • SpringSource-certified architecture

reference from here.

lets start by downloading spring roo now from here.

next, we have to use Maven to test, run, build … your application until now before using an IDE like STS (Spring tool Suite) or IntelliJ IDE.

After downloading we will decompress the zip file of Spring Roo in any location of your choice.

open your Terminal if you use Linux as an operating system (i use now Ubuntu 12.04 as a Linux distribution), you can open it by pressing (CTRL+ALT+T). And write this command line:

# ln -s <Location of your Spring roo directory>/bin/roo.sh /usr/bin/roo

now you can use the command “roo” in anytime you like.

Now let’s create our first application Spring Roo.

create into your work-space a directory named SpringRooFirstApplication by typing this command line:

$ mkdir SpringRooFirstApplication

Now we will move into this directory:

$ cd SpringRooFirstApplication/

Let’s use roo command line to open Spring Roo shell to create our spring roo application and execute some command line to set up our database and our web component.

$ roo

Now create our first application by tapping that:

roo> project –topLevelPackage tn.gasmi.firstapplication –projectName SpringRooFirstApplication

Spring roo create files like :

  • pom.xml: show here
  • log4j.properties: setup Log4j framework to log the activity of our application.
  •  applicationContext.xml: declare spring beans

Now we have create a simple Maven/Spring application after taping one command line into spring roo shell.

this work will not show you the true power of spring roo, but now the true strength of spring roo will appear clearly.
To set up a simple JPA application you can do the job between 6 and 24 hours. But with Spring Roo this job will be very simple and we can setUp JPA into our application, create entities and create repository to expose CRUDs between 15 min and 1 hour it depends on the complexity of the project.

Let’s setup our jpa component into our application by tapping this command line into spring roo shell:
roo>  jpa setup –provider ECLIPSELINK –database HYPERSONIC_PERSISTENT

Spring roo create/update files like :

  • database.properties: properties of database like host name, user name, password, database name, …
  • applicationContext.xml: xml file to declare beans into our application like datasource, transactionManager, entityManagerFactory, setup AspectJ and the use of annotations into spring.
  • persistence.xml: jpa properties

Now let’s create a table named Student and add some fields like first name, last name, email, address, phone number; by tapping this command line into spring roo shell:

roo>  entity jpa –class ~.model.Student –testAutomatically

what it means this ~:  this is a shortcut to the top level package like “tn.gasmi.firstapplication” in our project.

this  command line create and entity of jpa with adding some Annotations like:

  • @RooJavaBean
  • @RooToString
  • @RooJpaActiveRecord

this annotarion help spring roo do know that he should create AspectJ files like:

  • Student_Roo_JavaBean.aj : to declare getters and setters of fields into Student java class.
  • Student_Roo_Configurable.aj : for configuration
  • Student_Roo_Jpa_ActiveRecord.aj : into this aspect spring roo declare the entity manager and CRUDs to use it from Student directly.
  • Student_Roo_ToString.aj : declare the toString method.
  • Student_Roo_Jpa_Entity.aj : declare the id, version, and name of the table into database.

All this aspects are generated automatically and you can use the methods in these aspects directly from Student java class.

what it means this –testAutomatically: this will ask spring roo to create a junit test for Student entity, that mean that you can test all CRUDs of Student by using mvn test command line or run a junit test from your IDE.

Spring Roo create some files into src/test/java like:

  • StudentIntegrationTest.java: java class for Junit Test.
  • StudentIntegrationTest_Roo_Configurable.aj : configuration of the junit test.
  • StudentIntegrationTest_Roo_IntegrationTest.aj : CRUDs Tests like: testCountStudent(), testFindStudent(), testFlush() , testMergeUpdate() …
  • StudentDataOnDemand.java, StudentDataOnDemand_Roo_DataOnDemand.aj, StudentDataOnDemand_Roo_Configurable.aj : create fake entities of Student to test with it.

Let’s add some field to Student entity by tapping commands:

~.model.Student  roo> field string –fieldName firstname –sizeMin 1 –sizeMax 30 –notNull –column FIRST_NAME

~.model.Student  roo> field string –fieldName lastname –sizeMin 1 –sizeMax 30 –notNull –column LAST_NAME

~.model.Student roo> field email template –fieldName email

~.model.Student roo> field string –fieldName address –column ADDRESS –sizeMax 100

~.model.Student roo> field string –fieldName phonenumber –sizeMin 6 –sizeMax 12 –regexp “^0[1-9]([-. ]?[0-9]{2}){4}$”

Now, you can test your application by using mvn test or run a junit test with your IDE.

but i will show you how to test directly your CRUDs so you can create, update, delete, and list Students from the web.

Let’s setup the web component into our projetc by tapping this:

roo> web mvc setup

roo> web mvc all –package ~.web

the first command set up Spring MVC by creating views and configuration file “webmvc-config.xml”.

the second command create views and controllers for All entity into the package ~.web.

Now lets run our project but how?

Spring Roo setup two web container; jetty and tomcat so with maven command you have only to tap into Linux shell:

  • mvn jetty:run
  • mvn tomcat:run

and we will show the result into the web browser with this url: http://localhost:8080/SpringRooFirstApplication/

So you show that:

now you show that Spring roo have done all work to create a complete application architecture and use Spring MVC like a Presentation Layer.

That’s all for our first tutorial Spring roo and see you in an other tutorial.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s