亲宝软件园·资讯

展开

springboot动态调用实现类 springboot动态调用实现类方式

Gr_lbxx 人气:8
想了解springboot动态调用实现类方式的相关内容吗,Gr_lbxx在本文为您仔细讲解springboot动态调用实现类的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:springboot实现类,动态调用实现类,下面大家一起来学习吧。

springboot动态调用实现类

定义规则的多种类型

/**
 * 数据规则处理类型枚举
 */
public enum RuleType {
    MYRULEBYID1,
    MYRULEBYID2
}

接口

import java.util.List;
import java.util.Map;
 
public interface DataRuleParse {
    /**
     * 获取规则处理的类型
     * @return
     */
      RuleType getRuleType();
 
    /**
     * 根据规则处理类型生成sql
     * @return
     */
   List<String> getSQl( Map<String,Object> paramMap);
}

实现类,规则类型1

package gds.application.masterdata.DataRuleParse; 
import gds.application.common.constants.ConnectorConstants;
import gds.application.common.constants.XmlConfigurationContants;
import gds.application.common.util.ApplicationContextUtil;
import gds.application.masterdata.constants.ConnectDataProcessesConstants;
import gds.application.masterdata.service.impl.BatchExecutionSqlServiceImpl;
import org.springframework.stereotype.Component;  
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Component
public class MYRULEBYID1 implements DataRuleParse{   
    @Override
    public RuleType getRuleType() {
        return RuleType.MYRULEBYID1;
    }
 
    @Override
    public List<String> getSQl( Map<String,Object> paramMap) {
       //规则1实现方法
        return null;
    }

实现类,规则类型2

package gds.application.masterdata.DataRuleParse; 
import gds.application.common.constants.ConnectorConstants;
import gds.application.common.constants.XmlConfigurationContants;
import gds.application.common.util.ApplicationContextUtil;
import gds.application.masterdata.constants.ConnectDataProcessesConstants;
import gds.application.masterdata.service.impl.BatchExecutionSqlServiceImpl;
import org.springframework.stereotype.Component;  
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Component
public class MYRULEBYID2 implements DataRuleParse{   
    @Override
    public RuleType getRuleType() {
        return RuleType.MYRULEBYID2;
    }
 
    @Override
    public List<String> getSQl( Map<String,Object> paramMap) {
       //规则2实现方法
        return null;
    }

工厂类,调用方法获取具体实现类

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 规则转换工厂类
 */
@Component
public class DataRuleParseFactory implements ApplicationContextAware {
    private static Map<String, DataRuleParse> dataRuleBeanMap;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
        Map<String, DataRuleParse> map = applicationContext.getBeansOfType(DataRuleParse.class);
        dataRuleBeanMap = new HashMap<String, DataRuleParse>();
        map.forEach((key, value) -> dataRuleBeanMap.put(value.getRuleType().toString(), value));
    }
 
    public static <T extends DataRuleParse> T getTrafficMode(String ruleType) {
        return (T)dataRuleBeanMap.get(ruleType);
    }
}

根据参数获取对应的方法,ruleName为MYRULEBYID1或者MYRULEBYID2

  DataRuleParse dataRuleParse= DataRuleParseFactory.getTrafficMode(ruleName);
  List<String> sqlList=dataRuleParse.getSQl(paramMap);

springboot手动获取实现类

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
WebApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(sc);
weixinService = (IWeixinService) cxt.getBean("IWeixinService");
qqService = (IQQService) cxt.getBean("IQQService");

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论