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

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

/**
 * 
 * ExecutionTask 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 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 methods= new ArrayList<>();
    		String[]  taskEvents =taskEvent.split(",");
    		for(int i=0;iparams= 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 methods= new ArrayList<>();
		//执行参数列表
		Mapparams= new HashMap<>();
		params.put("flowType", "urge");
		params.put("phone", "13985138082");
		params.put("msg", "短信内容");
		methods.add(new ExcutionEntity("com.epf.oa.utills.ExecutionTask", "getName",params));
		
		Mapparams2= 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 params) {
		System.out.println("********************发送短信**************************");
		return "";
	}
	
	public String  getName2(HashMap params) {
		System.out.println("********************执行方法**************************");
		return "";
	}
}









你可能感兴趣的:(java)