Tuesday, October 4, 2011

Spring Roo 1.1 Cookbook promotion at Javaranch

This week I'll be responding to questions related to Spring Roo in the Spring forum of Javaranch (http://www.coderanch.com/forums/f-96/Spring). You can win a copy of the book by posting questions in the Spring forum.

Spring Roo - future of Java application development

Traditional Java enterprise application development required developers to write the boilerplate code. The Spring Framework simplified Java enterprise application development with DI and POJO-based approach to developing applications. No wonder, Spring Framework has become the most popular platform for developing applications.

Spring Framework simplifies developing enterprise applications using standard technologies, like JPA, Hibernate, JDBC, Flex, JavaMail, JMS, and so on. Still, you need to write some generic configuration and generic code which can be generated by a tool. Let's look at a few examples:

1. If you write a class, you normally override the toString method
2. If you write a JPA entity class, then you provide getter/setters methods for persistent fields, method to obtain reference to EntityManager, create persistence.xml, and so on.
3. When using Spring Web MVC, you need to configure handler mappings, create controller classes corresponding to JPA entities, create JSP views, and so on.
...

And the list goes on and on, depending upon the technology or framework that you are using. If all these generic configurations and code are generated and managed by a development tool, developers will be more productive. And, if the code generated follows best practices of development, then you don't need to even worry about incorporating best practices in your application architecture. This is where Spring Roo comes as the next generation productivity improvement tool.

Spring Roo generates generic configurations and code to help developers focus on writing the business logic. There are no runtime dependencies of application on Spring Roo, and you can easily modify the Roo-generated code by using Push-in refactoring. So, even though you are using a productivity improvement tool, you have full control over modifying the generated code, and you don't need to learn any new language or frameworks to do so. If you know Spring, you can easily get started with using Spring Roo effectively. Spring Roo truely defines the future of enterprise Java application development.

Thursday, September 29, 2011

How to get the most out of Spring Roo 1.1 Cookbook

The recipes covered in Spring Roo 1.1 Cookbook are applicable to version 1.1.3, 1.1.4 and 1.1.5 version of Spring Roo. The Spring Roo 1.2 is currently in the milestone release, and it's final release will come sometime in December or early next year. Here, a few of my recommendations when going through recipes in Spring Roo 1.1 Cookbook:

1 - Use Spring Roo 1.1.5 when working with recipes described in Spring Roo 1.1 Cookbook. 
2 - Don't do copy-paste of commands from eBook to Roo shell. Instead, type it yourself on the Roo shell (you don't need to type everything because Roo offers auto-completion feature)
3 - If you are evaluating Spring Roo for developing an application which makes use of a set of technologies or frameworks, then pay close attention to 'How it works...' section, which describes what all configuration and code is generated by Spring Roo.
4 - If you are using Spring Roo to learn a technology, it is important to focus on 'How it works...' section.
5 - Be prepared to write code or customize the Roo-generated code. A lot of recipes show what to customize and how to customize Roo-generated code.

2 recipes from Chapter 4 of Spring Roo 1.1 Cookbook

2 recipes from Chapter 4 of Spring Roo 1.1 Cookbook are now available at Packt website: http://www.packtpub.com/article/working-with-roo-generated-web-applications

Wednesday, September 28, 2011

What is covered in Spring Roo 1.1 Cookbook

As a follow up to the previous post, here I've detailed what information you'll find in Spring Roo 1.1 Cookbook and for what information you need to refer to other resources.

AspectJ ITDs - Information about AspectJ ITDs and how they are used by Spring Roo is covered in extensive detail. You'll recipes related to Push-in and Push-out refactoring also.


Spring framework and Maven - The Spring Roo Cookbook assumes that you have enough understanding of Spring and Maven. If not, then please refer to Spring reference documentation or any other tutorial or book.

Technology or framework information - Spring Roo Cookbook attempts to give a crash course in JPA, GWT, Flex, Spring Web Flow, Spring Web MVC, Selenium, Solr, and other technologies for which base add-ons are provided by Roo. If you have never used these frameworks, Spring Roo Cookbook woul;d still be an easy read for you. If you are looking for details on these technologies or frameworks, then it is recommended that you refer to some other resources.


