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");

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: