java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot动态调用实现类

springboot动态调用实现类方式

作者:Gr_lbxx

这篇文章主要介绍了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");

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

您可能感兴趣的文章:
阅读全文