OSGi - You'll find some introductory information about OSGi, as this forms the heart of Spring Roo add-ons


Roo-generated code information - This is at the heart of Spring Roo Cookbook. It attempts to cover all the Roo-generated code and configuration information. You'll find information about maven plugins that are configured by Roo in pom.xml file, AspectJ ITDs, Java source code, and so on.


Add-on development - Chapter 7 shows the code generated by Roo for simple and advanced add-ons. It should give you a solid starting point to create your own add-ons. This should give enough information about internal workings of Spring Roo.

How to get the most out of Spring Roo development tool

Let me say this upfront - Spring Roo is not a replacement for a developer's technical skills. It'll only help you to be more productive but don't expect the tool to do everything for you.

To get the most out of Spring Roo, you need understanding of the following areas:

1. AspectJ ITDs
2. Maven
3. Spring framework


The first 3 points address the must-haves for using Spring Roo or you won't be able to understand what's going on when you execute commands. 

4. Good understanding of the framework or technology for which you are using Spring Roo to generate code

This is important if you want to modify the code generated by Spring Roo. For instance, if you don't know Spring Web MVC, then it won't be easy for you to figure out what you need to change to meet your application's requirements.


5. Some understanding of OSGi

If you have some understanding of OSGi, then you can easily understand what add-ons are and why Spring Roo follows add-on based architecture. This could also be helpful if you want to get started with developing your custom add-ons.
 
6. A solid understanding of the code generated by Spring Roo

The code generated by Spring Roo follows best practices and gives a solid foundation for developing your application. This understanding is important if you want to make changes to Roo-generated code to meet specific requirements of your applications.
 
7. A good understanding of add-ons

Not all features that you need are part of base add-ons provided by Spring Roo. So some understanding of how to install and remove add-ons is helpful. For instance, if there is a Portlet add-on, you need to know how to install it in your Spring Roo installation to create portlets.

8. A good understanding of how Spring Roo works when you execute commands from Roo shell


It is important to know how code generation works in Spring Roo. For instance, add-ons are responsible for managing AspectJ ITDs so you should not modify them.

Tuesday, September 27, 2011

Scaffolding a Spring Web MVC application

We saw in earlier two posts that using project command you can create a Java project, and by using persistence setup command you can setup a persistence provider (Hibernate, OpenJPA, and so on).

It's important to note the following when using Spring Roo:
- You don't need to remember commands because Roo is a very user-friendly development tool. You can look at all the Roo commands and their brief description using help command.
- Also, Roo offers auto-completion features - which means you partially type a command or an argument name, and Roo auto-completes it for you.

You can scaffold a Spring Web MVC application from JPA entities using controller all command.

Step 1: Download ch04_web-app.roo script from the Google Code project for Spring Roo 1.1 Cookbook (http://code.google.com/p/spring-roo-cookbook/downloads/list).  Execute ch04_web-app.roo script using script command of Spring Roo. A Roo script contains a sequence of commands which are executed when you execute script command, as shown here:

roo> script --file ch04_web-app.roo

Executing ch04_web-app.roo script creates flight-app Roo project, sets up Hibernate as persistence provider, configures MySQL as the database for the application, creates Flight and FlightDescription JPA entities and defines many-to-one relationship between Flight and FlightDescription entities. If you are using a different database than MySQL or your connection settings are different than what is specified in the script, then modify the script accordingly.


Step 2: Execute controller all command:

.. roo> controller all --package ~.web

The controller all command creates JSPX views, controllers, copies custom tags, default themes, and so on. The package argument specifies the location where the Spring Web MVC controllers are created by Roo.

Step 3. You can now convert the project into an Eclipse IDE project using perform eclipse command. The perform eclipse command generates .project and .classpath files required by Eclipse IDE.

Step 4. You can also run the flight-app application in embedded Tomcat container by exiting the Roo shell (by executing quit command) and executing mvn tomcat:run goal from the command prompt. Now, you access the web application by going to the following URL: http://localhost:8080/flight-app

The following figure shows how the scaffolded web application looks like:


By default, the scaffolded web application allows you to perform CRUD operations on JPA entities, and to search entities based on the dynamic finder methods that you added to them.