Sunday, September 25, 2011

Creating your first Java project using Spring Roo

Spring Roo is a productivity improvement tool....and you got to use it to believe this. In this post I'll show how to quickly get started with Spring Roo and create a Roo project. I will also talk about some of the artifacts created by Roo.

Note - Some of the information covered in this post are covered in Chapter 1 of Spring Roo 1.1 Cookbook. Once the book is released, Chapter 1 will become freely available. (Update - Sample Chapter 1 of book is now available here: https://www.packtpub.com/spring-roo-1-1-cookbook/book)

Pre-requisite
Install Java SE 6 or later and set JAVA_HOME variable
Install Maven 3.x or later

1. Setting up Roo
Setting up Roo is simple, download the ZIP file from SpringSource website (http://www.springsource.org/roo/start) and unzip into a folder. Now, add path to bin directory of  Roo to your PATH variable.
2. Verify installation
Create a directory named myfirst-app. Open command prompt and go to myfirst-app directory and enter roo on the command line. You'll see the following output if your installation was correct:
    ____  ____  ____
   / __ \/ __ \/ __ \
  / /_/ / / / / / / /
 / _, _/ /_/ / /_/ /
/_/ |_|\____/\____/    x.y.z.RELEASE

roo>

The command prompt now becomes roo> indicating that you are now inside the Roo shell. The output shows the Spring Roo release that you are using. All the commands that you enter now will be processed by Spring Roo. You can think of a Roo shell as nothing but a standalone Java application that processes commands.

3. Creating a project
The first step is to create a Roo project. Though this is called a Roo project, it's just a simple mavenized project. To create a Roo project enter project command of Roo, as shown here:

roo>project --topLevelPackage sample.roo.flightapp --java 6 --projectName flight-app

Created C:\roo-cookbook\ch01-recipe\pom.xml
.....
Created SRC_MAIN_RESOURCES\META-INF\spring\applicationContext.xml
Created SRC_MAIN_RESOURCES\log4j.properties

sample.roo.flightapp roo>

As you can see from the above output, nothing specific to Spring Roo is created. It is like any maven project for creating a Spring-based application. 

topLevelPackage specifies the root package of your project.
projectName specifies the name of the project. This becomes the <artifact-id> and <name> element values in pom.xml file.

The applicationContext.xml file is shown here:

<beans ..>

  <context:property-placeholder
     location="classpath*:META-INF/spring/*.properties"/>

  <context:spring-configured/>

  <context:component-scan base-package="sample.roo.flightapp">

    <context:exclude-filter
                   expression=".*_Roo_.*" type="regex"/>

    <context:exclude-filter expression=
      "org.springframework.stereotype.Controller"   
      type="annotation"/>

 </context:component-scan>

</beans>

As the above listing shows, it contain <component-scan> element to auto-register Spring components. It excludes classes that follow the naming convention *_Roo_* and which are annotated with @Controller annotation.

As you can see, project command simply creates a maven project with some Roo-specific configuration. These Roo-specific configurations are quite limited and you can remove them, when you decide to do so. For instance, you can modify the <component-scan> shown above to exclude Roo-specific exclusion rule, and you can use this project as any other Spring project.

In the next post, we'll look at how create a JPA entity and import the flight-app project into Eclipse or STS or IDEA IDE.

1 comment:

  1. > You can think of a Roo shell as nothing but a standalone Java application that processes commands.
    Because that's exactly what it is! :-) Internally we use the Apache Felix OSGi container to load the core modules, core addons, and 3rd party addons as OSGi bundles.

    ReplyDelete