Showing posts with label Spring Roo. Show all posts
Showing posts with label Spring Roo. Show all posts

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.

Tuesday, September 20, 2011

Creating service layer in Spring Roo

In Installing an installable add-on recipe of Spring Roo Cookbook, I showed how to download and install gvNIX add-on for creating Services in your Roo-based application.
Spring Roo 1.2 M1 adds a new service command which you can use to create service layer of your application, as mentioned in this Spring Roo blog entry.



Saturday, September 17, 2011

Source code of Spring Roo 1.1 Cookbook

The final source code of Spring Roo 1.1 Cookbook is now available at Google Code project: http://code.google.com/p/spring-roo-cookbook/downloads/list

If you have any questions, then please post it in the forum for Spring Roo Cookbook: https://groups.google.com/forum/#!forum/spring-roo-cookbook

Sunday, June 12, 2011

Roo is for developers

Since Spring Roo 1.0 I've used it in implementing multiple PoC (Proof of Concepts) and applications. I've found it most useful when you understand the underlying Spring and related frameworks, that form part of your application. Roo only generates CRUD operations and UIs, which is not necessarily the case in most real-world applications. Also, you may not use JPA to implement your persistence layer (though Roo is making big leaps towards incorporating support for NoSQL and graph databases). Still, in its current form, it does a lot of heavy lifting to save time in implementing any PoC or application in which Spring plays an important role in the middle-tier.

In many many cases, I used Roo to generate a Spring Web MVC application against a throw-away JPA layer and I moved out of Roo, and  modified the roo-generated application as described here:
1. Deleted the JPA layer, and instead used an in-memory cache or a NoSQL database. Added my own DAO layer for interaction with the cache or NoSQL database.
2. Deleted UI layer of the application, as it was not required in some cases.
3. Modified roo-generated web-tier controllers as per the application requirements. The controllers were accessed via Spring remoting or via HttpClient for sending/receiving JSON objects.
4. Created service layer of the application.
5. Modified pom.xml to not use compile-time weaving and removed or added maven plugins that I needed in the application.
 
It's amazing how fast you can go about developing applications if the boilerplate code and configurations are generated by a tool like Spring Roo, and all you need to do is to tweak the code/configuration to quickly get started with doing what is important, and this is - writing business logic.

Thursday, June 10, 2010

Rapid Application Development with Spring Roo

A few months back I bumped into Spring Roo project and since then I have been closely following its progress. Spring Roo is different from what we have seen so far in the rapid application development arena for enterprise application.

Spring Roo is a text-driven, intuitive tool which is meant for rapidly developing Java enterprise applications, which make use of standard technologies/frameworks like Spring Web Flow, Hibernate, Spring Security, GWT, and so on. All you need to do is to run Spring Roo tool and provide it with the instructions for creating a project, developing persistence layer objects (and its fields), controllers for the web layer, unit/integration test scripts, and so on. The rest is taken care by the Spring Roo to create a standard maven project, provide glue code between your web and persistence layer and create necessary configuration files for interacting with the database, unit/integration testing scripts, and so on.

You can think of Spring Roo as a code generator, but it is lot more than just a code generator.

Spring Roo is different because:
- Your application code is independent of Spring Roo, that is, your application code is not coupled with tool-specific libraries or classes ---- something which has been at the center of any Spring project, which makes Spring Roo a non-intrusive tool.
- You don't need to learn anything but you shouldn't forget anything about enterprise application development. You still need to code when using Spring Roo for developing applications. This is because Spring Roo only generates code that can be conveniently passed as set of instructions to the Spring Roo tool, the rest you need to write.
- No runtime dependency of application code on Spring Roo specific libraries. Your application code is like any other enterprise application code developed without Spring Roo.
- Your enterprise application project is mavenized, by default
- You choose the technology/framework that you want to use for developing enterprise applications. Spring Roo supports Spring Web Flow, Hibernte, OpenJPA, GWT, Spring MVC, and so on. You can use any of these technologies or framework based on your requirement.
- Unit/integration tests can be automatically generated by Spring Roo
- Best practices are followed in the application architecture and code. Spring Roo makes use of best practices in developing application architecture and application code, therefore, the quality of enterprise application is guaranteed by Spring Roo tool

In the background Spring Roo makes use of AspectJ's ITD (Inter-type declarations) to generate the source code for the application. In the next blog entry I'll discuss ITDs in detail and how Spring Roo makes use of it.