这篇文章主要介绍了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);
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");
感谢你能够认真阅读完这篇文章,希望小编分享的“springboot如何动态调用实现类”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。