欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring Roo安装使用简介

程序员文章站 2023-12-20 20:53:40
一直以来,java/spring开发被认为是笨重的代表,无法快速生成项目原型和骨架。所以,spring推出了spring roo这个项目,帮助我们快速生成项目原型。本文参考...

一直以来,java/spring开发被认为是笨重的代表,无法快速生成项目原型和骨架。所以,spring推出了spring roo这个项目,帮助我们快速生成项目原型。本文参考自spring roo的官方文档,如果熟悉英文的话可以直接看原文档,内容更加丰富。

安装

安装命令行工具

spring roo是一套命令行工具,如果你使用的是eclipse/sts,还可以使用eclipse对应的插件。

首先先来下载命令行工具。到,选择对应版本下载。这里我选择的是最新的2.0.0.rc1 ,毕竟我有更新强迫症。下载完成之后解压,会得到一个文件夹,其中bin目录下就是spring roo的可执行文件了。可以看到它有bat和sh两种格式,可以在不同系统上运行。为了方便以后在终端窗口运行,我建议同时将这个文件夹添加到环境变量中。

安装好之后,打开命令提示符或者其他终端窗口,输入roo命令,就可以启动roo了。值得提一点,roo会在命令提示符对应的文件夹位置创建项目,所以如果需要在特定位置创建项目,先在命令提示符中切换到该文件夹,然后再启动roo。

spring Roo安装使用简介

安装eclipse插件

打开你的eclipse/sts,然后遵循以下步骤:

点击菜单栏 help ▸ install new software
点击 available software sites
点击 import 按钮
找到 “$roo_home/conf/sts-sites-bookmarks.xml” 并确定,这里$roo_home是你安装roo的目录
根据需要选择对应的版本
在过滤栏输入roo
选中功能 spring ide roo support
然后一路确定并允许条款
最后重启ide即可

下面是官方文档的图,如果有疑问照着这张图来就行了。

spring Roo安装使用简介

到这一步还没完,插件是安装好了,但是还没有配置。其实要配置的也很简单,告诉插件你的roo工具安装到哪里就行了。点击 window ▸ preferences ▸ spring ▸ roo support ,打开设置,然后照着官方文档截图设置好你的工具路径即可。

spring Roo安装使用简介

这样插件就设置完毕了。其实这个插件也没啥作用,就是在eclipse中开了一个窗口,能运行roo命令,和直接在命令提示符中运行其实是一样的。

使用roo

运行roo脚本

$roo_home\samples文件夹下有三个示例项目脚本,使用roo运行它们可以快速创建相应的项目。如果没有耐心,可以直接从这里开始。

spring Roo安装使用简介

比如说,我要运行clinic实例项目,就可以输入以下命令:

roo> script --file clinic.roo

稍等片刻,程序就会创建完毕。

spring Roo安装使用简介

最后创建出的是一个基于maven的spring boot程序。在idea下是这么一个样子。可以看到项目中有一个名字叫log.roo的日志文件,它记录了这个roo脚本执行的内容。另外不知道为什么程序在idea下会有一点报错,不过不影响编译和运行。

spring Roo安装使用简介

这个petclinic示例程序使用了spring security来保护页面。我查阅了一下,spring boot下spring security默认的用户名是user,密码则在程序启动的时候随机输出到控制台中。最后运行截图如下,大家可以自己运行和测试一下这个程序。

spring Roo安装使用简介

脚本解释

下面来解释一下petclinic这个程序的roo脚本,让我们来看看roo是如何工作的。这里只做一下简单解释,如果需要详细资料的话可以参考官方文档的附录,完整介绍了roo的各种命令和参数以及用法。

首先是创建项目并指定*包名,这样会创建一个基于maven的spring boot项目。

project setup --toplevelpackage org.springframework.roo.petclinic

然后是指定jpa存储类型,这里用的是hibernate,数据库是存储在内存的hsqldb。当然也可以使用其它数据库,不过相应地需要增加用户名等其他参数。

jpa setup --provider hibernate --database hypersonic_in_memory

然后是几个枚举类,将会在实体类中用到,这里的~指代前面设置的*包名。

enum type --class ~.domain.reference.pettype
enum constant --name dog
enum constant --name cat
enum constant --name bird

enum type --class ~.domain.reference.specialty
enum constant --name cardiology
enum constant --name dentistry
enum constant --name nutrition

然后是项目中的几个实体类。

entity jpa --class ~.domain.pet --sequencename pet_seq --entityformatexpression "#{name} (#{type})"
entity jpa --class ~.domain.visit --sequencename visit_seq --entityformatmessage visit_format
entity jpa --class ~.domain.abstractperson --abstract 
entity jpa --class ~.domain.vet --extends ~.domain.abstractperson --entityformatexpression "#{lastname} (#{specialty})"
entity jpa --class ~.domain.owner --extends ~.domain.abstractperson --entityformatexpression "#{lastname} (#{city})"

