若是你想快速開發Spring應用程序,可使用Spring Boot CLI命令行工具,它容許你運行Groovy腳本,這意味着你有一個相似的java類語法,沒有那麼多樣板代碼,你還能夠引導新項目或爲其編寫本身的命令。java
可使用!SDKMAN手動安裝Spring Boot CLI(命令行接口)或若是你是OSX用戶,可使用Homebrew或MacPorts。有關全面的安裝說明,請參閱「開始」部分中的第10.2節,「安裝Spring Boot CLI」。git
一旦安裝了CLI,你能夠經過輸入spring
並在命令行上按Enter來運行它,若是你在沒有參數的狀況下運行spring
,將顯示一個簡單的幫助屏幕,以下所示:github
$ spring usage: spring [--help] [--version] <command> [<args>] Available commands are: run [options] <files> [--] [args] Run a spring groovy script ... more command help is shown here
你能夠輸入spring help
來得到任何受支持命令的更多細節,以下面的示例所示:web
$ spring help run spring run - Run a spring groovy script usage: spring run [options] <files> [--] [args] Option Description ------ ----------- --autoconfigure [Boolean] Add autoconfigure compiler transformations (default: true) --classpath, -cp Additional classpath entries -e, --edit Open the file with the default system editor --no-guess-dependencies Do not attempt to guess dependencies --no-guess-imports Do not attempt to guess imports -q, --quiet Quiet logging -v, --verbose Verbose logging of dependency resolution --watch Watch the specified file for changes
version
命令提供了一種快速檢查你正在使用的Spring Boot的哪一個版本的方法,以下所示:spring
$ spring version Spring CLI v2.0.2.RELEASE
你可使用run
命令來編譯和運行Groovy源代碼,Spring Boot CLI是徹底獨立的,所以不須要任何外部Groovy安裝。segmentfault
下面的示例顯示了用Groovy編寫的「hello world」web應用程序:緩存
hello.groovy安全
@RestController class WebApplication { @RequestMapping("/") String home() { "Hello World!" } }
要編譯和運行應用程序,輸入如下命令:app
$ spring run hello.groovy
將命令行參數傳遞給應用程序,使用--
將命令與「spring」命令參數分離,以下例所示:spring-boot
$ spring run hello.groovy -- --server.port=9000
要設置JVM命令行參數,可使用JAVA_OPTS
環境變量,以下例所示:
$ JAVA_OPTS=-Xmx1024m spring run hello.groovy
在Microsoft Windows上設置JAVA_OPTS
時,請確保引用整個指令,例如set "JAVA_OPTS=-Xms256m -Xmx2048m"
,這樣作能夠確保將值正確地傳遞給進程。
標準Groovy包含一個@Grab
註解,它容許你聲明對第三方庫的依賴關係,這個有用的技術讓Groovy能夠像Maven或Gradle那樣下載jar,但不須要你使用構建工具。
Spring Boot進一步擴展了這種技術,並嘗試根據代碼推斷要「抓取」哪些庫,例如,因爲前面顯示的WebApplication
代碼使用了@RestController
註解,因此Spring Boot抓取了「Tomcat」和「Spring MVC」。
如下項目被用做「抓取提示」:
JdbcTemplate
,NamedParameterJdbcTemplate
,DataSource
@EnableJms
@EnableCaching
@Test
@EnableRabbit
@EnableReactor
extends Specification
@EnableBatchProcessing
@MessageEndpoint
@EnableIntegration
@Controller
@RestController
@EnableWebMvc
@EnableWebSecurity
@EnableTransactionManagement
請參閱Spring Boot CLI源代碼中的 CompilerAutoConfiguration的子類,以瞭解如何應用定製。