所以我有一個現有的 spring boot 應用程式。我想添加一個 Groovy 腳本(比如說“HelloWorld.groovy”)來顯示訊息 hello world。我怎樣才能做到這一點?下面是我想要的樣子:
// some random code here
// ...
// ...
// groovy script : "HelloWorld" to be executed
// some random code ...
uj5u.com熱心網友回復:
有很多不同的方法可以做到這一點,并且問題中沒有足夠的資訊來確定最適合您的解決方案是什么,但一種方法是創建一個GroovyShell并評估腳本那個殼。
import groovy.lang.GroovyShell;
public class GroovyDemo {
public static void main(String[] args) {
System.out.println("This represents some random code");
String groovyScript = "println 'first line of Groovy output'\n"
"println 'second line of Groovy output'";
GroovyShell groovyShell = new GroovyShell();
// instead of passing a String you could pass a
// URI, a File, a Reader, etc... See GroovyShell javadocs
groovyShell.evaluate(groovyScript);
System.out.println("This represents some more random code");
}
}
輸出:
This represents some random code
first line of Groovy output
second line of Groovy output
This represents some more random code
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/436331.html
上一篇:Caching(Caffeine)&Spring-基于方法引數值的兩個快取
下一篇:專案運行時不會創建新日期
