springboot Dynamic Call Implementation Class Mode
- 2021-12-12 08:24:20
- OfStack
Directory springboot dynamically calls implementation class springboot to manually obtain implementation class
springboot Dynamic Call Implementation Class
Defining multiple types of rules
/**
* Enumeration of data rule processing types
*/
public enum RuleType {
MYRULEBYID1 ,
MYRULEBYID2
}
Interface
import java.util.List;
import java.util.Map;
public interface DataRuleParse {
/**
* Gets the type of rule processing
* @return
*/
RuleType getRuleType();
/**
* Generated according to the rule processing type sql
* @return
*/
List<String> getSQl( Map<String,Object> paramMap);
}
Implementation class, rule type 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) {
// Rules 1 Implementation method
return null;
}
Implementation class, rule type 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) {
// Rules 2 Implementation method
return null;
}
Factory class, calling the method to get the concrete implementation class
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;
/**
* Rule Transformation Factory Class
*/
@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);
}
}
According to the parameters, the corresponding method is obtained, and ruleName is MYRULEBYID1 or MYRULEBYID2
DataRuleParse dataRuleParse= DataRuleParseFactory.getTrafficMode(ruleName);
List<String> sqlList=dataRuleParse.getSQl(paramMap);
springboot Manually Getting Implementation Classes
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");