一直以來,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。
安裝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即可
下面是官方文檔的圖,如果有疑問照著這張圖來就行了。
到這一步還沒完,插件是安裝好了,但是還沒有配置。其實要配置的也很簡單,告訴插件你的roo工具安裝到哪里就行了。點擊 window ? preferences ? spring ? roo support ,打開設置,然后照著官方文檔截圖設置好你的工具路徑即可。
這樣插件就設置完畢了。其實這個插件也沒啥作用,就是在eclipse中開了一個窗口,能運行roo命令,和直接在命令提示符中運行其實是一樣的。
使用roo
運行roo腳本
$roo_home\samples文件夾下有三個示例項目腳本,使用roo運行它們可以快速創建相應的項目。如果沒有耐心,可以直接從這里開始。
比如說,我要運行clinic實例項目,就可以輸入以下命令:
1
|
roo> script --file clinic.roo |
稍等片刻,程序就會創建完畢。
最后創建出的是一個基于maven的spring boot程序。在idea下是這么一個樣子。可以看到項目中有一個名字叫log.roo的日志文件,它記錄了這個roo腳本執行的內容。另外不知道為什么程序在idea下會有一點報錯,不過不影響編譯和運行。
這個petclinic示例程序使用了spring security來保護頁面。我查閱了一下,spring boot下spring security默認的用戶名是user,密碼則在程序啟動的時候隨機輸出到控制臺中。最后運行截圖如下,大家可以自己運行和測試一下這個程序。
腳本解釋
下面來解釋一下petclinic這個程序的roo腳本,讓我們來看看roo是如何工作的。這里只做一下簡單解釋,如果需要詳細資料的話可以參考官方文檔的附錄,完整介紹了roo的各種命令和參數以及用法。
首先是創建項目并指定頂級包名,這樣會創建一個基于maven的spring boot項目。
1
|
project setup --toplevelpackage org.springframework.roo.petclinic |
然后是指定jpa存儲類型,這里用的是hibernate,數據庫是存儲在內存的hsqldb。當然也可以使用其它數據庫,不過相應地需要增加用戶名等其他參數。
1
|
jpa setup --provider hibernate --database hypersonic_in_memory |
然后是幾個枚舉類,將會在實體類中用到,這里的~指代前面設置的頂級包名。
1
2
3
4
5
6
7
8
9
|
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 |
然后是項目中的幾個實體類。
1
2
3
4
5
|
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命令指定要設置的實體類。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
focus -- class ~.domain.pet field boolean --fieldname sendreminders --notnull --primitive field string --fieldname name --notnull --sizemin 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 --sizemin 3 --sizemax 30 field string --fieldname lastname --notnull --sizemin 3 --sizemax 30 field string --fieldname address --notnull --sizemax 50 --sizemin 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 --sizemin 6 field date --fieldname birthday --type java.util.date --notnull |
然后設置實體類之間的投影關系并設置jpa repository。
1
2
3
4
5
|
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的相關內容。
1
2
3
4
|
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視圖。
1
2
3
4
|
web mvc setup web mvc view setup --type thymeleaf web mvc controller --all --responsetype json web mvc controller --all --responsetype thymeleaf |
然后是生成查詢和詳情頁面。這里針對前面設置的所有查詢條件生成相應的查詢頁面,然后生成指定實體類的詳情頁面。最后指定了頁面語言,目前好像只支持英語和西班牙語。
1
2
3
4
5
6
7
8
|
// 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,密碼是打印在控制臺的隨機字符串。第二行配置了一下用戶權限,只有管理員角色的用戶才能執行刪除操作。
1
2
3
|
security setup --provider default security authorize -- class ~.service.impl.ownerserviceimpl --method delete.* --roles admin |
然后啟用了審計功能,程序會自動記錄相應實體類的編輯時間和編輯者。
1
2
3
4
5
|
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下查看。
1
2
|
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 |
最后自動為這些實體類和服務生成單元測試和集成測試。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// 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安裝使用簡介的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/u011054333/article/details/75949728