Quick Guide to Spring Roo – Spring的鲁的快速指南

最后修改: 2017年 5月 4日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Spring Roo is a Rapid Application Development (RAD) tool that aims to deliver fast, and instant results focused on Spring web applications and newer Spring technologies. It allows us to generate boilerplate code and project structure for Spring applications with simple to use commands.

Spring Roo是一个快速应用开发(RAD)工具,旨在提供快速、即时的结果,专注于Spring网络应用和更新的Spring技术。它允许我们用简单的命令为Spring应用程序生成模板代码和项目结构。

Roo can be used as a standalone application running from the operating system command line. There’s no requirement to use Eclipse, Spring Tool Suite (STS) or any other IDE; in fact, we can use any text editor to write code!

Roo可以作为一个独立的应用程序从操作系统的命令行运行。不需要使用Eclipse、Spring Tool Suite(STS)或任何其他IDE;事实上,我们可以使用任何文本编辑器来编写代码

However, for simplicity, we will use STS IDE with the Roo Extension.

然而,为了简单起见,我们将使用带有Roo扩展的STS IDE。

2. Installing Spring Roo

2.安装Spring Roo

2.1. Requirements

2.1.要求

To follow this tutorial, these have to be installed:

为了遵循本教程,必须安装这些东西。

  1. Java JDK 8
  2. STS
  3. Spring Roo

2.2. Installation

2.2.安装

Once we download and install Java JDK and STS, we need to unzip Spring Roo and add it to the system path.

一旦我们下载并安装了Java JDK和STS,我们需要解压Spring Roo并将其添加到系统路径中。

Let’s create the ROO_HOME environment variable and add %ROO_HOME%\bin to the path.

让我们创建ROO_HOME环境变量并将%ROO_HOME%/bin添加到路径中。

To verify that Roo is installed correctly, we can open the command line and execute following commands:

为了验证Roo是否正确安装,我们可以打开命令行并执行以下命令。

mkdir baeldung
cd baeldung
roo quit

After a few seconds we will see:

几秒钟后,我们将看到。

                _
 ___ _ __  _ __(_)_ __   __ _   _ __ ___   ___
