GROOVY - 技术教程文章

走进Groovy (一)【代码】【图】

一直很喜欢脚本语言,但是一直在不大的公司工作,用得一直是“高大上”的JAVA语言,在真正的项目中,没什么机会用到脚本语言。这两年,又断断续续的用了2年的Ruby,再回头继续用JAVA,说实话,真感觉JAVA语法的啰嗦——哪怕像C#那样多些语法糖也好。虽然在JVM上也有像JRuby,scala那样的动态语言,不知道怎么的,就是不想去用,直到最近心血来潮看了一下Groovy,突然被吸引住了,原因,一开始估计是被他的全兼容JAVA语法所吸引,再慢...

Groovy

Groovy 详细介绍Groovy 是一种基于 JVM 的敏捷开发语言,它结合了 Python、Ruby 和 Smalltalk 的许多强大的特性。Groovy 主要的特点包括:是一个基于Java 虚拟机的敏捷动态语言。构建在强大的 Java 语言之上 并添加了从 Python、Ruby 和 Smalltalk 等语言中学到的诸多特征。为 Java 开发者提供了现代最流行的编程语言特性,而且学习成本很低(几乎为零)。支持 DSL(Domain Specific Languages 领域定义语言)和其它简洁的语法,让...

用Groovy读取本地文件的代码【代码】【图】

下面这些包默认已经被导入了,不需要使用import再次显式导入:java.io.*java.lang.*java.math.BigDecimaljava.math.BigIntegerjava.net.*java.util.*groovy.lang.*groovy.util.*Groovy的运行时方法调用抉择运行时,Groovy根据参数类型决定具体哪一个方法被执行。而Java恰恰相反,被调用的方法根据参数类型,在编译期间就已经定下来了。In Groovy, the methods which will be invoked are chosen at runtime. This is called runtim...

Groovy入门(2-2)Groovy的eclipse插件安装【图】

1、安装eclipse插件启动eclipse,点击help -> Install New Software...在弹出的窗口中点击:Add...Groovy插件的地址:http://dist.springsource.org/release/GRECLIPSE/e4.4/其他eclipse版本可以到:https://github.com/groovy/groovy-eclipse/wiki下载对应的版本 在弹出的Install窗口中填写如下: Name:可任意填写名称,如:Groovy plugin Location:将上面的地址复制到这里,如:http://dist.springsource.org/...

TestGroovy.groovy【代码】

spackage com.iteye.lindows.mysql/** * @author Lindows * */ class TestGroovy { static main(args) { def var = "HelloWorld" println "${var},${var.class}" println "================" var = true println "${var}" } }end原文:http://lindows.blog.51cto.com/426871/1941002

TestRunnerInsertMysqlMore.groovy【代码】

s C:\Users\Lindows\workspace\GroovyTest\src\com\iteye\lindows\mysql\TestRunnerInsertMysqlMore.groovy 1package com.iteye.lindows.mysql2 3import junit.framework.Assert4import net.grinder.script.GTest5import net.grinder.scriptengine.groovy.junit.GrinderRunner6import net.grinder.scriptengine.groovy.junit.annotation.AfterThread7import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess8impor...

atitit groovy 总结java 提升效率

atitit groovy 总结java 提升效率 #---环境配置 1#------安装麻烦的 2三、创建groovy项目 23、 加入? Groovy 类 24、 编译执行groovy类 3Groovy断点调试不生效的解决 4Groovy实现执行原理 51. Groovy脚本的执行.. 52. Groovy类的执行.. 5Groovy语法长处: 63. 动态类型def 64. 循环变量不须要def 65. String 和Gstring 66. 范围 67. 默认參数值 78. 集合简化操作 79. 闭包(Closure) 810. Groovy类map构造函数 911. (5) 不须...

Groovy中那些神奇注解之InheritConstructors【代码】【图】

