springMVC使⽤反射调⽤⽅法
@Override
public ApiPageVO run(AlEnginTrackTreeVO tree) {
String currMethod = CurrMethod();
if(currMethod!=null) {
Class<?> clazz;
Method method;
ApiPageVO result= new ApiPageVO();
try {
clazz = TaskFlowExercisesServiceImpl.class;
// 定义参数类型
Class[] params = new Class[1];
params[0] = AlEnginTrackTreeVO.class;
//    for(int i=0;i<currParam.length;i++) {
//    params[i] = String.class;
//    }
method = Method("FCProcessor_"+currMethod, params);
result= (ApiPageVO) method.wInstance(), tree);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e    e.printStackTrace();
}
if(result!=null) {
return result;
}else {
return null;
}
}
return null;
}
这样就可以根据其中某个属性值来调⽤不同的⽅法
ApiPageVO FCProcessor_002(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_003(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_004(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_005(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_006(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_007(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_008(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_009(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_010(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_011(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_012(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_013(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_014(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_015(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_016(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_017(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_018(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_019(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_020(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_021(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_022(AlEnginTrackTreeVO tree);
springmvc的注解有哪些
ApiPageVO FCProcessor_023(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_024(AlEnginTrackTreeVO tree);
ApiPageVO FCProcessor_999(AlEnginTrackTreeVO tree);
但是在spring调⽤反射时会出现Autowired⽆法使⽤注⼊的坑,这个时候就得改下
@Override
public ApiPageVO run(AlEnginTrackTreeVO tree) {
String currMethod = CurrMethod();
if(currMethod!=null) {
Class<?> clazz;
Method method;
ApiPageVO result= new ApiPageVO();
try {
//获取spring的xml⽂件,也就是写了注解扫描包的那个xml
//                ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"l"});
ApplicationContext context = ApplicationContext();
//获取类,String("className")是我json中参数
Class<?>  cls = Bean("taskFlowExercisesServiceImpl").getClass();
//获取指定的⽅法
// 定义参数类型
Class[] params = new Class[1];
params[0] = AlEnginTrackTreeVO.class;
Method m = Method("FCProcessor_"+currMethod,params);
//调⽤invoke⽅法
result = (ApiPageVO) m.Bean("taskFlowExercisesServiceImpl"),tree);
// ⽼⽅法,spring⽆法注⼊
// 定义参数类型
/
/                Class[] params = new Class[1];
//                params[0] = AlEnginTrackTreeVO.class;
//    for(int i=0;i<currParam.length;i++) {
//    params[i] = String.class;
//    }
//    method = Method("FCProcessor_"+currMethod, params);
//    result= (ApiPageVO) method.wInstance(), tree);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException  e) {    e.printStackTrace();
}
if(result!=null) {
return result;
}else {
return null;
}
}
return null;
}
⾄于  SpringContextUtil  这个:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import t.ApplicationContext;
import t.ApplicationContextAware;
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; // Spring应⽤上下⽂环境
/**
* 实现ApplicationContextAware接⼝的回调⽅法,设置上下⽂环境
*
* @param applicationContext
* @throws BeansException
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
/
**
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取对象
*
* @param name
* @return Object ⼀个以所给名字注册的bean的实例
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
Bean(name);
}
/**
* 获取类型为requiredType的对象
* 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
*
* @param name
*            bean注册名
* @param requiredType
*            返回对象类型
* @return Object 返回requiredType类型对象
* @throws BeansException
*/
public static Object getBean(String name, Class requiredType) throws BeansException {
Bean(name, requiredType);
}
/**
* 如果BeanFactory包含⼀个与所给名称匹配的bean定义,则返回true
*
* @param name
* @return boolean
*/
public static boolean containsBean(String name) {
ainsBean(name);
}
/**
* 判断以给定名字注册的bean定义是⼀个singleton还是⼀个prototype。
* 如果与给定名字相应的bean定义没有被到,将会抛出⼀个异常(NoSuchBeanDefinitionException)  *
* @param name
* @return boolean
* @throws NoSuchBeanDefinitionException
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {  return applicationContext.isSingleton(name);
}
/**
* @param name
* @return Class 注册对象的类型
* @throws NoSuchBeanDefinitionException
*/
public static Class getType(String name) throws NoSuchBeanDefinitionException {
Type(name);
}
/**
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
*
* @param name
* @return
* @throws NoSuchBeanDefinitionException
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {  Aliases(name);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。