public class BaseController { /** * 通过指定的bean处理业务,非核心流程,不能支持java 动态代理对象@transactional注解的类 * @param bean * @throws Throwable */ @SuppressWarnings("unchecked") protected void process(BeanBase bean) throws Throwable { bean.setDatas(Util.converToBean(bean)); String processorName = bean.getNeedcontrolprocess(); if(processorName == null || processorName == ""){ return; } String[] beanAndMethod = processorName.split("\\."); String beanName = beanAndMethod[0]; //Service层里的方法名 String methodName = "process"; if(beanAndMethod.length > 1){ methodName = beanAndMethod[1]; } //spring 反射获得Service层的服务类beanName=taxFavInfo(@Service("taxFavInfo")) Object processBean = ApplicationContextHepler .getBeanByName(beanName); if (processBean == null) { throw new ServiceException("数据格式非法,没有找到相应的流程!"); } bean.setDatas(Util.converToBean(bean)); //获得运行时Class实例 @SuppressWarnings("rawtypes") Class clazz = processBean.getClass(); @SuppressWarnings("rawtypes") Class cz[] = new Class[1]; cz[0] = BeanBase.class; Method method = null; try { //Service层里的methodName(BeanBase bean)参数是BeanBase类型。 method = clazz.getDeclaredMethod(methodName,cz); //反射机制调用方法 参数1:service里的全路径,参数2:methodName方法的参数 method.invoke(processBean,bean); bean.setContent(Util.converToStr(bean)); } catch (NoSuchMethodException e) { throw new ServiceException("没找到相应的处理方法!"); } catch (SecurityException e) { throw new ServiceException("安全检查不通过!"); } catch (IllegalAccessException e) { throw new ServiceException("非法访问!"); } catch (IllegalArgumentException e) { throw new ServiceException("参数不合法!"); } catch (InvocationTargetException e) { if(e.getTargetException() != null){ if(e.getTargetException() instanceof ServiceException){ throw (ServiceException)e.getTargetException(); }else{ throw e.getTargetException(); } }else{ throw new ServiceException("调用失败"+e.getMessage()); } } } /** * 获取创建者工号字符串 * 方法说明:<br> * * @return */ protected String getCreateId() { return "|createId:"+UserUtil.getCurrentUser().getEmpCode(); } }
java 反射机制应用
猜你喜欢
转载自yaohongxing.iteye.com/blog/2282939
今日推荐
周排行