In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.