Python 官方文档:入门教程 => 点击学习
目录SpringBoot动态调用实现类springboot手动获取实现类springboot动态调用实现类 定义规则的多种类型 public enum RuleType {
定义规则的多种类型
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);
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");
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: springboot动态调用实现类方式
本文链接: https://lsjlt.com/news/157430.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0