之后的叫本详细设置了每个实体类的属性以及对应关系,由于比较多所以我只挑选了几个典型的。在设置实体类之前,需要使用focus命令指定要设置的实体类。

focus --class ~.domain.pet
field boolean --fieldname sendreminders --notnull --primitive
field string --fieldname name --notnull --si* 1
field number --fieldname weight --type java.lang.float --notnull --min 0
field enum --fieldname type --type ~.domain.reference.pettype --notnull
field set --fieldname visits --type ~.domain.visit

focus --class ~.domain.abstractperson
field string --fieldname firstname --si* 3 --sizemax 30
field string --fieldname lastname --notnull --si* 3 --sizemax 30
field string --fieldname address --notnull --sizemax 50 --si* 1
field string --fieldname city --notnull --sizemax 30
field string --fieldname telephone --notnull
field string --fieldname homepage --sizemax 30
field string --fieldname email --sizemax 30 --si* 6
field date --fieldname birthday --type java.util.date --notnull

然后设置实体类之间的投影关系并设置jpa repository。

entity projection --class ~.domain.vetinfo --entity ~.domain.vet --fields id,firstname,lastname,specialty --entityformatexpression "#{firstname} #{lastname}"
repository jpa --entity ~.domain.vet --interface ~.repository.vetrepository --defaultreturntype ~.domain.vetinfo

repository jpa --all --package ~.repository
service --all --apipackage ~.service.api --implpackage ~.service.impl

然后是设置dto(数据传输对象),它和页面中的表单等信息对应,然后在后台转换为相应的实体类。在这里还可以指定finder,也就是查询条件,查询条件的规则请参考spring data jpa的相关内容。

dto --class ~.domain.petnameandweightformbean
field string --fieldname name
field number --fieldname weight --type java.lang.float
finder add --entity ~.domain.pet --name findbynameandweight --formbean ~.domain.petnameandweightformbean

然后是设置spring web mvc,这里指定thymeleaf作为视图层,并为所有控制器生成json和thymeleaf视图。

web mvc setup
web mvc view setup --type thymeleaf
web mvc controller --all --responsetype json
web mvc controller --all --responsetype thymeleaf

然后是生成查询和详情页面。这里针对前面设置的所有查询条件生成相应的查询页面,然后生成指定实体类的详情页面。最后指定了页面语言,目前好像只支持英语和西班牙语。

// publishing finders
web mvc finder --all --responsetype thymeleaf

// adding details
web mvc detail --entity ~.domain.owner --field pets --views list,show,findbycitylike --responsetype thymeleaf
web mvc detail --all --views list,show --responsetype thymeleaf

web mvc language --code es

然后使用了spring security保护了一下程序。第一行的是使用spring security的默认配置,用户名是user,密码是打印在控制台的随机字符串。第二行配置了一下用户权限,只有管理员角色的用户才能执行删除操作。

security setup --provider default

security authorize --class ~.service.impl.ownerserviceimpl --method delete.* --roles admin

然后启用了审计功能,程序会自动记录相应实体类的编辑时间和编辑者。

jpa audit setup
jpa audit add --entity ~.domain.pet
jpa audit add --entity ~.domain.owner
jpa audit add --entity ~.domain.visit
jpa audit add --entity ~.domain.vet

然后启用了web服务端点功能,这些端点可以在/servicesurl下查看。

ws endpoint --service ~.service.api.ownerservice --sei ~.ws.api.ownerwebservice --class ~.ws.endpoint.ownerwebserviceendpoint --config ~.config.wsendpointsconfiguration
ws endpoint --service ~.service.api.petservice --sei ~.ws.api.petwebservice --class ~.ws.endpoint.petwebserviceendpoint --config ~.config.wsendpointsconfiguration

最后自动为这些实体类和服务生成单元测试和集成测试。

// generating unitary tests for all entities
test unit --class ~.domain.owner
test unit --class ~.domain.pet
test unit --class ~.domain.vet
test unit --class ~.domain.visit

// repository integration tests
test integration --class ~.repository.vetrepository
test integration --class ~.repository.ownerrepository
test integration --class ~.repository.visitrepository
test integration --class ~.repository.petrepository

// controller integration tests
test integration --class ~.web.ownerscollectionjsoncontroller
test integration --class ~.web.petsitemjsoncontroller
test integration --class ~.web.vetscollectionthymeleafcontroller
test integration --class ~.web.visitsitemthymeleafcontroller

总结

以上就是本文关于spring roo安装使用简介的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

上一篇:

下一篇: