springMVC使用反射调用方法

18 篇文章 0 订阅
@Override
	public ApiPageVO run(AlEnginTrackTreeVO tree) {
        String currMethod = tree.getCurrMethod();
		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 = clazz.getMethod("FCProcessor_"+currMethod, params);
				result= (ApiPageVO) method.invoke(clazz.newInstance(), 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);
	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 = tree.getCurrMethod();
		if(currMethod!=null) {
			Class<?> clazz;
			Method method;
            ApiPageVO result= new ApiPageVO();
			try {

                //获取spring的xml文件,也就是写了注解扫描包的那个xml

//                 ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
                ApplicationContext context = SpringContextUtil.getApplicationContext();

                //获取类,json1.getString("className")是我json中参数

                Class<?>  cls = context.getBean("taskFlowExercisesServiceImpl").getClass();

                //获取指定的方法

                // 定义参数类型
                Class[] params = new Class[1];
                params[0] = AlEnginTrackTreeVO.class;
                Method m = cls.getMethod("FCProcessor_"+currMethod,params);

                //调用invoke方法

                result = (ApiPageVO) m.invoke(context.getBean("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 = clazz.getMethod("FCProcessor_"+currMethod, params);
//				result= (ApiPageVO) method.invoke(clazz.newInstance(), 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 org.springframework.context.ApplicationContext;
import org.springframework.context.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 {
		return applicationContext.getBean(name);
	}

	/**
	 * 获取类型为requiredType的对象
	 * 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
	 * 
	 * @param name
	 *            bean注册名
	 * @param requiredType
	 *            返回对象类型
	 * @return Object 返回requiredType类型对象
	 * @throws BeansException
	 */
	public static Object getBean(String name, Class requiredType) throws BeansException {
		return applicationContext.getBean(name, requiredType);
	}

	/**
	 * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
	 * 
	 * @param name
	 * @return boolean
	 */
	public static boolean containsBean(String name) {
		return applicationContext.containsBean(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 {
		return applicationContext.getType(name);
	}

	/**
	 * 如果给定的bean名字在bean定义中有别名,则返回这些别名
	 * 
	 * @param name
	 * @return
	 * @throws NoSuchBeanDefinitionException
	 */
	public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
		return applicationContext.getAliases(name);
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值