执行Groovy,用Groovy写"Hello World"(4)

执行Groovy,用Groovy写"Hello World".
为了正确执行Groovy代码,我需要安装Groovy提供的工具,这里提供有执行Groovy的命令:
    Command                                               What it does
__________________________________________________________________________________________________________________

groovysh                      Starts the groovysh command-line shell, which is used to execute Groovy code
                              interactively. By entering statements or whole scripts, line by line, into the shell and
                              giving the go command, code is executed “on the fly.”

groovyConsole                 Starts a graphical interface that is used to execute Groovy code interactively; moreover,
                              groovyConsole loads and runs Groovy script files.

groovy                        Starts the interpreter that executes Groovy scripts. Single-line Groovy scripts can be
                              specified as command-line arguments.
_____________________________________________________________________________________________________
让我们先来看一下groovysh,他是用来执行Groovy程序的.在这个shell里面很容易编辑和执行Groovy,可以不用创建脚本文件,
执行groovysh.bat文件可以得到如下命令模式:
Lets get Groovy!
================
Version: 1.0-RC-01-SNAPSHOT JVM: 1.4.2_05-b04
Type 'exit' to terminate the shell
Type 'help' for command help
Type 'go' to execute the statements
groovy>
 
可以这么写Hello Word程序:
groovy> "Hello, World!"
groovy> go
===> Hello, World!
go是groovy能够识别的命令之一,其他的命令可以通过键入"help"去查询.
execute和go命令是一样的.
discard命令是让Groovy去清除刚才键入的脚本程序段.
Binding命令是绑定一个变量的.如果想让这个变量释放掉.那么结束这个groovy shell.再重新运行一个.eg:
groovy> greeting = "Hello"
groovy> "${greeting}, World!"
groovy> go
===> Hello, World!
groovy> binding
Available variables in the current binding
greeting = Hello
Inspect命令:
Inspect命令根据最后一行命令打开一个groovy object 浏览器 ,这个浏览器时一个用户Swing接口,让你可以看到通过经由Groovy GDK转换过后的 一个对象的动态java API和这个对象的任何额外的别的属性,
在这个groovy环境下,可以直接调用方法,几乎接近IDE开发环境. 

使用groovy:

groovy命令被用来执行代码和脚本,例如以下代码.用来打印十个数字.每个数字都是前面两个数字的和,

current = 1
next = 1
10.times {
print current + ' '
newCurrent = next
next = next + current
current = newCurrent
}
println ''

加入你想执行这段代码,保存该代码到test.groovy文件,要保持为groovy后缀,groovy脚本段程序也要保持到.groovy文件里.这样做的一个好处就是你在groovy命令行里面可以不用键入后缀名就可以执行.

程序得到结果为:

> groovy test

1 1 2 3 5 8 13 21 34 55

groovy还有一些额外参数.比如groovy –e "println 1+1" 就可以得到2.以后将会对这些有更详细的讲述.

 

到目前为止,我们介绍了直接在groovy里面运行groovy代码的直接模式,另一方面我们还有另外一种被要做预编译模式.groovyc编译.groovy文件,对每个Groovy文件也至少产生一个.class文件,再有JVM执行.两种模式区别就是,前者在内存里面执行,而后者被存储到硬盘里.

第二种模式:> groovyc –d classes test.groovy

我们加了-d参数,这样会产生两个.class文件.假如没有-d,.class文件会被建立,当你运行这个编译的时候,生成的.class文件的名字会被打印到控制台上.是否有多个class文件生成,取决于文件里的脚本代码,然而,我们不必关心这个,因为那是java虚拟机的事情.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中执行Groovy脚本可以使用ScriptEngineManager类。以下是一个简单的示例: 1. 添加依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.5.14</version> </dependency> ``` 2. 编Groovy脚本一个简单的Groovy脚本,例如: ```groovy def greet(name) { return "Hello, $name!" } ``` 3. 执行Groovy脚本 在Spring Boot应用程序中使用ScriptEngineManager类执行Groovy脚本。例如: ```java import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; @Service public class GroovyService { public String executeGroovy(String script, String arg) throws ScriptException { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("groovy"); engine.eval(script); Invocable invocable = (Invocable) engine; return (String) invocable.invokeFunction("greet", arg); } } ``` 在上面的代码中,Groovy脚本作为参数传递给ScriptEngine的eval方法。然后,使用Invocable接口调用脚本中定义的greet方法。最后,返回greet方法的结果。 4. 测试 使用以下代码测试GroovyService类: ```java @SpringBootTest class GroovyServiceTest { @Autowired GroovyService groovyService; @Test void testExecuteGroovy() throws ScriptException { String script = "def greet(name) {\n" + " return \"Hello, $name!\"\n" + "}"; String result = groovyService.executeGroovy(script, "World"); assertEquals("Hello, World!", result); } } ``` 在上面的代码中,我们传递了一个Groovy脚本和一个参数“World”给GroovyService的executeGroovy方法。然后,我们检查执行结果是否正确。 这是一个简单的示例,您可以根据自己的需要修改Groovy脚本和Java代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值