Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use groovy language as dynamic regular expression in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use groovy language as a dynamic regular expression in Java, I believe most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it together.

Because it is necessary to set conditional expressions for conditional branches in workflow design, language scripts that can be executed dynamically are needed as the rule engine of workflow. It is definitely not possible for workflow conditional branches to simply use sql scripts. After experiments, groovy scripts can return variables accepted by java. In the specific workflow implementation, we can not only store the groovy script in the text file, but also store the script directly in the database, and then take it out when we use it, of course, we can put it in the xml file.

The following is an example of java calling a groovy script:

Package org.openjweb.core.groovy.test; import groovy.lang.GroovyClassLoader; import java.io.File; public class InvokeGroovy {public static void main (String [] args) {ClassLoader cl = new InvokeGroovy (). GetClass () .getClassLoader (); GroovyClassLoader groovyCl = new GroovyClassLoader (cl); try {/ / read / / Class groovyClass = groovyCl.parseClass (new File ("D:/project/openjweb/src/java/org/openjweb/groovy/Foo.groovy")) from the file / / if you directly use the Groovy string, you can also get the correct result / / Class groovyClass = groovyCl.parseClass ("package org.openjweb.groovy;\ r\ nimport org.openjweb.core.groovy.test.IFoo;\ r\ n class Foo implements IFoo {public Object run (Object foo) {return 23}}"); Class groovyClass = groovyCl.parseClass ("package org.openjweb.groovy;\ r\ n import org.openjweb.core.groovy.test.IFoo") \ r\ n class Foo implements IFoo {public Object run (Object foo) {return 2 > 1} "); / / this returns true IFoo foo = (IFoo) groovyClass.newInstance (); System.out.println (foo.run (new Integer (2);} catch (Exception e) {e.printStackTrace ();}

Here is the text of Foo.groovy:

Package org.openjweb.groovy; import org.openjweb.core.groovy.test.IFoo; class Foo implements IFoo {public Object run (Object foo) {return 3} 2}}

From the above code, you can directly execute the groovy script of the string, and return the operation result after running. For example, 2-2 > 1 will return true.

Using Groovy or Jruby dynamic language scripts as a rule engine solution will become more and more popular.

That's all of the article "how to use groovy as dynamic regular expressions in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report