Spring boot中使用groovy

groovy 是当做一个脚本来用的,也可以从数据库加载代码做一些动态数据处理。

搭建一个spring boot环境,pom.xml中编辑器和jar包,spring boot是 2.1.6.RELEASE

<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-all</artifactId>
  <version>2.4.7</version>
</dependency>


<plugin>
  <groupId>org.codehaus.gmavenplus</groupId>
  <artifactId>gmavenplus-plugin</artifactId>
  <version>1.13.1</version>
  <executions>
    <execution>
      <goals>
        <goal>addSources</goal>
        <goal>addTestSources</goal>
        <goal>generateStubs</goal>
        <goal>compile</goal>
        <goal>generateTestStubs</goal>
        <goal>compileTests</goal>
        <goal>removeStubs</goal>
        <goal>removeTestStubs</goal>
      </goals>
    </execution>
  </executions>
</plugin>

 
在一个spring boot工程中,新建groovy脚本文件,

def sayHello(name){
    return "Hello,$name!"
}

def sayHi(name){
    return "Hi,$name!"
}

 

 ScriptEngine方式

新建一个正常java类,交给spring 容器托管

package com.groovy;

import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory;
import org.codehaus.groovy.runtime.GStringImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import java.io.InputStreamReader;

@Service
public class HelloService {

    @Autowired
    private ApplicationContext context;

    public String sayHello(String name) throws Exception {
        Resource script = context.getResource("classpath:scripts/hello.groovy");
        GroovyScriptEngineFactory factory = new GroovyScriptEngineFactory();
        ScriptEngine engine = factory.getScriptEngine();
        engine.eval(new InputStreamReader(script.getInputStream()));
        Invocable invocable = (Invocable) engine;


//      GStringImpl obj = (GStringImpl)invocable.invokeFunction("sayHello",name);
        GStringImpl obj = (GStringImpl) invocable.invokeFunction("sayHi", name);

        String ret = obj.toString();
        System.out.println(ret);
        return ret;
    }
}

 

 

写一个测试类

package org.example;

import com.groovy.HelloService;
import com.lw.service.cangbaoe.CangbaoeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest

//@Configuration
@ComponentScan(basePackages = {"com.lw","com.groovy"})
//@ContextConfiguration(locations = {"classpath:application.properties"})
@SpringBootConfiguration
//@SpringJUnitConfig
//@TestPropertySource("classpath:application.properties")


public class SpringTest {

    @Autowired
    private HelloService helloService;

    @Test
    public void test(){
        helloService.sayHello("king");
    }
}

 

GroovyClassLoader 调用方式

public String sayHello2(String name) throws Exception {
    Resource script = context.getResource("classpath:scripts/hello.groovy");

    GroovyClassLoader classLoader = new GroovyClassLoader();
    GroovyCodeSource codeSource = new GroovyCodeSource(script.getURI());
    Class groovyClass = classLoader.parseClass(codeSource);
    GroovyObject groovyObject = (GroovyObject)groovyClass.newInstance();
    GStringImpl obj = (GStringImpl)groovyObject.invokeMethod("sayHi",name);

    String ret = obj.toString();
    System.out.println(ret);
    return ret;
}

 

GroovyShell 调用方式

private static GroovyShell shell = new GroovyShell();
public String sayHello3(String name) throws Exception {
    Resource script = context.getResource("classpath:scripts/hello.groovy");

    Script script1 = shell.parse(script.getURI());
    GStringImpl obj = (GStringImpl)InvokerHelper.invokeMethod(script1,"sayHi",name);

    String ret = obj.toString();
    System.out.println(ret);
    return ret;
}

 

posted @ 2024-04-18 11:59  伟衙内  阅读(195)  评论(0编辑  收藏  举报