日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Roo 简介

發(fā)布時間:2023/12/10 javascript 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Roo 简介 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一直以來,Java/Spring開發(fā)被認(rèn)為是笨重的代表,無法快速生成項(xiàng)目原型和骨架。所以,Spring推出了Spring Roo這個項(xiàng)目,幫助我們快速生成項(xiàng)目原型。本文參考自Spring Roo的官方文檔,如果熟悉英文的話可以直接看原文檔,內(nèi)容更加豐富。

安裝

安裝命令行工具

Spring Roo是一套命令行工具,如果你使用的是Eclipse/STS,還可以使用Eclipse對應(yīng)的插件。

首先先來下載命令行工具。到下載頁面,選擇對應(yīng)版本下載。這里我選擇的是最新的2.0.0.RC1 ,畢竟我有更新強(qiáng)迫癥。下載完成之后解壓,會得到一個文件夾,其中bin目錄下就是Spring Roo的可執(zhí)行文件了??梢钥吹剿衎at和sh兩種格式,可以在不同系統(tǒng)上運(yùn)行。為了方便以后在終端窗口運(yùn)行,我建議同時將這個文件夾添加到環(huán)境變量中。

安裝好之后,打開命令提示符或者其他終端窗口,輸入roo命令,就可以啟動Roo了。值得提一點(diǎn),Roo會在命令提示符對應(yīng)的文件夾位置創(chuàng)建項(xiàng)目,所以如果需要在特定位置創(chuàng)建項(xiàng)目,先在命令提示符中切換到該文件夾,然后再啟動Roo。

Roo命令行工具

安裝Eclipse插件

打開你的Eclipse/STS,然后遵循以下步驟:

  • 點(diǎn)擊菜單欄 Help ? Install New Software
  • 點(diǎn)擊 Available Software sites
  • 點(diǎn)擊 Import 按鈕
  • 找到 "$ROO_HOME/conf/sts-sites-bookmarks.xml" 并確定,這里$ROO_HOME是你安裝Roo的目錄
  • 根據(jù)需要選擇對應(yīng)的版本
  • 在過濾欄輸入roo
  • 選中功能 Spring IDE Roo Support
  • 然后一路確定并允許條款
  • 最后重啟IDE即可
  • 下面是官方文檔的圖,如果有疑問照著這張圖來就行了。

    官方文檔截圖

    到這一步還沒完,插件是安裝好了,但是還沒有配置。其實(shí)要配置的也很簡單,告訴插件你的Roo工具安裝到哪里就行了。點(diǎn)擊 Window ? Preferences ? Spring ? Roo Support ,打開設(shè)置,然后照著官方文檔截圖設(shè)置好你的工具路徑即可。

    官方文檔設(shè)置截圖

    這樣插件就設(shè)置完畢了。其實(shí)這個插件也沒啥作用,就是在Eclipse中開了一個窗口,能運(yùn)行Roo命令,和直接在命令提示符中運(yùn)行其實(shí)是一樣的。

    使用Roo

    運(yùn)行Roo腳本

    $ROO_HOME\samples文件夾下有三個示例項(xiàng)目腳本,使用Roo運(yùn)行它們可以快速創(chuàng)建相應(yīng)的項(xiàng)目。如果沒有耐心,可以直接從這里開始。

    示例項(xiàng)目腳本

    比如說,我要運(yùn)行clinic實(shí)例項(xiàng)目,就可以輸入以下命令:

    roo> script --file clinic.roo

    稍等片刻,程序就會創(chuàng)建完畢。

    Roo運(yùn)行結(jié)果

    最后創(chuàng)建出的是一個基于Maven的Spring Boot程序。在IDEA下是這么一個樣子??梢钥吹巾?xiàng)目中有一個名字叫l(wèi)og.roo的日志文件,它記錄了這個Roo腳本執(zhí)行的內(nèi)容。另外不知道為什么程序在IDEA下會有一點(diǎn)報錯,不過不影響編譯和運(yùn)行。

    程序代碼截圖

    這個PetClinic示例程序使用了Spring Security來保護(hù)頁面。我查閱了一下,Spring Boot下Spring Security默認(rèn)的用戶名是user,密碼則在程序啟動的時候隨機(jī)輸出到控制臺中。最后運(yùn)行截圖如下,大家可以自己運(yùn)行和測試一下這個程序。

    運(yùn)行截圖

    腳本解釋

    下面來解釋一下PetClinic這個程序的Roo腳本,讓我們來看看Roo是如何工作的。這里只做一下簡單解釋,如果需要詳細(xì)資料的話可以參考官方文檔的附錄,完整介紹了Roo的各種命令和參數(shù)以及用法。

    首先是創(chuàng)建項(xiàng)目并指定頂級包名,這樣會創(chuàng)建一個基于Maven的Spring Boot項(xiàng)目。

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

    然后是指定JPA存儲類型,這里用的是Hibernate,數(shù)據(jù)庫是存儲在內(nèi)存的HSQLDB。當(dāng)然也可以使用其它數(shù)據(jù)庫,不過相應(yīng)地需要增加用戶名等其他參數(shù)。

    jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY

    然后是幾個枚舉類,將會在實(shí)體類中用到,這里的~指代前面設(shè)置的頂級包名。

    enum type --class ~.domain.reference.PetType enum constant --name DOG enum constant --name CAT enum constant --name BIRDenum type --class ~.domain.reference.Specialty enum constant --name CARDIOLOGY enum constant --name DENTISTRY enum constant --name NUTRITION

    然后是項(xiàng)目中的幾個實(shí)體類。

    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})"

    之后的叫本詳細(xì)設(shè)置了每個實(shí)體類的屬性以及對應(yīng)關(guān)系,由于比較多所以我只挑選了幾個典型的。在設(shè)置實(shí)體類之前,需要使用focus命令指定要設(shè)置的實(shí)體類。

    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.Visitfocus --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

    然后設(shè)置實(shí)體類之間的投影關(guān)系并設(shè)置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.VetInforepository jpa --all --package ~.repository service --all --apiPackage ~.service.api --implPackage ~.service.impl

    然后是設(shè)置DTO(數(shù)據(jù)傳輸對象),它和頁面中的表單等信息對應(yīng),然后在后臺轉(zhuǎn)換為相應(yīng)的實(shí)體類。在這里還可以指定finder,也就是查詢條件,查詢條件的規(guī)則請參考Spring Data JPA的相關(guān)內(nèi)容。

    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

    然后是設(shè)置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

    然后是生成查詢和詳情頁面。這里針對前面設(shè)置的所有查詢條件生成相應(yīng)的查詢頁面,然后生成指定實(shí)體類的詳情頁面。最后指定了頁面語言,目前好像只支持英語和西班牙語。

    // 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 THYMELEAFweb mvc language --code es

    然后使用了Spring Security保護(hù)了一下程序。第一行的是使用Spring Security的默認(rèn)配置,用戶名是user,密碼是打印在控制臺的隨機(jī)字符串。第二行配置了一下用戶權(quán)限,只有管理員角色的用戶才能執(zhí)行刪除操作。

    security setup --provider DEFAULTsecurity authorize --class ~.service.impl.OwnerServiceImpl --method delete.* --roles ADMIN

    然后啟用了審計功能,程序會自動記錄相應(yīng)實(shí)體類的編輯時間和編輯者。

    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服務(wù)端點(diǎn)功能,這些端點(diǎn)可以在/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

    最后自動為這些實(shí)體類和服務(wù)生成單元測試和集成測試。

    // 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

    總結(jié)

    以上是生活随笔為你收集整理的Spring Roo 简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。