Skip to content

Spring ROO – Intro

January 25, 2010
tags: ,

Spring Roo (Real Object Oriented) è un Rapid Application Development  java da riga di comando.

Roo si utilizza solamente nella fase di sviluppo, nessun runtime è richiesto per il funzionamento in produzione, oltre che dalla shell può essere usato nello Spring Tool Suite.

Essenzialemente Roo esegue le richieste dell’ utente e per mezzo di add-on a cui fornisce le api per creare/modificare file su filesystem permette di geenrare delle applicazioni complete. Questi file possono essere file java, jsp, XML, aj, properites.

Con Roo possiamo generare una intera applicazione, partendo dalle classi di dominio e arrivando alla parte web con tanto di operazioni CRUD e finders, passando dalla persistenza, ai test alla autenticazione e authorizzazione. Si tratta quindi di un approccio DDD.

Vediamo ora come funziona la generazione assisitita della webapp da riga di comando.

Creiamo una directory dove creare la nostra applicazione e invochiamo roo.

    ____  ____  ____
   / __ \/ __ \/ __ \
  / /_/ / / / / / / /
 / _, _/ /_/ / /_/ /
/_/ |_|\____/\____/    1.0.0.RELEASE [rev 564]

Welcome to Spring Roo. For assistance press TAB or type "hint" then hit ENTER.
roo>

Con con l’hint e con il tab ci viene suggerito in base al punto di avanzamento del progetto quale azione effettuare e quali sono i comandi disponibili

roo> hint
Welcome to Roo! We hope you enjoy your stay!

Before you can use many features of Roo, you need to start a new project.

To do this, type 'project' (without the quotes) and then hit TAB.

Enter a --topLevelPackage like 'com.mycompany.projectname' (no quotes).
When you've finished completing your --topLevelPackage, press ENTER.
Your new project will then be created in the current working directory.

