若是你想快速开发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的子类,以了解如何应用定制。