springboot动态调用实现类
定义规则的多种类型
public enum RuleType {
MYRULEBYID1,
MYRULEBYID2
}
接口
import java.util.List;
import java.util.Map;
public interface DataRuleParse {
RuleType getRuleType();
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");
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。