/ __| '_ \| '__| | '_ \ / _` | | '__/ _ \ / _ \
\__ \ |_) | |  | | | | | (_| | | | | (_) | (_) |
|___/ .__/|_|  |_|_| |_|\__, | |_|  \___/ \___/
    |_|                 |___/          2.0.0.RC1

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

Roo is installed, and it is working. Please note that Spring Roo version will vary and the steps and instructions may depend on the actual version used.

Roo已经安装,并且正在工作。请注意,Spring Roo的版本会有所不同,步骤和说明可能取决于实际使用的版本。

Important: Spring Roo 2.0 is not backward compatible with 1.x.

重要提示:Spring Roo 2.0不向后兼容1.x。

2.3. Adding and Configuring STS Extension

2.3.添加和配置STS扩展

Out-of-the-box, STS supports a development of Spring applications and includes ready-to-use extensions. However, Spring Roo extension is not included. Therefore, we will need to add it manually.

开箱即用,STS支持Spring应用程序的开发,并包括随时可用的扩展。然而,Spring Roo扩展并不包括在内。因此,我们将需要手动添加它。

In STS let’s go to Install New Software and import bookmarks to Available Software Sites. Currently, bookmarks are in the %ROO_HOME%\conf folder. Once we imported the bookmarks, we can search for simply roo and install the latest version of Spring IDE Roo Support. In the end, we will be asked to restart STS.

在STS中,让我们进入安装新软件,将书签导入可用的软件网站。目前,书签在%ROO_HOME%\conf文件夹中。一旦我们导入了书签,我们就可以简单地搜索roo并安装最新版本的Spring IDE Roo Support。最后,我们会被要求重新启动STS。

For detailed and up-to-date steps we can always go and check Spring Roo Getting Started documentation.

对于详细和最新的步骤,我们可以随时去查看Spring Roo入门文档。

Once we have installed Roo Support in STS, we need to set up the extension. It is as easy as pointing Roo Support to %ROO_HOME% folder. Again, Spring Roo Getting Started gives detailed steps how to do it.

一旦我们在STS中安装了Roo Support,我们就需要设置这个扩展。这就像把Roo Support指向%ROO_HOME%文件夹一样简单。同样,Spring Roo入门给出了如何操作的详细步骤。

Now we can go to “Window” application menu once more and select Show View > Roo Shell.

现在我们可以再次进入 “窗口 “应用程序菜单,选择显示视图> Roo Shell.

3. First Project

3.第一个项目

3.1. Setting up a Project in STS

3.1.在STS中设置一个项目

In STS let’s open the Roo Shell window and click on Create New Roo Project icon. This will open a New Roo Project window.

在STS中,让我们打开Roo Shell窗口并点击Create New Roo Project图标。这将打开一个新Roo项目窗口。

We will name the project roo and use com.baeldung as our top level package name. We can leave all other default values and proceed to the end to create a new project using Roo.

我们将项目命名为roo,并使用com.baeldung作为我们的顶级包名。我们可以保留所有其他的默认值,并继续进行到最后,使用Roo创建一个新项目。

In STS this will run the following command for us:

在STS中,这将为我们运行以下命令。

project setup --topLevelPackage com.baeldung --projectName "roo" --java 8 --packaging JAR

As already mentioned, we don’t need an IDE, and we could have run that command from Roo Shell ourselves! For simplicity, we are using built-in features of STS.

正如已经提到的,我们不需要IDE,我们可以自己从Roo Shell中运行该命令!我们可以使用STS的内置功能。为了简单起见,我们使用STS的内置功能。

If we get the following error:

如果我们得到以下错误。

Could not calculate build plan: Plugin org.codehaus.mojo:aspectj-maven-plugin:1.8 
  or one of its dependencies could not be resolved: 
  Failed to read artifact descriptor for org.codehaus.mojo:aspectj-maven-plugin:jar:1.8

the easiest way to fix it is to manually edit pom.xml file and update aspectj.plugin.version from 1.8 to 1.9:

最简单的方法是手动编辑pom.xml文件,将aspectj.plugin.version1.8升级到1.9

<aspectj.plugin.version>1.9</aspectj.plugin.version>

At this stage, there shouldn’t be any errors in the project, and there will be a few auto-generated files for us.

在这个阶段,项目中不应该有任何错误,而且会有一些自动生成的文件给我们。

3.2. Roo Shell

3.2.Roo Shell

Now it is time to familiarize with the Roo Shell. Spring Roo’s primary user interface is, in fact, command prompt!

现在是时候熟悉一下Roo Shell了。Spring Roo的主要用户界面实际上是命令提示符

Therefore, let’s go back to Roo Shell window. In it, let’s run the first command by typing ‘h’ and pressing CTRL+SPACE:

因此,让我们回到Roo Shell窗口。在其中,让我们通过输入’h’并按CTRL+SPACE来运行第一个命令。

roo> h

help    hint

Roo will automatically suggest and autocomplete commands for us. We can type ‘hi’, press CTRL+SPACE, and Roo will auto-suggest hint command.

Roo会自动为我们建议和自动完成命令。我们可以输入’hi’,按CTRL+SPACE,Roo会自动建议hint命令。

Another great feature of Roo Shell is the context awareness. For example, the output of the hint command will change depending on the previous input.

Roo Shell的另一个伟大功能是上下文意识。例如,hint命令的输出将根据先前的输入而改变。

Let’s now execute the hint command and see what happens:

现在让我们执行hint命令,看看会发生什么。

roo> hint 
Roo requires the installation of a persistence configuration.

Type 'jpa setup' and then hit CTRL+SPACE. We suggest you type 'H'
then CTRL+SPACE to complete "HIBERNATE".

After the --provider, press CTRL+SPACE for database choices.
For testing purposes, type (or CTRL+SPACE) HYPERSONIC_IN_MEMORY.
If you press CTRL+SPACE 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.

It gives us the next steps we need to perform. Let’s add a database now:

它给出了我们需要执行的下一个步骤。现在让我们添加一个数据库。

roo> jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY 
Created SRC_MAIN_RESOURCES\application.properties
Updated SRC_MAIN_RESOURCES\application.properties
Updated SRC_MAIN_RESOURCES\application-dev.properties
Updated ROOT\pom.xml [added dependencies org.springframework.boot:spring-boot-starter-data-jpa:, org.springframework.boot:spring-boot-starter-jdbc:, org.hsqldb:hsqldb:; added property 'springlets.version' = '1.2.0.RC1'; added dependencies io.springlets:springlets-data-jpa:${springlets.version}, io.springlets:springlets-data-jpa:${springlets.version}; added dependencies io.springlets:springlets-data-commons:${springlets.version}, io.springlets:springlets-data-commons:${springlets.version}]

At this stage, we will need to execute some commands. Between each of them, we can always run hint command to see what is suggested by Roo. This is a very useful feature.

在这个阶段,我们将需要执行一些命令。在每个命令之间,我们总是可以运行hint命令,看看Roo建议的内容。这是一个非常有用的功能。

Let’s run the commands first, and we will go through them afterward:

让我们先运行这些命令,之后我们再去看。

roo> 
entity jpa --class ~.domain.Book
field string --fieldName title --notNull 
field string --fieldName author --notNull 
field string --fieldName isbn --notNull 
repository jpa --entity ~.domain.Book
service --all 
web mvc setup
web mvc view setup --type THYMELEAF 
web mvc controller --entity ~.domain.Book --responseType THYMELEAF

We are now ready to run our application. However, let’s step back through the commands to see what we have done.

我们现在已经准备好运行我们的应用程序了。然而,让我们回过头来看看我们都做了些什么。

First, we created a new JPA persistent entity in the src/main/java folder. Next, we created three String fields in Book class, gave them a name and set to be not null.

首先,我们在src/main/java文件夹中创建了一个新的JPA持久化实体。接下来,我们在Book类中创建了三个String字段,给它们一个名字并设置为不null

After that, we have generated Spring Data repository for the specified entity and created a new service interface.

之后,我们为指定的实体生成了Spring Data资源库,并创建了一个新的服务接口。

In the end, we included Spring MVC configuration, installed Thymeleaf and created a new controller that manages our entity. Because we have passed Thymeleaf as the response type, the generated methods and views will reflect this.

最后,我们包含了Spring MVC配置,安装了Thymeleaf,并创建了一个新的控制器来管理我们的实体。因为我们将Thymeleaf作为响应类型,生成的方法和视图将反映这一点。

3.3. Running the Application

3.3.运行应用程序

Let’s refresh the project and right click on roo project and select Run As > Spring Boot App.

让我们刷新项目,右击roo项目,选择Run As > Spring Boot App

Once the application has started, we can open a web browser and go to http://localhost:8080. Next, to Roo icon, we will see Book menu and underneath two options: Create Book and List Books. We can use this to add a book to our application and view the list of added books.

一旦应用程序启动,我们可以打开一个网络浏览器,进入http://localhost:8080。在Roo图标的旁边,我们会看到Book菜单,下面有两个选项。创建书籍列出书籍。我们可以用它来向我们的应用程序添加一本书,并查看所添加的书的列表。

3.4. Other Features

3.4.其他特点

When we open Book.java class file, we’ll notice that the class is annotated with @Roo annotations. These were added by Roo Shell and are used to control and customize the content of AspectJ inter-type declaration (ITD) files. We can view the files in Package Explorer in STS by unselecting “Hide generated Spring Roo ITDs” filter in View Menu, or we can open the files directly from the file system.

当我们打开Book.java类文件时,我们会注意到该类有@Roo注解。这些是由Roo Shell添加的,用于控制和定制AspectJ类型间声明(ITD)文件的内容。我们可以通过取消选择视图菜单中的 “隐藏生成的Spring Roo ITD “过滤器,在STS的包资源管理器中查看这些文件,或者我们可以直接从文件系统中打开这些文件。

Roo annotations have SOURCE retention policy. This means the annotations won’t be present in compiled class bytecode, and there won’t be any dependency to Roo in deployed applications.

Roo注解有SOURCE保留策略。这意味着注释不会出现在编译的类字节码中,而且在部署的应用程序中不会有任何对Roo的依赖。

Another, obviously missing part in the Book.java class are getters and setters. These are stored in separate AspectJ ITDs files, as already mentioned. Roo will actively maintain this boilerplate code for us. Therefore, changes to fields in any class will be automatically reflected in AspectJ ITDs as Roo is “monitoring” all changes – either done via Roo Shell or directly by a developer in IDE.

Book.java类中另一个明显缺少的部分是getterssetters。如前所述,这些都存储在单独的AspectJ ITD文件中。Roo会主动为我们维护这些模板代码。因此,任何类中字段的变化都会自动反映在AspectJ ITD中,因为Roo正在 “监视 “所有的变化–无论是通过Roo Shell还是直接由IDE中的开发者完成。

Roo will take care of the repetitive code like toString() or equals() methods too.

Roo也会处理重复的代码,如toString()equals()方法。

Moreover, the framework can be easily removed from the project, avoiding vendor lock-in, by removing annotations and pushing-in AspectJ ITD to standard java code.

此外,通过删除注解和将AspectJ ITD推入标准java代码,该框架可以很容易地从项目中移除,避免了厂商锁定。

4. Conclusion

4.结论

In this quick example, we managed to install and configure Spring Roo in STS and created a small project.

在这个快速的例子中,我们设法在STS中安装和配置Spring Roo并创建了一个小项目。

We used Roo Shell to set it up and didn’t need to write a single line of actual Java code! And we were able to get a working application prototype in a few minutes and Roo took care of all boilerplate code for us.

我们用Roo Shell来设置它,不需要写一行实际的Java代码!而且我们能够在几分钟内得到一个工作的应用程序原型,Roo为我们处理了所有的模板代码。

As always, the code used throughout the discussion can be found over on GitHub.

一如既往,整个讨论中所使用的代码可以在GitHub上找到超过