ReflectionUtils spring boot 提供反射工具类。
Method mh = ReflectionUtils.findMethod(SpringContextUtil.getBean(params.getBeanName()).getClass(),
params.getInvokMethod(), new Class[]{params.getParams().getClass()} );
Object obj = ReflectionUtils.invokeMethod(mh, SpringContextUtil.getBean(params.getBeanName()),
params.getParams());
SpringContextUtil
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* @author yingk
*/
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
/**
* 获得spring上下文
* @return ApplicationContext spring上下文
*/
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
/***
* 根据一个bean的id获取配置文件中相应的bean
*/
public static Object getBean(String beanId) throws BeansException {
if (applicationContext.containsBean(beanId)) {
return applicationContext.getBean(beanId);
}
return null;
}
/***
* 根据一个bean的类型获取配置文件中相应的bean
*/
public static <T> T getBeanByClass(Class<T> requiredType) throws BeansException {
return applicationContext.getBean(requiredType);
}
/**
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
*/
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
}