Get the App
SLTechnology News&Howtos  ›  Development  › 

How to call the implementation class dynamically by springboot

Shulou Source: shulou.com Published: 2022-06-02 14:50:28 10月27日 Update

This article mainly introduces how springboot dynamically calls the implementation class, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.

Springboot dynamic invocation implementation class

Define 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 {/ * get the type of rule processing * @ return * / RuleType getRuleType (); / * generate sql * @ return * / List getSQl (Map paramMap) according to the rule processing type;}

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 @ Componentpublic class MYRULEBYID1 implements DataRuleParse {@ Override public RuleType getRuleType () {return RuleType.MYRULEBYID1;} @ Override public List getSQl (Map paramMap) {/ / Rule 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 @ Componentpublic class MYRULEBYID2 implements DataRuleParse {@ Override public RuleType getRuleType () {return RuleType.MYRULEBYID2;} @ Override public List getSQl (Map paramMap) {/ / Rule 2 implementation method return null;}

Factory class, calling method to get 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 conversion factory class * / @ Componentpublic class DataRuleParseFactory implements ApplicationContextAware {private static Map dataRuleBeanMap; @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {Map map = applicationContext.getBeansOfType (DataRuleParse.class); dataRuleBeanMap = new HashMap () Map.forEach ((key, value)-> dataRuleBeanMap.put (value.getRuleType (). ToString (), value);} public static T getTrafficMode (String ruleType) {return (T) dataRuleBeanMap.get (ruleType);}}

Obtain the corresponding method according to the parameter. RuleName is MYRULEBYID1 or MYRULEBYID2.

DataRuleParse dataRuleParse= DataRuleParseFactory.getTrafficMode (ruleName); List sqlList=dataRuleParse.getSQl (paramMap); springboot manually get the implementation class 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") Thank you for reading this article carefully. I hope the article "how to dynamically call the implementation class of springboot" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Rules types methods articles dynamic processing factories values interests parameters at the same time multiple manual interfaces data more friends knowledge ribbon industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information OPPO Reno Huawei Linux Shulou Tech Info