运用设计模式,把需要执行的类名配置在数据库,获取配置,再spring容器获取bean的实例并且执行其方法

本文详细介绍了一种基于Spring框架的路由服务设计与实现过程,包括数据库表设计、全局Spring上下文类定义、抽象类与具体实现类的创建,以及前端测试Controller的使用。通过这些步骤,可以有效地实现服务的动态路由和执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:设计数据库表,根据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;
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值