第一步:设计数据库表,根据channel_id查找到类名、开关字段,用于判断开关和根据类名用spring容器获取其实例,截图如下
第二步:定义一个全局的spring上下文类,定义其静态方法获取其bean的实例的方法,代码如下:
public class ApplicationContextHolder {
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
protected static void setApplicationContext(ApplicationContext applicationContext) {
ApplicationContextHolder.applicationContext = applicationContext;
}
/**
* 获取bean
* @param name service注解方式name为小驼峰格式
* @return Object bean的实例对象
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
}
第三部定义一个抽象类和抽象方法,写一个子类继承该抽象类并且实现其方法,另外写一个类去操作路由到具体的实现类,兵器执行excute(),代码如下
抽象类:
public abstract class AbstractRouteService {
public abstract Object excute(JSONObject json);
}
具体实现类;
@Component
public class ApplyInfoService extends AbstractRouteService{
/* 日志对象初始化 第三方*/
private static Logger logger = LoggerFactory.getLogger(ApplyInfoService.class);
@Override
public Object excute(JSONObject json) {
ApplSubmitInfoInputDTO inputDTO = new ApplSubmitInfoInputDTO();
BaseOutputDTO outDTO = new BaseOutputDTO();
try {
logger.info("ApplyInfoService excute(){}:"+JSON.toJSONString(json));
ApplLimitService apptOthService = DubboFactory.getDubboService(ApplLimitService.class, "1.0.0",
"basic.zk.address");
inputDTO.setIdNo(json.getString("IdNo"));
SubmitInfoBo bo = apptOthService.applSubmitInfo(inputDTO);
return bo;
} catch (Exception e){
logger.error("rest 异常{}", e.getMessage());
outDTO.setCode(CommonReturnCode.ERR_UNKNOWN_ERROR.getCode());
outDTO.setMsg(CommonReturnCode.ERR_UNKNOWN_ERROR.getMsg());
}
return outDTO;
}
}
路由类:
@Component
public class RouteService extends BaseService<IntfChannelTransCtrl> {
/* 日志对象初始化 第三方*/
private static Logger logger = LoggerFactory.getLogger(RouteService.class);
@Autowired
private IntfChannelTransCtrlMapper intfChannelTransCtrlMapper;
public Object excute(String serviceId,JSONObject param){
JSONObject json = new JSONObject();
IntfChannelTransCtrl req = new IntfChannelTransCtrl();
req.setServiceId(serviceId);
//IntfChannelTransCtrl info = intfChannelTransCtrlMapper.getById(req);
IntfChannelTransCtrl info = req;
info.setServiceSts("0");
info.setClassName("applyInfoService");
if(info!=null && info.getServiceSts().equals("0")){ //0表示状态启用
String className = info.getClassName();
//反射方式执行
Object obj = ApplicationContextHolder.getBean(className);
AbstractRouteService abstractRouteService = (AbstractRouteService) obj;
Object res = abstractRouteService.excute(param);
logger.info("ApplyInfoService excute()正常响应结果{}:"+JSON.toJSONString(res));
}else{
json.put("code", "99999");
json.put("msg", "处理失败");
logger.error("ApplyInfoService excute()入参{}:"+JSON.toJSONString(param));
}
return json;
}
@Override
protected BaseMapper<IntfChannelTransCtrl> getMapper() {
// TODO Auto-generated method stub
return intfChannelTransCtrlMapper;
}
}
第四部:前端测试controller,代码如下
/*@param serviceId 服务ID
* 测试执行路由service
*/
@RequestMapping(value="/excute" ,method = RequestMethod.GET)
public Object excute(@RequestBody String serviceId,HttpServletRequest request){
Object res = new Object();
JSONObject param = new JSONObject();
try {
res = routeService.excute(serviceId, param);
} catch (Exception e){
logger.error("rest 异常{}", e.getMessage());
}
return res;
}