上一篇:Groovy中那些神奇注解之ToString写完ToString,本来想今天就写到这了,突然觉得InheritConstructors注解实在也是个神器,写起来也没多少字,还是写了吧。InheritConstructors,从名字看就知道是继承构造函数(这话貌似我每篇都说,不得不说人家名字取得好),不知道大家有没有这样的经历,继承JAVA的Exception的时候,为了能和Exception原本的构造函数对应起来,会写上很多个构造函数,而函数的实现,往往就是super(...),...

Groovy split竖杆注意【代码】【图】

前几天将09年写的一个Asp程序使用Grails改造重写,在处理手机号码Split的时候,Asp代码:dim phoneArr phoneArr = split(phones,"|")照搬原来的分割处理,改成Groovy代码如下:String[] phoneArr = phones.split("|")但是返回的记过竟然是:[, 1, 3, 5, 5, 3, 0, 0, 9, 9, 9, 0, |, 1, 3, 5, 0, 0, 4, 3, 2, 3, 1, 1]最后才发现Groovy split是带正则表达式的,而|符号是正则表达式的特殊字符,最后改成如下即可:String[] phoneArr...

groovy脚本导致的FullGC问题【代码】

这个是由一个线上问题导致的:背景: 应用中内嵌了groovy引擎,会动态执行传入的表达式并返回执行结果 线上问题:发现机器的fullGC从某个时候开始暴涨,并且一直持续;登到机器上,用jstat -gcutil 命令观察,发现perm区一直是100%,fullGC无法回收;将这台机器的内存dump出来进行分析;在类视图中,发现大量的groovy.lang.GroovyClassLoader$InnerLoader;在类加载器视图里面也看到大量的groovy的InnerLoader;基本上可以定位问题在...

用groovy采集网页数据【图】

首先,用?http://groovyconsole.appspot.com/?测试下面的代码,发现引用总是失败.下载了GGTS:?https://spring.io/tools/ggts测试成功:?@Grapes( @Grab(group=‘org.ccil.cowan.tagsoup‘, module=‘tagsoup‘, version=‘1.2‘) )import org.ccil.cowan.tagsoup.Parser;class TestHtml {public static void main(String[] args){ String ENCODING = "UTF-8" def PARSER = new XmlSlurper(new Parser() )def url = "http://www.nf...

一个简单的groovy script生成的class文件及其反编译【图】

Groovydef task = {println "Hello from source code-Task"} task()def task2 = {println "Hello from source code-Task2"}class B {String getName(){return "Young"} }println new B().getName();task2(); 结果Hello from source code-Task Young Hello from source code-Task2 Groovy默认是不为这个script写class文件的,但是我们可以通过配置文件或者直接修改原码, conf.setTargetDirectory指定class的存放路径这里生成四个clas...

在Mac上使用GVM Tool安装Groovy、Grails环境【代码】

一、下载gvmtoolcurl -s get.gvmtool.net | bash命令运行后提示最后几句如下:Please open a new terminal, or run the following in the existing one:source "/Users/zz/.gvm/bin/gvm-init.sh"Then issue the following command: gvm helpEnjoy!!!二、初始化gvmtool运行环境source "/Users/zz/.gvm/bin/gvm-init.sh"三、安装groovygvm install groovy四、安装grailsgvm install grails安装后的groovy、grails文件位置为: ...

Groovy--使用模板引擎和GroovyShell执行插值字符串【代码】

