Does groovy provide an include mechanism?

2019-04-27 00:14发布

问题:

We are searching for an include mechanism for groovy scripts to have space for cross-cutting-concerns.

In my example we have, web service endpoints as groovy scripts and want to log to our web service protocol. for that we use our implicit object (getting from our framework) to create the logging statement.

But this is boilerplate code if we code this in every web service endpoint.

We are searching for something like include() in php, that includes other groovy scripts, are there any ideas how to do this?

回答1:

Since you already mentioned “cross-cutting-concerns” I’d say that you need to intercept your webservice calls AOP style (not an include mechanism).

Grails is completely integrated with Spring framework, so this makes for a good option for exploiting Spring AOP features. Take a look at this chapter from grails official guide: http://grails.org/doc/latest/guide/14.%20Grails%20and%20Spring.html and search for word AOP.

Maybe there is a purely groovy way of doing AOP, but I'd go with grails and spring.



回答2:

Groovy treats its files as objects (think of it as of automatic wrapping). And it makes all .groovy files within the java classpath available as classes. So if you have the file util.groovy, that contains something like this inside:

def static AuxMethod() {
    return "Hello World"
}

To call it from another file you just write:

println util.AuxMethod()

That's it. Again, just make sure that your util.groovy file is in the classpath.



回答3:

To invoke script u.groovy from the current script, passing along the original arguments to the u.groovy, run

run(new File('u.groovy'), args)

Obviously, you could also send any String arguments that you want to:

run(new File('u.groovy'),
        ['one', new File('two.text').absolutePath] as String[])


回答4:

Look at the evaluate(File) function:

 Object evaluate(File file) 

http://groovy.codehaus.org/api/groovy/lang/Script.html



回答5:

I did some research on this for a Domain Specific Language I was creating. There are three possibilities:

  1. Create your classes as inheriting a parent groovy class. Put your shared code in the base class.

  2. Use the ScriptBaseClass see http://groovy.codehaus.org/Embedding+Groovy . This is a class upon which all of your scripts will be created.

  3. Use the import static methods capability. Note that you can do this inside the java container (see http://mrhaki.blogspot.com/2011/06/groovy-goodness-add-imports.html ).

All of these work great. My preference is the ScriptBaseClass. This works best if the common code is Groovy (the ScriptBaseClass must be a groovy class. It can not be a java class.)

Of course, with all of these items, you will still need to actually call the common method in your groovy code. For example:

doCommonStuff();
.
. do the rest of it here
.

That's not too awful, I don't think. Certainly about the same as adding some sort of #include pre-processor statement.

And finally, all of this assumes that you have access to the java program which is calling your Groovy code. If that's not the case, you can still use the static imports. It's just one extra line of code.

import static com.mycompany.mycode.doCommonStuff
doCommonStuf()
.
. do the rest of it here
.


回答6:

I found this mail list helpful. http://groovy.329449.n5.nabble.com/Groovy-scripts-Reusing-declared-methods-in-other-scripts-How-Include-td5703723.html