springboot动态调⽤实现类⽅式⽬录
springboot动态调⽤实现类
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 stants.ConnectorConstants;
import stants.XmlConfigurationContants;
import gds.applicationmon.util.ApplicationContextUtil;
import gds.stants.ConnectDataProcessesConstants;
import gds.application.masterdata.service.impl.BatchExecutionSqlServiceImpl;
import org.springframework.stereotype.Component;spring framework和spring的关系
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 stants.ConnectorConstants;
import stants.XmlConfigurationContants;
import gds.applicationmon.util.ApplicationContextUtil;
import gds.stants.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 t.ApplicationContext;
import t.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 = BeansOfType(DataRuleParse.class);        dataRuleBeanMap = new HashMap<String, DataRuleParse>();
map.forEach((key, value) -> dataRuleBeanMap.RuleType().toString(), value));
}
public static <T extends DataRuleParse> T getTrafficMode(String ruleType) {
return ((ruleType);
}
}
根据参数获取对应的⽅法,ruleName为MYRULEBYID1或者MYRULEBYID2
DataRuleParse dataRuleParse= TrafficMode(ruleName);
List<String> SQl(paramMap);
springboot⼿动获取实现类
import org.t.WebApplicationContext;
import org.t.support.WebApplicationContextUtils; WebApplicationContext cxt = WebApplicationContext(sc); weixinService = (IWeixinService) Bean("IWeixinService");
qqService = (IQQService) Bean("IQQService");
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。