Groovy动态执行java

该示例展示了如何在Spring应用中使用SpringContextUtils进行动态注入,并通过GroovyClassLoader运行时编译及注入Java代码,实现动态任务处理。核心功能包括Groovy脚本的创建、实例化以及@Autowired注解的处理。
摘要由CSDN通过智能技术生成

处理spring动态注入:

package com.xxl.job.executor.mvc.controller;

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);
        }
    }

示例参考:

package com.xxl.job.executor.mvc.controller;

import com.xxl.job.core.handler.IJobHandler;
import groovy.lang.GroovyClassLoader;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class IndexController {

    @RequestMapping("/")
    @ResponseBody
    public String index() {
        return "xxl job executor running.";
    }


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

        String codeSource = "package com.xxl.job.service.handler; " +
                " " +
                "import com.xxl.job.core.context.XxlJobHelper; " +
                "import com.xxl.job.core.handler.IJobHandler; " +
                "import com.xxl.job.executor.mvc.controller.IndexController; " +
                "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); " +
                " " +
                "    @Autowired " +
                "    private IndexController c2; " +
                " " +
                "    @Override " +
                "    public void execute() throws Exception{ " +
                "        logger.info(\"------------------actJobHandler2 job start----------------\"); " +
                "        logger.info(\"测试glue模式 XxlJobHelper2.log\"+c2.index()); " +
                "        logger.info(\"------------------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();
                } 
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值