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