pom文件引入groovy包
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.5.5</version>
</dependency>
java接口
package com.yydone.test;
interface IFoo {
Object add(Object x, Object y);
}
groovy实现类,实现IFoo接口
package com.yydone.test
class Foo implements IFoo {
Object add(Object x, Object y) {
return x + y
}
}
java测试类
package com.yydone.test;
import groovy.lang.GroovyClassLoader;
public class GroovyClassLoaderExample {
public static void main(String[] args) {
try {
Class clazz;
try (GroovyClassLoader loader = new GroovyClassLoader()) {
clazz = loader.loadClass("com.yydone.test.Foo");
}
IFoo foo = (IFoo) clazz.newInstance();
System.out.println( foo.add(new Integer(2), new Integer(3)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果
5
成功调用groovy实现