通过方法名和类名去调用方法

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import com.sinosig.app.base.exception.BusinessException;
import com.sinosig.app.base.global.ApplicationContextListener;
import com.sinosig.app.indexcal.domain.IndexCalParameterDto;

public class ClassMethodCall {
    /**
     * 
     * @param className
     *            类名
     * @param methodName
     *            方法名
     * @param parameter
     *            参数列表
     * @return
     * @throws Exception
     * @throws SecurityException
     */
    @SuppressWarnings("rawtypes")
    public static Object indexCallJava(String indexName, String beanId,
            String methodName, IndexCalParameterDto parameter) throws Exception {
        try {
            Object obj = null;
            Class[] partypes = null;
            obj = ApplicationContextListener.getApplicationContext().getBean(
                    beanId);
            Method runMethod = null;
            // parameter.size()
            partypes = new Class[1];
            partypes[0] = IndexCalParameterDto.class;
            runMethod = obj.getClass().getMethod(methodName, partypes);
            if (!Modifier.isPublic(runMethod.getModifiers())) {
                return null;
            }
            return runMethod.invoke(obj, parameter);
        } catch (Exception e) {
            if (e instanceof InvocationTargetException) {
                Throwable throwableException = ((InvocationTargetException) e)
                        .getTargetException();
                if (throwableException instanceof BusinessException) {
                    throw (BusinessException) throwableException;
                } else {
                    throw e;
                }
            } else {
                throw e;
            }
        }

    }
}
 

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Application Lifecycle Listener implementation class ApplicationListener
 *
 */
public class ApplicationContextListener implements ServletContextListener {
    private static transient WebApplicationContext springContext;
    
    /**
     * 获取ApplicationContext对象</p>
     * <p>Description: TODO</p>
     * @return
     * @author wxy  2016年1月11日 上午11:54:06
     */
    public static ApplicationContext getApplicationContext()
    {
        return springContext;
    }
    
    public ApplicationContextListener() {
        super();
    }
    
    public void contextInitialized(ServletContextEvent event) {
        springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
        SystemConfig.springContext = springContext;
    }
    
    public void contextDestroyed(ServletContextEvent event) {
    }

    public static WebApplicationContext getSpringContext() {
        return springContext;
    }

    public static void setSpringContext(WebApplicationContext springContext) {
        ApplicationContextListener.springContext = springContext;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值