這篇文章主要講解了“springboot怎么集成groovy腳本使用”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“springboot怎么集成groovy腳本使用”吧!

我們提供的服務(wù)有:成都網(wǎng)站制作、網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、東源ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的東源網(wǎng)站制作公司
在我們的應(yīng)用中引入腳本能力,可以很好的提升靈活性,我們的核心開發(fā)工作可以集中在核心平臺(tái)能力的開發(fā)上,具體場景的功能可以通過腳本來實(shí)現(xiàn),例如jenkins就可以通過groovy腳本來編寫pipeline,可以很靈活的定制構(gòu)建過程。 spring本身提供了groovy集成的機(jī)制,分為兩種方式,一種是用groovy開發(fā)程序,跟用java開發(fā)類似,需要經(jīng)過編譯。一種是將groovy作為腳本來執(zhí)行,不需要編譯。在此我們介紹的是第二種方式,將groovy作為腳本來使用。
在spring中集成groovy腳本,主要有2種思路,一種是在groovy腳本中定義bean,這樣groovy腳本就融入了整個(gè)spring的體系,跟使用普通的bean沒有區(qū)別。一種是在程序中調(diào)用groovy腳本,讓groovy腳本成為一個(gè)可執(zhí)行的部件。下面我們分別介紹這2種方式。 在spring中聲明groovy腳本中定義的bean有兩種方式,一種是傳統(tǒng)的xml,一種是spring-framework-4中引入的groovy聲明方式。
首先我們定義一個(gè)interface:
public interface MyService {
String fun(MyDomain myDomain);
}這兒提供了一種思路,我們可以用java代碼編寫默認(rèn)的interface實(shí)現(xiàn),如果默認(rèn)實(shí)現(xiàn)不滿足特定場景的要求時(shí),配合策略模式,用groovy腳本實(shí)現(xiàn)特定場景,程序會(huì)變的很靈活,配合腳本的熱加載機(jī)制,當(dāng)處理邏輯需要變化時(shí),在程序運(yùn)行的過程中,我們可以隨時(shí)調(diào)整腳本內(nèi)容且能夠及時(shí)生效。
在groovy腳本MyServiceImpl.groovy中實(shí)現(xiàn)這個(gè)interface:
class MyServiceImpl implements MyService {
@Autowired
FunBean useBean;
String myProp;
String fun(MyDomain myDomain) {
return myDomain.toString() + useBean.getFunName() + myProp;
}
}下面分別介紹通過xml和groovy兩種配置方式來聲明bean。
通過xml配置聲明bean是spring傳統(tǒng)的方法,這種方法近來已經(jīng)被通過java代碼聲明的方式取代,但是對(duì)于聲明groovy腳本中定義的bean來說還是最簡單的方法。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/lang https://www.springframework.org/schema/lang/spring-lang.xsd"> <lang:groovy id="myServiceXml" script-source="classpath:MyServiceImpl.groovy" refresh-check-delay="10000" > <lang:property name="myProp" value=" this is xml init prop" /> </lang:groovy> </beans>
以上xml代碼聲明了myServiceXml這個(gè)bean,script-source指定了這個(gè)bean的來源是classpath:MyServiceImpl.groovy這個(gè)腳本文件。將classpath替換為file,可以指定任一位置的腳本文件。refresh-check-delay定義了腳本的刷新間隔,當(dāng)腳本內(nèi)容發(fā)生變化后,可以自動(dòng)刷新腳本的內(nèi)容。lang:property這個(gè)標(biāo)簽可以對(duì)bean的屬性進(jìn)行初始化賦值。
我們分別用xml和groovy兩種聲明bean的方式給myProp這個(gè)屬性賦值不同的初始值,在后續(xù)的演示代碼中可以看到。
spring-framework-4中引入了groovy聲明bean的方式,我們用groovy來聲明myServiceGroovy這個(gè)bean,相比于xml的方式,groovy的聲明方式可讀性更強(qiáng)一些。
詳細(xì)介紹見spring的官方博文: Groovy Bean Configuration in Spring Framework 4
import org.springframework.scripting.groovy.GroovyScriptFactory
import org.springframework.scripting.support.ScriptFactoryPostProcessor
beans {
scriptFactoryPostProcessor(ScriptFactoryPostProcessor) {
defaultRefreshCheckDelay = 10000
}
myServiceGroovy(GroovyScriptFactory, 'classpath:MyServiceImpl.groovy') {
bean ->
bean.scope = "prototype"
myProp = ' this is Bean Builder init prop'
bean.beanDefinition.setAttribute(ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, 6000)
}
}通過GroovyScriptFactory可以指定定義bean的groovy腳本位置。 通過bean的lambda表達(dá)式,可以對(duì)bean的屬性進(jìn)行賦值,除了我們定義的myProp這個(gè)屬性外,還可以定義scope和腳本刷新時(shí)間。
前面我們通過xml和groovy兩種方式分別聲明了2個(gè)bean: myServiceXml和myServiceGroovy,下面我們?cè)诔绦蛑姓{(diào)用這2個(gè)bean。
@SpringBootApplication
@ImportResource({"classpath:xml-bean-config.xml", "classpath:BeanBuilder.groovy"})
public class Application implements CommandLineRunner {
@Autowired
private MyService myServiceXml;
@Autowired
private MyService myServiceGroovy;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws ScriptException, ResourceException, IllegalAccessException, InstantiationException {
MyDomain myDomain = new MyDomain();
myDomain.setName("test");
System.out.println(myServiceXml.fun(myDomain));
myDomain.setName("test2");
System.out.println(myServiceGroovy.fun(myDomain));
}
}首先我們通過@ImportResource來引入bean的聲明文件,然后就是普通的bean的依賴注入和方法調(diào)用,可以看到在bean的使用上,腳本定義的bean和用程序編寫的bean沒有任何區(qū)別。 在run方法中,我們分別調(diào)用了myServiceXml和myServiceGroovy的這2個(gè)bean的fun方法。 執(zhí)行run方法可以看到輸出到結(jié)果:
MyDomain(name=test)FunBean this is xml init prop MyDomain(name=test2)FunBean this is Bean Builder init prop
除了前面提到的在groovy中實(shí)現(xiàn)bean以外,我們還可以通過groovy提供的GroovyScriptEngine來執(zhí)行g(shù)roovy腳本,這種方式不依賴于springframework,普通的java程序中也可以使用。
@Component
public class MyEngine {
private final GroovyScriptEngine engine;
@Autowired
private FunBean funBean;
public MyEngine() throws IOException {
engine = new GroovyScriptEngine(ResourceUtils.getFile("classpath:scripts/").getAbsolutePath()
, this.getClass().getClassLoader());
}
public void runScript(int x, int y) throws IllegalAccessException,
InstantiationException, ResourceException, ScriptException {
Class<GroovyObject> calcClass = engine.loadScriptByName("CalcScript.groovy");
GroovyObject calc = calcClass.newInstance();
Object result = calc.invokeMethod("calcSum", new Object[]{x, y});
System.out.println("Result of CalcScript.calcSum() method is " + result);
Binding binding = new Binding();
binding.setVariable("arg", "test");
binding.setVariable("funBean", funBean);
Object result1 = engine.run("CalcScript.groovy", binding);
System.out.println("Result of CalcScript.groovy is " + result1);
}
}首先我們初始化GroovyScriptEngine,在構(gòu)造方法中傳入腳本文件的路徑。
執(zhí)行腳本的方法有2種,一種是獲取到GroovyObject,通過invokeMethod來執(zhí)行腳本中的某個(gè)方法,方法的參數(shù)通過Object數(shù)組傳入。
Class<GroovyObject> calcClass = engine.loadScriptByName("CalcScript.groovy");
GroovyObject calc = calcClass.newInstance();
Object result = calc.invokeMethod("calcSum", new Object[]{x, y});第二種是直接運(yùn)行g(shù)roovy腳本,可以通過Binding將變量傳遞到groovy腳本中。
Binding binding = new Binding();
binding.setVariable("arg", "test");
binding.setVariable("funBean", funBean);
Object result1 = engine.run("CalcScript.groovy", binding);感謝各位的閱讀,以上就是“springboot怎么集成groovy腳本使用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)springboot怎么集成groovy腳本使用這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
本文名稱:springboot怎么集成groovy腳本使用
轉(zhuǎn)載源于:http://chinadenli.net/article8/gesjip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、面包屑導(dǎo)航、品牌網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)公司、商城網(wǎng)站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)