1、引包

  <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.21</version>
        </dependency>
        <dependency>
            <groupId>com.xuxueli</groupId>
            <artifactId>xxl-job-core</artifactId>
            <version>2.3.1</version>
        </dependency>

 方式一:编辑java类

package com.sleep.demo.groovy;

import com.sleep.demo.service.UserService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class MyGroovy  extends IJobHandler {
    private static Logger logger = LoggerFactory.getLogger(MyGroovy.class);
    @Autowired
    UserService userService;

    public ReturnT<String> execute1(String... strings) throws Exception {
        Integer run = userService.run(1, 2);
        logger.info("ReturnT<String> execute run="+run);
        return new ReturnT("成功:"+run);
    }

    @Override
    public void execute() throws Exception {
        Integer run = userService.run(3, 4);
        logger.info("void execute run="+run);

    }
}

 编辑spring容器工具类:

package com.sleep.demo.groovy;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextUtils implements ApplicationContextAware {
    static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext context)
            throws BeansException {
        SpringContextUtils.context = context;
    }

    public static ApplicationContext getContext() {
        return context;
    }

    public static void autowireBean(Object bean) {
        context.getAutowireCapableBeanFactory().autowireBean(bean);
    }

    public static <T> T getBean(Class<T> clazz) {
        return context.getBean(clazz);
    }
}

 4编写controller测试

package com.sleep.demo.controller;

import com.sleep.demo.groovy.MyGroovy;
import com.sleep.demo.groovy.SpringContextUtils;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import groovy.lang.GroovyClassLoader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/groovy")
@Slf4j
public class GroovyController {
    @RequestMapping("/index1")
    public String index1() {
        try {
            MyGroovy instance = MyGroovy.class.newInstance();//实例化对象
            SpringContextUtils.autowireBean(instance); // 对脚本java中的Autowired成员变量进行注入
            if (instance!=null) {
                if (instance instanceof MyGroovy) {
                    MyGroovy instance1 = (MyGroovy) instance;
                    ReturnT<String> stringReturnT = instance1.execute1();
                    return "ok"+stringReturnT.getContent();
                }
            }

        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "token:nok";
    }

    @RequestMapping("/index2")
    public void index2() throws Exception {
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader();

        String codeSource = "package com.sleep.demo.groovy;" +
                "import com.xxl.job.core.context.XxlJobHelper;" +
                "import com.xxl.job.core.handler.IJobHandler;" +
                "import org.slf4j.Logger;" +
                "import org.slf4j.LoggerFactory;" +
                "import org.springframework.beans.factory.annotation.Autowired;" +
                " " +
                "public class DemoGlueJobHandler2 extends IJobHandler { " +
                "    private static Logger logger = LoggerFactory.getLogger(DemoGlueJobHandler2.class); " +
                "    @Override " +
                "    public void execute() throws Exception{ " +
                "        logger.error(\"------------------actJobHandler2 job start----------------\"); " +
                " System.out.printf(\"777777777\");" +
                "        logger.error(\"------------------actJobHandler2 job end----------------\"); " +
                "    } " +
                " " +
                "} ";
        Class<?> clazz = groovyClassLoader.parseClass(codeSource);
        if (clazz != null) {
            Object instance = clazz.newInstance();
            SpringContextUtils.autowireBean(instance); // 对脚本java中的Autowired成员变量进行注入
            if (instance!=null) {
                if (instance instanceof IJobHandler) {
                    IJobHandler instance1 = (IJobHandler) instance;
                    instance1.execute();
                }
            }
        }
    }
}

 这里运用了 xxljob的 IJobHandler接口 也可以自定义一个接口 就可以少引用一个xxljob的包