Note that Roo frequently allows the use of TAB, so press TAB regularly.
Once your project is created, type 'hint' and ENTER for the next suggestion.
You're also welcome to visit http://forum.springframework.org for Roo help.
roo>
*/            /*            //            ;             addon
date          development   exit          help          hint
metadata      poll          project       quit          reference
script        system        version
roo>

Decidiamo di creare un progetto, ci viene richiesto il package principale del progett, una volta inviato il comando verrà generata tutta la struttura del

progetto:

project --topLevelPackage it.pronetics.pizzeria
Created /Users/max/dev/workspace/roo-pronetics/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/META-INF/spring/log4j.properties
it.pronetic.pizzeria roo>

A questo punto se chiediamo un suggerimento ci viene detto di configurare la persistenza

it.pronetics.pizza roo> hint
Roo requires the installation of a JPA provider and associated database.

Type 'persistence setup' and then hit TAB three times.
We suggest you type 'H' then TAB to complete "HIBERNATE".
After the --provider, press TAB twice for database choices.
For testing purposes, type (or TAB) HYPERSONIC_IN_MEMORY.
If you press TAB again, you'll see there are no more options.
As such, you're ready to press ENTER to execute the command.

Once JPA is installed, type 'hint' and ENTER for the next suggestion.

utilizzando il tab per avere i suggerimenti, scegliamo hibernate come persitence provider e H2 in memory come db

it.pronetics.pizzeria roo> persistence setup --provider HIBERNATE --database H2_IN_MEMORY
Created SRC_MAIN_RESOURCES/META-INF/persistence.xml
Created SRC_MAIN_RESOURCES/META-INF/spring/database.properties
Managed SRC_MAIN_RESOURCES/META-INF/spring/applicationContext.xml
Managed ROOT/pom.xml

Terminata la scelta della persistenza, è arrivato il momento di far generare le classi di dominio della nostra applicazione

it.pronetics.pizzeria roo> hint
You can create entities either via Roo or your IDE.
Using the Roo shell is fast and easy, especially thanks to the TAB completion.

Start by typing 'ent' and then hitting TAB twice.
Enter the --class in the form '~.domain.MyEntityClassName'
In Roo, '~' means the --topLevelPackage you specified via 'create project'.

After specify a --class argument, press SPACE then TAB. Note nothing appears.
Because nothing appears, it means you've entered all mandatory arguments.
However, optional arguments do exist for this command (and most others in Roo).
To see the optional arguments, type '--' and then hit TAB. Mostly you won't
need any optional arguments, but let's select the --testAutomatically option
and hit ENTER. You can always use this approach to view optional arguments.

After creating an entity, use 'hint' for the next suggestion.
it.pronetics.pizzeria roo> entity --class Pizza
Created SRC_MAIN_JAVA/it/pronetics/pizzeria
Created SRC_MAIN_JAVA/it/pronetics/pizzeria/Pizza.java
Created SRC_MAIN_JAVA/it/pronetics/pizzeria/Pizza_Roo_Entity.aj
Created SRC_MAIN_JAVA/it/pronetics/pizzeria/Pizza_Roo_ToString.aj
Created SRC_MAIN_JAVA/it/pronetics/pizzeria/Pizza_Roo_Configurable.aj

Ora vediamo una delle parti più interessanti di Roo.
A differenza di altri tool, Roo combina la generazione passiva e attiva.
Quella passiva per creare qualcosa da shell, solitamente file java e xml,
quella attiva per costruire modelli utilizzando i metadati attraverso le annotazioni @Roo*,

e per aggiornare incrementalmente i file .aj e .jsp (mai quelli java).

In un articolo seguente vedremo in maniera approfondita come vengono usati gli aspect in Roo.

per ora ci limitiamo a dire che negli aspect mettiamo tutte le logiche proprie di un Rich domain model, nella classe java rimarranno

solo i campi e le annotazioni per richiamare le logiche contenute negli aspect.

L’aspectJ compiler farà in modo che nei .class sia contenuta tutta la logica che avremmo facendo il push-in

dagli aspect alla classe java.

Ora aggiungiamo dei campi al nostro oggetto Pizza, vediamo cosa ci viene suggerito scrivendo solo “fi” e usando il tab

~.Pizza roo> fi

field boolean          field date             field email template
field enum             field number           field other
field reference        field set              field string
finder add             finder list

Aggiungiamo un campo descrizione di tipo stringa e un campo prezzo di tipo numerico

~.Pizza roo> field string --fieldName descrizione
Managed SRC_MAIN_JAVA/it/pronetics/pizzeria/Pizza.java
Created SRC_MAIN_JAVA/it/pronetics/pizzeria/Pizza_Roo_JavaBean.aj
Managed SRC_MAIN_JAVA/it/pronetics/pizzeria/Pizza_Roo_ToString.aj
~.Pizza roo> field number --type java.math.BigDecimal --fieldName prezzo
Managed SRC_MAIN_JAVA/it/pronetics/pizzeria/Pizza.java
Managed SRC_MAIN_JAVA/it/pronetics/pizzeria/Pizza_Roo_JavaBean.aj
Managed SRC_MAIN_JAVA/it/pronetics/pizzeria/Pizza_Roo_ToString.aj

Dopo aver aggiunto questi campi vediamo cosa ci viene suggerito per proseguire

~.Pizza roo> hint
At this stage of the project, you have a few options:

 * List all hint topics via 'hint topics'
 * Create more fields with 'hint fields'
 * Create more entities with 'hint entities'
 * Create a web controller with 'hint controllers'
 * Create dynamic finders with 'hint finders'
 * Setup your logging levels via 'hint logging'
 * Run tests via Maven (type 'exit' and then 'mvn test')
 * Build a deployment artifact (type 'perform package')
 * Learn about Eclipse integration by typing 'hint eclipse'
 * Discover all Roo commands by typing 'help'
~.Pizza roo>

Scegliamo di creare dei controller in modo da completare la paarte web che opera sulla nostra entità Pizza.

~.Pizza roo> hint controllers
Creating RESTful Spring MVC web controllers is quick and easy using Roo.
Controllers can be made that automatically expose an entity. Alternately, we
can create a stub, empty controller for you to finish off.

For the former, type 'controller scaff' and hit TAB three times.
The --class is the controller name; it need not reflect an entity name.
We suggest putting controllers under a '~.web' package to improve maintenance.
You can also specify the --entity the controller should expose.

After creating a controller, use 'hint' for further suggestions.

Scegliamo di far generare il controller e tutta l’insfrastruttura della webapp con i seguenti comandi:

~.Pizza roo> controller scaffold --class it.pronetics.pizzeria.web.PizzaController
Created SRC_MAIN_JAVA/it/pronetics/pizzeria/web
Created SRC_MAIN_JAVA/it/pronetics/pizzeria/web/PizzaController.java
Created SRC_MAIN_WEBAPP/WEB-INF/spring
Created SRC_MAIN_WEBAPP/WEB-INF/spring/webmvc-config.xml
Created SRC_MAIN_JAVA/it/pronetics/pizzeria/web/PizzaController_Roo_Controller.aj
Created SRC_MAIN_WEBAPP/images
Created SRC_MAIN_WEBAPP/images/nl.png
Created SRC_MAIN_WEBAPP/images/update.png
Created SRC_MAIN_WEBAPP/images/es.png
Created SRC_MAIN_WEBAPP/images/delete.png
Created SRC_MAIN_WEBAPP/images/add.png
Created SRC_MAIN_WEBAPP/images/resultset_previous.png
Created SRC_MAIN_WEBAPP/images/resultset_next.png
Created SRC_MAIN_WEBAPP/images/favicon.ico
Created SRC_MAIN_WEBAPP/images/banner-graphic.png
Created SRC_MAIN_WEBAPP/images/resultset_last.png
Created SRC_MAIN_WEBAPP/images/gb.png
Created SRC_MAIN_WEBAPP/images/springsource-logo.png
Created SRC_MAIN_WEBAPP/images/it.png
Created SRC_MAIN_WEBAPP/images/show.png
Created SRC_MAIN_WEBAPP/images/sv.png
Created SRC_MAIN_WEBAPP/images/list.png
Created SRC_MAIN_WEBAPP/images/resultset_first.png
Created SRC_MAIN_WEBAPP/images/de.png
Created SRC_MAIN_WEBAPP/styles
Created SRC_MAIN_WEBAPP/styles/standard.css
Created SRC_MAIN_WEBAPP/styles/alt.css
Created SRC_MAIN_WEBAPP/WEB-INF/classes
Created SRC_MAIN_WEBAPP/WEB-INF/classes/standard.properties
Created SRC_MAIN_WEBAPP/WEB-INF/classes/alt.properties
Created SRC_MAIN_WEBAPP/WEB-INF/layouts
Created SRC_MAIN_WEBAPP/WEB-INF/layouts/default.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/layouts/layouts.xml
Created SRC_MAIN_WEBAPP/WEB-INF/views
Created SRC_MAIN_WEBAPP/WEB-INF/views/views.xml
Created SRC_MAIN_WEBAPP/WEB-INF/views/resourceNotFound.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/views/index.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/views/uncaughtException.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/views/dataAccessFailure.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/views/controller-index.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/tags
Created SRC_MAIN_WEBAPP/WEB-INF/tags/language.tagx
Created SRC_MAIN_WEBAPP/WEB-INF/tags/theme.tagx
Created SRC_MAIN_WEBAPP/WEB-INF/tags/pagination.tagx
Created SRC_MAIN_WEBAPP/WEB-INF/i18n
Created SRC_MAIN_WEBAPP/WEB-INF/i18n/messages_sv.properties
Created SRC_MAIN_WEBAPP/WEB-INF/i18n/messages.properties
Created SRC_MAIN_WEBAPP/WEB-INF/i18n/messages_nl.properties
Created SRC_MAIN_WEBAPP/WEB-INF/i18n/messages_it.properties
Created SRC_MAIN_WEBAPP/WEB-INF/i18n/messages_es.properties
Created SRC_MAIN_WEBAPP/WEB-INF/i18n/messages_de.properties
Created SRC_MAIN_WEBAPP/WEB-INF/i18n/application.properties
Managed SRC_MAIN_WEBAPP/WEB-INF/i18n/application.properties
Created SRC_MAIN_WEBAPP/WEB-INF/views/pizza
Created SRC_MAIN_WEBAPP/WEB-INF/views/pizza/list.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/views/pizza/show.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/views/pizza/create.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/views/menu.jspx
Managed SRC_MAIN_WEBAPP/WEB-INF/i18n/application.properties
Managed SRC_MAIN_WEBAPP/WEB-INF/views/menu.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/views/pizza/update.jspx
Managed SRC_MAIN_WEBAPP/WEB-INF/i18n/application.properties
Managed SRC_MAIN_WEBAPP/WEB-INF/views/menu.jspx
Created SRC_MAIN_WEBAPP/WEB-INF/views/pizza/views.xml
Created SRC_MAIN_WEBAPP/WEB-INF/urlrewrite.xml
Created SRC_MAIN_WEBAPP/WEB-INF/web.xml
Managed SRC_MAIN_WEBAPP/WEB-INF/web.xml
Managed ROOT/pom.xml

Per concludere configuriamo la parte della sicurezza

~.Pizza roo> security setup
Managed ROOT/pom.xml
Created SRC_MAIN_RESOURCES/META-INF/spring/applicationContext-security.xml
Created SRC_MAIN_WEBAPP/WEB-INF/views/login.jspx
Managed SRC_MAIN_WEBAPP/WEB-INF/views/views.xml
Managed SRC_MAIN_WEBAPP/WEB-INF/web.xml
Managed SRC_MAIN_WEBAPP/WEB-INF/spring/webmvc-config.xml</pre>

Ora chiudiamo roo avviamo tomcat e apriamo il browser

~.Pizza roo> q
magicbox:roo-pronetics max$ mvn tomcat:run

all’ indirizzo http://localhost:8080/pizzeria per vedere questo:

Prossimamente vedremo in maniera approfondita e maggiormente tecnica il funzionamento interno di Roo e degli add-on

Author: Massimiliano Dessì

Advertisements
One Comment leave one →
  1. June 4, 2010 9:53 am

    L’ho iniziato ad usare da qualche giorno e mi sta piacendo moltissimo, penso che possa veramente velocizzare lo sviluppo di applicazioni web java ed inoltre per me Spring continua a rimanere un punto di riferimento essenziale.
    Voi avete già usato Roo per qualche progetto?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: