通过配置后反射执行指定方法

  public Object executionMethod(String className, String methodName, Object... args){
  	
      try {
          Class<?> clazz =  wac.getBean(className).getClass(); 
          Class[] classes = null;
          if (args != null && args.length != 0){
              List<Object> parameters = Arrays.asList(args);
              classes = new Class[parameters.size()];
              for (int i = 0; i < parameters.size(); i++) {
                  classes[i] = parameters.get(i).getClass();
              }
          }
          Method method = clazz.getMethod(methodName, classes);
          Object reObject=method.invoke(wac.getBean(className), args);
          return reObject;
      } catch (Exception e) {
          e.printStackTrace();
          logger.info("ExecutionTask.executionMethod:出错了...");
          return "error";
      }
  }
测试文件:
/**
 * com.epf.oa.utills
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2019年1月6日 		GZEPF
 *
 * Copyright (c) 2019, gomai.
*/

package com.epf.oa.utills;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import com.epf.oa.entity.business.BaseBusinessEntity;
import com.epf.oa.entity.form.ExcutionEntity;
import com.epf.oa.entity.form.TaskEventEntity;
import com.epf.oa.service.TaskEventService;
import com.github.pagehelper.StringUtil;

/**
 * 
 * <code>ExecutionTask</code> TODO (描述该类做什么)
 * @author   GZEPF
 * @version  V1.0
 * @date	 2019 2019年1月13日 下午4:32:18
 */
@Component
public class ExecutionTask {
	
	@Autowired
	private TaskEventService taskEventService;
	@Autowired
	WebApplicationContext wac;
 
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
    /**
     * @param obj           一定要传一个已初始化对象(因为方法invoke的时候用此对象调用)
     * @param methodName    方法名
     * @param args          方法参数
     */
    public Object executionMethod(String className, String methodName, Object... args){
    	
        try {
            Class<?> clazz =  wac.getBean(className).getClass(); 
            Class[] classes = null;
            if (args != null && args.length != 0){
                List<Object> parameters = Arrays.asList(args);
                classes = new Class[parameters.size()];
                for (int i = 0; i < parameters.size(); i++) {
                    classes[i] = parameters.get(i).getClass();
                }
            }
            Method method = clazz.getMethod(methodName, classes);
            Object reObject=method.invoke(wac.getBean(className), args);
            return reObject;
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("ExecutionTask.executionMethod:出错了...");
            return "error";
        }
    }
    
    
    /**
     * 执行TaskNodeConfig理的配置
     * @return 
     * @date 2019年1月18日 上午10:30:01
     */
    public void executionTaskNodeConfig(BaseBusinessEntity businesses){
    	String  taskEvent =businesses.getTaskNode().getTaskEvent();
    	if(StringUtil.isNotEmpty(taskEvent)){
    		List<ExcutionEntity> methods= new ArrayList<>();
    		String[]  taskEvents =taskEvent.split(",");
    		for(int i=0;i<taskEvents.length;i++){
    			TaskEventEntity taskEventEntity=  taskEventService.getTaskEventByCode(taskEvents[i]);
    			if(taskEventEntity!=null){
    				//执行参数列表
    				Map<String, Object >params= new HashMap<>();
    				params.put("businesses", businesses);
    				methods.add(new ExcutionEntity(taskEventEntity.getClassPath(), taskEventEntity.getMethod(),params));
    			}
    		}
    		methods.forEach(excutionEntity->{
    			executionMethod(excutionEntity.getClazz(), excutionEntity.getMethod(),excutionEntity.getParams());
    		});
    		
    	}
    }
    
    
    
    public static void main(String[] args) {
		ExecutionTask executionTask = new ExecutionTask();
		//执行方法列表
		List<ExcutionEntity> methods= new ArrayList<>();
		//执行参数列表
		Map<String, Object >params= new HashMap<>();
		params.put("flowType", "urge");
		params.put("phone", "13985138082");
		params.put("msg", "短信内容");
		methods.add(new ExcutionEntity("com.epf.oa.utills.ExecutionTask", "getName",params));
		
		Map<String, Object >params2= new HashMap<>();
		//执行参数列表
		params2.put("flowType", "urge-other");
		params2.put("phone", "13985138082-other");
		params2.put("msg", "短信内容-other");
		methods.add(new ExcutionEntity("com.epf.oa.utills.ExecutionTask", "getName2",params2));
		
		methods.forEach(excutionEntity->{
			executionTask.executionMethod(excutionEntity.getClazz(), excutionEntity.getMethod(),excutionEntity.getParams());
		});
	}
 
	
	public String  getName(HashMap<String, String > params) {
		System.out.println("********************发送短信**************************");
		return "";
	}
	
	public String  getName2(HashMap<String, String > params) {
		System.out.println("********************执行方法**************************");
		return "";
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值