springboot动态调用实现类方式
作者:Gr_lbxx 时间:2021-07-05 21:33:17
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");
来源:https://blog.csdn.net/Gr_lbxx/article/details/90486736
标签:springboot,调用,实现类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot将项目打包成war包的操作方法
2022-01-02 22:30:50
Java经典设计模式之模板方法模式定义与用法示例
2021-07-09 14:07:49
java实现上传文件类型检测过程解析
2023-11-09 12:04:21
java动态绑定和静态绑定用法实例详解
2022-04-17 19:55:20
Spring Boot如何优化内嵌的Tomcat示例详解
2023-11-13 17:52:53
springboot结合maven实现多模块打包
2022-01-16 07:13:51
Java消息队列JMS实现原理解析
2023-05-14 04:02:04
![](https://img.aspxhome.com/file/2023/4/81354_0s.png)
java 交换两个数据的方法实例详解
2021-12-06 00:56:04
Java中的显示锁ReentrantLock使用与原理详解
2021-11-14 07:04:00
![](https://img.aspxhome.com/file/2023/4/69694_0s.jpg)
Java实现选择排序
2021-06-30 16:53:55
![](https://img.aspxhome.com/file/2023/2/84602_0s.jpg)
Android 如何实现动态申请权限
2023-07-30 00:51:31
C#调用Java方法实例详解
2021-10-16 22:50:20
![](https://img.aspxhome.com/file/2023/9/85659_0s.png)
C#特性-迭代器(上)及一些研究过程中的副产品
2023-12-05 18:26:49
![](https://img.aspxhome.com/file/2023/1/80081_0s.jpg)
windows系统配置Java开发环境变量
2022-03-07 23:28:07
![](https://img.aspxhome.com/file/2023/2/62152_0s.jpg)
深入理解TextView实现Rich Text--在同一个TextView设置不同字体风格
2023-05-25 04:52:01
![](https://img.aspxhome.com/file/2023/8/84738_0s.gif)
java中sdk与jdk的区别详细解析
2023-11-25 03:37:14
SpringBoot启动yaml报错的解决
2021-09-09 22:58:14
![](https://img.aspxhome.com/file/2023/9/69089_0s.png)
生成8位随机不重复的数字编号的方法
2023-11-26 07:00:29
Java中对话框的弹出方法
2022-04-24 14:35:52
在Eclipse安装Spring boot插件的步骤(图文)
2023-01-14 05:04:05
![](https://img.aspxhome.com/file/2023/2/78352_0s.png)