Tuesday, September 27, 2011

Spring Roo 1.1 Cookbook published

I just heard from Packt that Spring Roo 1.1 Cookbook is now available. Spring Roo 1.1 Cookbook provides a comprehensive coverage of Spring Roo development tool features.

For more details, please refer the following links:

1. List of recipes covered in Spring Roo 1.1 Cookbook: https://groups.google.com/forum/#!topic/spring-roo-cookbook/mnM_GJMTto8
2. Book page at Packt: https://www.packtpub.com/spring-roo-1-1-cookbook/book You can download sample Chapter 1 from this page.
3. Amazon link: http://www.amazon.com/gp/product/1849514585/
4. Book review: http://www.owal.co.uk/a/springroocookbook
5. Book source code: http://code.google.com/p/spring-roo-cookbook/downloads/list

Monday, September 26, 2011

Creating JPA entities using Spring Roo

In the previous post we saw how you can create a Java project using project command of Roo. In this post, you'll see that how you can create JPA entities using Roo.

Before we go about creating JPA entities, if you take a quick look at the pom.xml file of flight-app project, you'll see that it contains standard dependencies that you'll typically have in your Spring-based Java projects. The extra dependencies that you see are related to AspectJ and Spring Roo's annotation. You'll also notice that pom.xml configures Eclipse Maven plugin and Tomcat Maven Plugin.

Follow these steps to create a Flight JPA entity:
1. Setup a Hibernate as the persistence provider. Open command prompt and execute persistence setup command, as shown here:

... roo> persistence setup --provider HIBERNATE --database MYSQL
--databaseName myFlightAppDB

provider argument identifier a JPA provider (a couple of JPA providers are supported by Spring Roo)
database argument identifies the database that you are going to use for your application (a couple of databases are supported by Spring Roo)
databaseName argument identifies the name of the database

Executing persistence setup command creates a database.properties file which contains database configuration information (username, password, database name, database driver class). It also creates a persistence.xml file required by the JPA provider.

2. Create a persistence entity using entity command, as shown here:
..roo> entity --class ~.domain.Flight --table FLIGHT_TBL


The entity command shown above creates a Flight.java class (representing a JPA entity) in com.sample.flightapp.domain package. The table argument identifies the table to which the Flight entity maps. You'll see couple of more files created with .aj extension....these AspectJ ITD files, which you should never modify.

The following code shows the Flight.java class:

@RooJavaBean
@RooToString
@RooEntity(...)
public class Flight {...}


As you can see, Flight.java is annotated with couple of @Roo* annotations. These annotations trigger code generation in Spring Roo. The identifier definition, getter/setters for properties defined in Flight.java class, the persistence related methods (create, merge, update, and so on), and the toString methods are all inside the AspectJ ITD files generated by Roo.

The following things to note:
- AspectJ ITD files are like any other Java file, with a slightly different syntax. You don't need to learn a new language to understand AspecJ ITDs.
- The method, constructor, and so on, definitions in AspectJ ITD files are compiled into the corresponding Java source file. In our case, declarations contained in Flight_Roo_Entity.aj file are compiled into Flight class. This means resulting bytecode is not dependent on Roo at all.
- AspectJ ITD files are managed by Spring Roo when you make modifications to Java source file. So, you should not change them.
- If you want to modify a declaration in AspectJ ITD file, you can simply use Push-in Refactoring (using your IDE) to push the declaration (which could be method, attribute or constructor) into the corresponding Java source file. After doing push-in refactoring, you can modify the code manually in Java source file.

In the next post we'll go a step further and scaffold a Spring Web MVC application.


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

Thursday, September 15, 2011

List of recipes in Spring roo cookbook


Chapter 1: Getting started with Spring Roo
1.       Setting up Roo
2.       Getting help and hints from Roo
3.       Creating a Roo project
4.       Configuring logging
5.       Importing Roo project into Eclipse/IntelliJ IDEA IDE
6.       Viewing properties defined in a properties file
7.       Managing properties defined in a properties file
8.       Creating a  Java class
9.       Adding attributes to a Java class
10.   Creating a Java interface
11.   Referring to a type from Roo shell
12.   Creating application artifacts from Roo script

Chapter 2: Persisting objects using JPA
1.       Setting up a JPA provider for your project
2.       Viewing database configuration properties
3.       Managing database configuration properties
4.       Creating persistent entities
5.       Adding JSR 303 constraints to persistent fields
6.       Creating integration tests for persistent entities
7.       Creating new ‘data on demand’ for testing entities
8.       Creating mock tests for persistent entities
9.       Executing persistent entities tests
10.   Controlling auto-generated methods of persistent entities
11.   Creating applications that interact with multiple databases
12.   Packaging your Roo project

Chapter 3: Advanced JPA support in Spring Roo
1.       Viewing candidate dynamic finder methods
2.       Adding dynamic finder methods to an entity
3.       Creating many-to-one (or one-to-one) relationship between entities
4.       Creating one-to-many (or many-to-many) relationship between entities
5.       Creating a mapped superclass
6.       Customizing Roo-generated identifier definition
7.       Generating database metadata
8.       Creating entities from database

Chapter 4: Web application development with Spring Web MVC
1.       Auto-generating Spring MVC controllers and JSPX views from JPA entities
2.       Packaging, deploying and using Roo-generated Spring MVC application
3.       Modifying Roo-generated views
4.       Round-tripping support in Spring Roo for web controllers and views
5.       Creating a Spring MVC controller for a specific JPA entity
6.       Manually creating a Spring MVC controller for a JPA entity
7.       Adding static views to Roo-generated web application
8.       Internationalizing Roo-generated web applications
9.       Adding or modifying themes generated by Roo
10.   Adding JSON support to domain objects and controllers
11.   Creating and executing selenium tests for web controllers

Chapter 5: Web application development with GWT, Flex and Spring Web Flow
1.       Scaffolding GWT application from JPA entities
2.       Getting started with Flex application development
3.       Scaffolding Flex application from JPA entities
4.       Getting started with Spring Web Flow

Chapter 6: Emailing, Messaging, Spring Security, Solr and GAE
1.       Sending emails using JavaMail API
2.       Sending and receiving JMS messages
3.       Configuring Spring security for your application
4.       Using Spring Security with Apache Directory Server
5.       Deploying a GWT application on GAE
6.       Deploying a Spring Web MVC application on GAE
7.       Adding search capability to your domain model with Solr

Chapter 7: Developing add-ons and removing Roo from projects
1.       Setting up GnuPG for add-on development
2.       Installing an installable add-on
3.       Developing a simple add-on
4.       Developing an advanced add-on
5.       Converting non-OSGi JDBC drivers into OSGi-compliant bundles
6.       Removing Roo with push-in refactoring
7.       Adding Roo to a project using pull-out refactoring
8.       Upgrading to latest version of Roo

Wednesday, September 14, 2011

Spring Roo Cookbook status

Spring Roo Cookbook is now complete and is scheduled to go into print next week.