Spring Roo is easy to install as standalone tool and runs on Windows, Mac OSX and Linux alike. Integrated development environments (IDEs) for Java such as the SpringSource Tool Suite (STS), Eclipse (+ AJDT) and JetBrains IntelliJ also integrate Spring Roo directly into their toolset.
The only prerequisites for running the Spring Roo shell are a working Java 6 SDK and Apache Maven (2.0.9+) installation. Once these two are installed, you can go ahead and download and unpack the Spring Roo distribution. You can find the ~7MB download for the latest release of Spring Roo at http://www.springsource.com/ download/community?project=Spring%20Roo. Once downloaded, simply unpack it into and add it to your path. Windows users can add %ROO_HOME%\bin to their path and *nix users can create a symbolic link to $ROO_HOME/bin/roo.sh.
Alternatively, you can install STS, which includes an integrated version of the Spring Roo shell without any need for further configuration.
Once this is complete, you can simply start your Spring Roo shell in a fresh directory:
The Spring Roo Shell
The Spring Roo shell offers hinting support for newcomers. This way you can learn about the suggested steps beginning with the creation of a new project, followed by configuration of persistence features to the creation of your first domain classes.
Tab completion plays a central role when using the Spring Roo shell. By simply hitting the Tab key, the Roo shell will offer commands, command attributes or even brief comments about the functionality offered by a command or a command attribute. Typing the first letters of a command followed by Tab will complete the command. Subsequent uses of Tab will complete mandatory command attributes until a user selection or input is required. Note, the integrated Roo shell in STS uses the Ctrl + Space key combination familiar to Eclipse users in lieu of the Tab completion in the standalone Roo shell.
The Roo shell will automatically hide commands, which are not applicable in the current context. For example, the ‘persistence setup’ command will not be shown when using Tab before a new project has been created using the ‘project’ command. Roo also offers help for any of the commands it offers. To see help details for the ‘persistence setup’ command, simply type ‘help persistence’ and Roo will present a short description and also all mandatory and optional command attributes along with a brief description for each. Finally, the Roo shell will automatically rollback changed files in case a problem is encountered.
Your First Roo Project
The first action to take in a new Spring Roo managed project is to create a new project:
roo> project –topLevelPackage net.addressbook
Created /home/sschmidt/AddressBook/pom.xml
Created SRC_MAIN_JAVA
Created SRC_MAIN_RESOURCES
Created SRC_TEST_JAVA
Created SRC_TEST_RESOURCES
Created SRC_MAIN_WEBAPP
Created SRC_MAIN_RESOURCES/META-INF/spring
Created SRC_MAIN_RESOURCES/META-INF/spring/applicationContext.xml
Created SRC_MAIN_RESOURCES/log4j.properties
As you can see, Roo has created a standard Maven project layout with a Spring application context and a Log4J configuration. After the creation of the project, you can type ‘hint’ and follow the suggestions for your next steps. Note, the integrated ‘hinting’ support is context sensitive. This means that the ‘hint’ command is aware of state of your project and adjusts its suggestions accordingly. For example, after the creation of a new project, the ‘hint’ command will suggest to configure your persistence setup.
Then in the same fashion, you can create your first domain entity (along with a JUnit integration test) and create a few fields for it:
roo> entity --class ~.domain.Person --testAutomatically
~.domain.Person roo> field string --fieldName name
~.domain.Person roo> field date --fieldName birthDay --type java.util.Date
Of course, you can refine your domain model through the Spring Roo shell or directly in your .java source code, create new types, fields, references, etc.
Once your work is complete, you may want to test the integrity of your domain model by running the Roo generated integration tests. You can do that by running ‘perform tests’ in the Roo shell, ‘mvm test’ in your normal shell or through your IDE. The integration tests will execute against your configured database and the test data will be cleaned up automatically after the tests have completed.
Taking a Look at the Source Code
A quick look at Person.java reveals the advantage of using
AspectJ ITDs:
package net.addressbook.domain;
import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import java.util.Date;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.springframework.format.annotation.DateTimeFormat;
@RooJavaBean
@RooToString
@RooEntity
public class Person {
private String name;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = “S-”)
private Date birthDay;
}
As mentioned before, the repetitive boilerplate code for things like the toString() method, mutators and accessors are managed by Spring Roo in separate AspectJ ITDs in the background, leaving you with a more concise code artifact that is much easier to manage and comprehend. Roo has also taken care of correctly configured JPA annotations (such as @Temporal) and Spring annotations (such as @DateTimeFormat) to your fields where appropriate to provide correct persistence and conversion behavior. You’ll notice the @RooToString annotation has also been added automatically that triggers Roo to produce the following ITD:
package net.addressbook.domain;
import java.lang.String;
privileged aspect Person_Roo_ToString {
public String Person.toString() {
StringBuilder sb = new StringBuilder();
sb.append(“Name: “).append(getName()).append(“, “);
sb.append(“BirthDay: “).append(getBirthDay());
return sb.toString();
}
}
You will notice that the toString() method looks just like a normal Java method with the only difference of the AspectJ target type definition in front of the method name. Furthermore, AspectJ type definitions use the ‘privileged aspect’ definition instead of ‘public class’ you would see in typical Java sources.
As discussed in the previous section, Spring Roo integrates all persistence related functionality through an ITD into the target type. This allows for a lean architecture without compromising on functionality or flexibility. Take a look into the Person_Roo_Entity.aj ITD to see the code generated and maintained by Roo. By default Roo will generate the following methods to support persisting your Person domain entity (listing contains the most relevant methods only):
Method Name |
Purpose |
void persist() |
Save a new person record. |
Person merge() |
Update an existing person record. |
void remove() |
Remove an existing person record. |
Person findPerson(ID) |
Find a person record by ID. |
List<Person> findAllPeople() |
Retrieve a list of all persistent Person records. |
List<Person> findPersonEntries(from, to) |
Retrieve a list of persistent Person records for a given range. |
long countPeople() |
Return a count of all Person records. |
roo> controller all --package ~.web
This command will automatically scaffold a complete Spring MVC Web UI which features:
- Best-practice Spring MVC out of the box
- REST support (JSON integration optional)
- Round tripping of view artifacts (jspx)
- Customizable tag library (tags)
- Integrated templating support (Tiles)
- Theming support (Spring MVC & CSS)
- Pagination support
- i18n & i11n built-in
- Form validation based on JSR 303
- Rich Dojo UI widgets for data selectors, etc
You can easily deploy your Web application to the configured Tomcat or Jetty Web containers in your project by issuing ‘mvm tomcat:run’ or ‘mvm jetty:run’ respectively and then navigate to your Web application in a Web browser:
If you prefer a UI scaffolded using Google Web Toolkit (GWT), you can issue the following command:
This command will provide a complete GWT-based UI with similar features to the MVC UI shown above:
Common Commands
Let’s recap what you have done so far by issuing just a few commands. You have created a fully configured and working enterprise Java application using Maven, Spring Framework with features such as dependency injection, transaction support, MVC, JSP, Tiles, CSS, Dojo, JPA, AspectJ, Java Bean Validation (server and client side), JUnit, Log4J and more.
The following table summarizes some of the common commands you should know about to get started with a new Spring Roo project:
Command |
Purpose |
project --topLevelPackage [..] |
Creates a new Spring Roo managed project. |
persistence setup --provider [..] --database [..] |
Installs or update a persistence provider and a database connection for your project. |
entity --class [..] |
Creates a new persistent entity in your project. |
field string | number | boolean | date | set | [..] |
Inserts a private field into the specified java file. |
enum type | constant [..] |
Inserts enum types and constants into your project. |
test integration | mock | stub |
Creates a new integration | mock | stub tests for the specified entity. |
controller all | scaffold [..] |
Creates a Spring MVC UI for your domain model. |
gwt setup |
Creates a GWT UI for your domain model. |
security setup |
Installs a Spring Security configuration for your project. |
logging setup --level [..] |
Configures logging in your project. |
For a more complete reference of all available commands (and command options), refer to the command index at:
http://static.springsource.org/spring-roo/reference/html-single/index.html#command-index
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}