package curveJudgeimport groovy.text.SimpleTemplateEngine/*** Created by Jxy on 2019/8/26 17:16**/class EngineTest {staticvoid main(String[] args) {def shell = new GroovyShell()def engine = new SimpleTemplateEngine()def map = [‘eg1‘ : 1,‘eg2‘ : 1,‘eg3‘ : 1,‘eg5‘ : 1,‘eg6‘ : 1,‘eg7‘ : 1,‘eg8‘ : 1,‘eg9‘ : 1,‘eg10‘: 1,‘eg11‘: 1,‘eg12‘: 1,‘eg14‘: 1,‘eg13‘: 1,‘eg15‘: 1,‘eg1...

Groovy中closure的delegate

hello.groovyimport groovy.lang.ClosureClosure clos = { println "Hello closure"} println "delegate:" + clos.delegate.class.name; println "owner: " + clos.owner.class.name;clos.call();println "==============================="class C {Closure clos = {println this.class.nameprintln "delegate:" + delegate.class.name;println "owner: " + owner.class.name; def nestClos = {println "in nest delegate:" + del...

[Groovy] 学习Groovy的好网站(内容全面)【图】

https://www.tutorialspoint.com/groovy/index.htm 原文:http://www.cnblogs.com/MasterMonkInTemple/p/7305926.html

「Apache Groovy」- 编写 retry 函数 @20210426【代码】

问题描述在 Jenkins Pipeline 中,通过 retry 函数,能够对某个操作重复进行,直到成功。尤其是在网络请求中,我们更应该使用 retry 函数,以防止服务器负载过高而产生的临时失败。但是,Jenkins Pipeline 的执行速度“较慢”(这是 Jenkins 的优化,防治对服务器产生过大压力),并且会大量产生 Pipeline Step 执行日志,因此我们希望通过 Groovy 实现 retry 函数,并加入一些特性。该笔记将记录:在 Apache Groovy 中,如何实现自...

groovy-搭建环境【代码】【图】

1. 官网http://www.groovy-lang.org/2. 简介是一个基于Java虚拟机的敏捷动态语言。构建在强大的Java语言之上 并添加了从Python,Ruby和Smalltalk等语言中学到的诸多特征。为Java开发者提供了现代最流行的编程语言特性,而且学习成本很低(几乎为零)。支持DSL(Domain Specific Languages领域定义语言)和其它简洁的语法,让你的代码变得易于阅读和维护。Goovy拥有处理原生类型,面向对象以及一个Ant DSL,使得创建Shell Scripts变...

通过 Groovy / Java 自动运行 Sikuli 测试【代码】

import org.sikuli.script.*;public class TestSikuli { public static void main(String[] args) { Screen s = new Screen(); try{ synchronized(s){ //s.click("imgs/spotlight.png", 0); //s.wait("imgs/spotlight-input.png"); //s.type(null, "hello world\n", 0); ...

Groovy在不同JDK版本下的性能差异【代码】【图】

Groovy作为一种动态语言,性能和JAVA比肯定是差不少,根据网友的测试,由于测试环境,场景和编译参数的不同,大概有差2到7倍的差距那么同样的Groovy,在不同的JDK版本下,会有着怎样的差异呢?好奇试了一下 测试代码写得非常简单,如下,为了测试一些动态语言中常用的特性,特别用上了闭包和动态特性,这样才有意义么class GroovyWithJdk{static void run(){1000.times { "fib"(20) } //为了动态特性}static def fib(n){if(n<2)re...

通过bean parser实现spring scan groovy文件装载bean

Spring使用groovy作为bean,官方用了lang标签,但都是一个个文件。Groovy本身编译成class文件后当然可以和Java完全一样可以被component-scan。但是我想实现能够扫描groovy文件,并且groovy文件发生修改时候能够重新load(方便开发环境中提高效率),网上查查了,然后自己摸索了下,简单实现了。思路:1. 通过NamespaceHandlerSupport自己写一个parser,parser和已有的component-scan的区别就是修改了ClassPathBeanDefinitionScanne...

groovy学习(四)io【代码】

package ch5numbers = [11, 12, 13, 14]def staffTel = [‘Ken‘ : 2745, ‘John‘ : 2746, ‘Jessie‘ : 2772]println("Numbers:${numbers}") //双引号中的内容会被解释println(‘Staff telephone:${staffTel}‘) //单引号中的内容不会被解释printf(‘My name is Ken‘, [])printf(‘My name is Ken\n‘, [])def a = 10def b = 15printf("The sum of %d and %d is %d \n", [a, b, a + b])def x = 1.234def y = 56.78printf(‘%f f...

Groory学习(一)之 Eclipse集成Groovy插件【图】

。Groovy插件的地址:http://dist.springsource.org/release/GRECLIPSE/e4.3/Next>>Finish原文:http://my.oschina.net/u/994490/blog/487693

java环境中基于jvm的两大语言:scala,groovy

java环境中基于jvm的两大语言:scala,groovy可以在java项目里混编这两种语言;scala:静态语言,多范式语言,糅合了面向对象、面向过程;可以与java和net互操作;融汇了众多的语言特性,包括类似net的闭包、lambda表达式;正是语言特性较多,语言复杂度也较高~~groovy:动态语言,既可作为面向对象语言又可做脚本语言;拥有动态语言的好处;语法简单~~scala、groovy都是作为java的替代语言,基于jdk1.6可以使用起来像jdk1.8的高级语...

[Training Video - 3] [Groovy in Detail] Non-static and Static variables in groovy【代码】

log.info "starting" // we use class to create objects of a class Planet p1 = new Planet() Planet p2 = new Planet() Planet p3 = new Planet()//Planet.name = "Pluto" illegalPlanet.shape = "Circle" p1.name = "earth" //p1.shape = "circle"p2.name = "jupiter" //p2.shape = "circle"p3.name = "mars" //p3.shape = "circle"log.info p1.name+" "+p1.shape log.info p2.name+" "+p2.shape log.info p3.name+" "+p3...

nGrinder TestRunnerBarrier.groovy / jihedian【代码】

s import net.grinder.script.Barrier import net.grinder.script.GTest import net.grinder.scriptengine.groovy.junit.GrinderRunner import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread import net.grinder.scriptengine.groovy.junit.annotation.Repeat import org.junit.Before import org.junit.Test import org.junit.runner.R...

Java中使用Groovy实现自定义表达式解析【代码】

Groovy作为一种JVM-Based语言,目前普及程度正在提高。本文演示一下在Java类中,通过继承GDK的groovy.lang.Script类如何支持自定义表达式解析功能。 输入: 表示一行数据的某个map结构。在实际应用中,产生这种结构的最常见场景可能是通过JDBC访问数据库、通过调用WebService服务得到的某行结果集等。目标设定: 假设我们希望对输入数据进行某个运算。此处示例中,我们模拟oracle中最常用的nvl函数。 处理过程: 首先,通过继...

JMeter中Groovy和BeanShell脚本的性能比较【图】

比较完常见后置处理器的性能之后,又顺便比较了下Groovy和BeanShell2者都是基于JVM的脚本语言,2者都能直接用Java的语法和类库这些国外网站都推荐用Groovy:http://jmeter.apache.org/usermanual/best-practices.htmlhttp://www.ubik-ingenierie.com/blog/magento-performance-toolkit-and-jmeter-best-practices/https://blazemeter.com/blog/beanshell-vs-jsr223-vs-java-jmeter-scripting-its-performance因为JMeter支持的一堆脚...

Jenkins groovy 修改build 结果为success或者failed【代码】【图】

有时候遇到jenkins 的build result并不是真正想要的状态,比如check是否有代码更新,如果没有代码更新,build result为failed,但是其实并没有编译,failed状态会造成误解。想要把failed强制改成success。可以使用Groovy强制修改状态。使用Groovy postbuild插件 import hudson.model.Result String desc = "Build Skipped" manager.build.setDescription(desc) manager.build.@result = hudson.model.Result.SUCCESS jenkins自带...

[转载] 详述三种现代JVM语言--Groovy,Scala和Clojure

转载自http://www.tuicool.com/articles/jYzuAv和http://www.importnew.com/1537.html在我与Martin Fowler曾经合作呈现的一次主题演讲中,他作出了一个有洞察性的观点:Java的遗产将是平台,而不是程序设计语言。Java技术的原始工程师们作出了一个明智的决定,就是将编程语言与运行时环境分开,最终这使得超过200种语言能够运行在Java平台上。这种架构对于该平台的长期活力是至关重要的,因为计算机程序设计语言的寿命一般都是比较...