7

私たちは、groovy スクリプトが分野横断的な懸念のためのスペースを確保するためのインクルード メカニズムを探しています。

私の例では、Groovy スクリプトとしての Web サービス エンドポイントがあり、Web サービス プロトコルにログを記録したいと考えています。そのために、暗黙的なオブジェクト (フレームワークから取得) を使用してログ ステートメントを作成します。

しかし、これをすべての Web サービス エンドポイントでコーディングする場合、これはボイラープレート コードです。

他の Groovy スクリプトを含む php の include() のようなものを探していますが、これを行う方法はありますか?

4

7 に答える 7

10

Groovy はそのファイルをオブジェクトとして扱います (自動ラッピングと考えてください)。また、Java クラスパス内のすべての .groovy ファイルをクラスとして使用できるようにします。したがって、ファイル util.groovy がある場合、その中に次のようなものが含まれています。

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

別のファイルから呼び出すには、次のように記述します。

println util.AuxMethod()

それでおしまい。ここでも、util.groovy ファイルがクラスパスにあることを確認してください。

于 2010-12-02T14:55:59.013 に答える
2

現在のスクリプトからスクリプトを呼び出しu.groovy、元の引数を u.groovy に渡すには、以下を実行します。

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

明らかに、必要な String 引数を送信することもできます。

run(new File('u.groovy'),
        ['one', new File('two.text').absolutePath] as String[])
于 2011-10-20T16:24:02.340 に答える
1

evaluate(File) 関数を見てください。

 Object evaluate(File file) 

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

于 2009-08-12T07:30:25.370 に答える
1

すでに「分野横断的な懸念事項」について言及されているので、Web サービスの呼び出しをAOPスタイル (インクルード メカニズムではなく) でインターセプトする必要があると言えます。

Grails はSpring フレームワークと完全に統合されているため、これは Spring AOP 機能を活用するための優れたオプションになります。grails の公式ガイド ( http://grails.org/doc/latest/guide/14.%20Grails%20and%20Spring.html ) のこの章を見て 、単語 AOP を検索してください。

AOP を行うための純粋にグルーヴィーな方法があるかもしれませんが、私は grails と spring を使用します。

于 2009-08-12T17:39:33.423 に答える
0

このメールリストは役に立ちました。 http://groovy.329449.n5.nabble.com/Groovy-scripts-Reusing-declared-methods-in-other-scripts-How-Include-td5703723.html

于 2013-08-28T00:10:13.370 に答える
0

スクリプト用のプリプロセッサを作成しました。特定のincludeパターンを検索します。以下に例を示します。

public final class IncludePreprocessor {

    @FunctionalInterface
    public interface IncludeLoader {

        InputStream load(String include) throws IOException;

    }

    private static final Pattern INCLUDE_PATTERN = Pattern.compile("include\\s+(.+)$");

    private final IncludeLoader includeLoader;

    public IncludePreprocessor(IncludeLoader includeLoader) {
        this.includeLoader = includeLoader;
    }

    public boolean preprocess(InputStream mainScript, Writer outputScript) throws IOException {
        boolean preprocessed = false;
        try (Scanner sc = new Scanner(mainScript)) {
            while (sc.hasNextLine()) {
                String line = sc.nextLine();

                Matcher m = INCLUDE_PATTERN.matcher(line);
                if (m.matches()) {
                    outputScript.append("//").append(line).append(System.lineSeparator());

                    String include = m.group(1);
                    try (InputStream in = includeLoader.load(include)) {
                        StringWriter sw = new StringWriter();
                        preprocess(in, sw);
                        outputScript.append(sw.toString()).append(System.lineSeparator());
                        preprocessed = true;
                    }
                    outputScript.append("//").append(line).append(" [EOF]").append(System.lineSeparator());
                } else {
                    outputScript.append(line).append(System.lineSeparator());
                }
            }
        }

        return preprocessed;
    }
}

そしてそれを使用する方法:

//common.groovy
def sum(a,b) {
   a + b
}

// main.groovy
include common.groovy
sum(1,2)


// Demo.java
public class Demo {
    public static void main(String[] args) {
        IncludePreprocessor ip = new IncludePreprocessor(include -> new FileInputStream("./" + include));
        
        StringWriter sw = new StringWriter();
        ip.preprocess(new FileInputStream("./main.groovy", sw));
        System.out.println(sw.toString());
    }
}
于 2020-07-29T19:39:32.843 に答える