好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringBoot项目中使用Groovy脚本的示例代码

最近项目中遇到了这样的需求:需要检查一个表的某些字段,是否为空,或者是否符合预期规则:比如大于0,或者在某个范围内。考虑将表名和字段名配置在数据库中,然后规则使用 Groovy 来写,比较灵活。

1. 引入依赖

?

1

2

3

4

5

6

7

8

9

10

< dependency >

      < groupId >org.codehaus.groovy</ groupId >

      < artifactId >groovy</ artifactId >

      < version >2.5.5</ version >

  </ dependency >

  < dependency >

      < groupId >org.codehaus.groovy</ groupId >

      < artifactId >groovy-jsr223</ artifactId >

      < version >2.5.5</ version >

  </ dependency >

2. 使用 脚本 引擎运行groovy脚本

?

1

2

3

4

5

6

7

8

9

ScriptEngineManager factory = new ScriptEngineManager();

ScriptEngine engine = factory.getEngineByName( "groovy" );

// 变量

Bindings bindings = engine.createBindings();

bindings.put( "value" , 10 );

// groovy脚本

String scriptStr = "if (value == null) { return false}\n if (value <= 0) {return false} else { return true}" ;

// 执行脚本

Object res = engine.eval(scriptStr, bindings);

在实际项目中,可以把ScriptEngine engine在项目初始化的时候生成,不需要每次调用都再去生成一个。

通过debug知道,在ScriptEngine内部有个map会缓存曾经执行过的脚本,所以性能方面也不需要担心。

Groovy语法参考

3.思考

使用这种方式的好处是什么?

方便:有新的表需要检查字段的时候,加配置就可以,减少工作量;简单的规则可以直接通过Groovy脚本实现,规则改变时可以很快的修改。 成本低:Groovy语法和Java很像,不需要付出太大的学习成本。

为什么不使用规则引擎?

这不是个复杂的需求,现阶段没必要引入规则引擎也可以很好的完成需求。引入规则引擎会增加更多成本,但效果是差不多的。

SpringBoot +Groovy运行动态脚本

GroovyClassLoader方式

Java使用GroovyClassLoader动态加载Groovy脚本,调用脚本中的方法

在java中调用test.groovy脚本文件

?

1

2

3

4

5

6

7

ClassLoader parent = this .getClass().getClassLoader();

GroovyClassLoader loader = new GroovyClassLoader(parent);

Class groovyClass = loader.parseClass(

         new File( "src/main/java/com/example/demo/groovyscript/test.groovy" )

);

GroovyObject groovyObject= (GroovyObject)groovyClass.newInstance();

String result = (String) groovyObject.invokeMethod( "test" , id);

Groovy

test.groovy脚本文件

?

1

2

3

4

5

6

7

package com.example.demo.groovyscript

 

class MyTest{

     static String test(String id){

         return "test1 id:" + id;

     }

}

GroovyScriptEngine方式

java使用GroovyScriptEngine脚本引擎加载Groovy脚本

方法调用

在java中调用test.groovy脚本文件

?

1

2

3

GroovyScriptEngine engine = new GroovyScriptEngine( "src/main/java/com/example/demo/groovyscript" );

Script script = engine.createScript( "test.groovy" , new Binding());

return (String) script.invokeMethod( "test" ,id);

Groovy

test.groovy脚本文件

package com.example.demo.groovyscript

?

1

2

3

def test(id){

     return "test2 id:" + id;

}

变量绑定

在java中调用test.groovy脚本文件

?

1

2

3

4

5

GroovyScriptEngine engine = new GroovyScriptEngine( "src/main/java/com/example/demo/groovyscript" );

Binding binding = new Binding();

binding.setVariable( "id" ,id);

engine.run( "test.groovy" ,binding);

return binding.getVariable( "output" ).toString();

Groovy

test.groovy脚本文件

?

1

2

3

package com.example.demo.groovyscript

 

output = "test3 id: ${id}"

到此这篇关于SpringBoot项目中使用Groovy脚本的示例代码的文章就介绍到这了,更多相关SpringBoot使用Groovy脚本内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://www.jianshu.com/p/4995e2535ceb

查看更多关于SpringBoot项目中使用Groovy脚本的示例代码的详细内容...

  阅读:18次