Introduction
There are number of occasions when an appropriate design needs to be applied to an existing J2EE architecture to solve different problems. For example MVC design is the one that is commonly used for interaction of Model, View and Controller components. The predefined interactions between the objects to solve a common problem is a Design Pattern. Another important aspect that should be considered while building J2EE applications is the automation of common functions. Using Frameworks in J2EE applications helps automate common functions like transaction management, logging, security etc. Hence a framework can be defined as,"A set of configurable foundation components that automate basic elements of an application"
A framework takes meta-data as an input to the “framework” services that automate and drive the behaviour of J2EE components. Following snapshot depicts an overview of what Spring Framework offers to its users.
Spring Roo that uses maven for building and managing spring-based projects can be described as,
"A command line intelligent code-completion tool that can save developers time by doing most of the tedious coding itself."
This article focuses more on the common cases where Spring roo can play its great role in rapid application development.
Basic Requirements
Maven (Should be installed first if not already installed)
Spring roo
Note: Once Spring roo is installed add it to system path variable and go to the command line. Now change directory to the root location of the project. From there you can run different roo commands to generate code within your project.
Set up Spring Roo project by running the following command,
project --topLevelPackage myTopLevelPackage
The above command will create the project structure with the given top level package.
Setting up ORM framework
Run following command to set up hibernate in your project.
persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
After the above command is executed we shall notice that Roo has added following items in the project.
- Several new dependencies in pom.xml for Hibernate, Hibernate JPA 2.0, Commons Pool, Common DBCP, Spring dependencies including Spring JDBC and transaction support.
- JPA persistence entity information file (src/main/resources/META-INF/persistence.xml)
- Spring application context file (src/main/resources/META-INF/spring/applicationContext.xml)
- Repository to pom.xml file where new dependencies can be found.
Database Reverse Engineering
First create the database schema in MySQL installation. For setting up the persistence specific to your application run following command.
persistence setup --provider HIBERNATE --database MYSQL --databaseName schemaName --userName userId --password password
In order to see the meta data of the selected schema on the roo shell console run the following command.
introspect --schema schemaName
Now run the following command that does the actual reverse engineering work.
reverse engineer --schema schemaName --package ~.packageName
After the execution of above command all the entities will be generated inside the given package.
Editing JPA Entity
While editing a JPA entity in Spring Roo project and adding a field of interest, the corresponding accessor and mutator pair for that field are automatically added to a shadow class definition in the background. Similarly, it will implement a toString() or/and equals() definitions (reflecting the fields added) if one does not already exist. If you update the field, the accessor and mutator as well as the equals and toString methods are also updated. If you add an equals method to the JPA entity, the shadow definition is removed, delegating to your implementation instead. So, this shadow class definition is kept in sync, responding to your changes, but it does not get in your way.
Developing Web Application
Create a controller class by executing following command from the roo shell.
controller scaffold ~.web.MyController
This command takes more time to execute as it does a lot of things in the background. The first time Spring Roo generates a web artifact for you, it automatically upgrades your Roo project to be a web project. Spring Roo will set up a default web project that will install the correct configuration and numerous files that we’ll reuse in the rest of the project, such as images, JavaScript assets, styles, and JSP tag libraries. It’ll correctly update the Maven build to have all the dependencies that are required.
Application Logging
Run following to set up the application logging.
logging setup --level INFO
This single commands adds the log4j.properties file with appropriate logging level configured.
Application Security
Configured a Spring MVC application is a must, otherwise the security commands will not be available on the roo shell. Run following to set up the application security.
security setup
The above command adds following items to the project.
- The appropriate Spring Security Maven dependencies to the pom.xml file
- A new Spring configuration file (src/main/resources/META-INF/spring/applicationContext-security.xml)
- A login.jspx page, and a new Tiles definition (view.xml)
- The Spring Security servlet to web.xml, and configured extra URL protection in webmvc-config.xml.
The core security configuration resides in the applicationContext-security.xml.
Once this configuration is in place, you can more granularly restrict access to specific fragments of pages. For example, you might wish to restrict which menu options are shown to people in the menu. Open up the menu.jspx page, and then add the JSP tag namespace for the Spring Security tag.
xmlns:sec="http://www.springframework.org/security/tags"
For the specific parts of the page that you wish to restrict, enclose it with this new tag,like this:
<sec:authorize ifAllGranted="ROLE_ADMIN">
...
</sec:authorize>
With these simple changes, we’ve already introduced URL-security, login management and page fragment security.
Application Testing
When creating entities, you can eaisly add integration tests for the created entities by appending the --testAutomatically to the command, like this:
entity --class ~.domain.Customer --testAutomatically
You can add the integration tests retroactively, too, if you forget to specify this option at creation time or you had the classes generated using database reverse engineering.
test integration --entity ~.domain.Customer
This will generate an integration test (src/test/java/com/crmco/crm/domain/CustomerIntegrationTest.java).
Integration tests are the most typical type of unit test, but not the only one. Roo also supports mock tests as well as interaction testing with Selenium. Mock testing describes the art of testing interactions between APIs by delegating API calls to dumb objects that record the interactions and check that they meet test expectations. You can create simple mock test, quickly, like this:
test mock
This will create a single mock test, which you can then build on.
Interface Testing
It should not be surprising then that Spring Roo makes it very simple to build Selenium tests. Roo can generate your Selenium test for any controller, like this:
selenium test --controller ~.web.CustomerController
This modifies the Maven configuration for the project and installs support for the Maven plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>selenium-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<suite>src/main/webapp/selenium/test-suite.xhtml</suite>
<browser>*firefox</browser>
<results>${project.build.directory}/selenium.html</results>
<startURL>http://localhost:4444/</startURL>
</configuration>
</plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>selenium-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<suite>src/main/webapp/selenium/test-suite.xhtml</suite>
<browser>*firefox</browser>
<results>${project.build.directory}/selenium.html</results>
<startURL>http://localhost:4444/</startURL>
</configuration>
</plugin>
This will generate tests for the controller in the src/main/webapp/selenium folder. You can run these tests through Maven using the following command:
mvn selenium:selenese
The above maven command will launch Firefox and run the tests for you.
Email Support
Suppose we are using Gmail as our SMTP server to focus on sending email using Roo. Adding email support to an application is done using the following command.
The email sender command installs a Spring JavaMailSender in your project. You can change email-related properties in the email.properties file.
email sender setup --hostServer smtp.gmail.com --username \
<Your email address> --password <Your email password> --port 587 --protocol SMTP
The email sender command installs a Spring JavaMailSender in your project. You can change email-related properties in the email.properties file.
Internationalization Support
Spring Roo adds internationalization support by using the web mvc install language command, which installs a new language in your application. For example, the commands for Spanish and Italian are:
web mvc install language --code es
web mvc install language --code it