In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use Spring to implement strategic patterns". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
You should be familiar with the @ Autowired annotation in Spring, and those who have used Spring can not do without this annotation, which can help us to automatically inject the Bean we want.
In addition to this basic functionality, @ Autowired has more powerful capabilities, such as injecting arrays of specified types, List/Set collections, and even Map objects.
For example, the current application has a payment interface PayService, which needs to connect Alipay, WeChat Pay and bank cards respectively, so there are three different implementation classes AliPayService,WechatPayservice,BankCardPayService.
If I need to get all the PayService Bean of the current system class at this time, the old way we can only get it through BeanFactory or ApplicationContext.
/ / first get all the BeanString of PayService type through getBeanNamesForType [] names = ctx.getBeanNamesForType (PayService.class); List anotherPayService = Lists.newArrayList (); for (String beanName: names) {anotherPayService.add (ctx.getBean (beanName, PayService.class));} / or get all PayService types Map beansOfType = ctx.getBeansOfType (PayService.class) through getBeansOfType; for (Map.Entry entry: beansOfType.entrySet ()) {anotherPayService.add (entry.getValue ());}
But now that we don't have to bother, we can directly use @ Autowired to inject the PayService Bean array, or the PayService List/Set collection, or even the Map collection of PayService.
@ AutowiredList payServices;@AutowiredPayService [] payServicesArray
Knowing this feature, it is very simple when we need to implement the policy pattern using Spring.
Some partners may not know much about the strategy model, it doesn't matter, this kind of fan introduces a business scenario, through this scenario to introduce you to the strategy model.
Or the above example, our current system needs to dock WeChat Pay, Alipay, and bank card payment.
When we receive this requirement, we first need to get the corresponding interface document and analyze the commonness of the three.
Suppose we find here that the three patterns are similar, but some of the parameters are different.
So we abstract a set of common interfaces PayService according to the commonness of the three.
Public interface PayService {PayResult epay (PayRequest request);}
Then implement three implementation classes, all of which inherit this interface.
So now the question is, since there are three implementation classes, how to choose a specific implementation class?
In fact, this problem is easy to solve, the request parameter is passed in a unique identity, and then we select the corresponding implementation class according to the identity.
For example, we have a channelNo field in the request class PayRequest, which represents the unique ID of the corresponding payment channel. For example, Alipay is 00000001, WeChat Pay is 00000002, and bank card payment is 00000003.
Then we need to map the unique identity to the concrete implementation class one by one, and we can just use Map to store this mapping relationship.
We implement a RouteService. The specific code logic is as follows:
@ Servicepublic class RouteService {@ Autowired Map payServiceMap; public PayResult epay (PayRequest payRequest) {PayService payService = payServiceMap.get (payRequest.getChannelNo ()); return payService.epay (payRequest);}}
We automatically inject all the relevant Bean of PayService in RouteService, and then use the unique identity to find the implementation class.
In this way, we shield the differences in payment channels, and other service classes only need to call RouteService.
However, there is still a slight problem with this implementation, because we are uniquely identified as a string of numbers, so if we directly use @ Autowired to inject Map as above, we need to implement the class with a Bean name of 00000001.
But this kind of naming is not very elegant, which will make it difficult for students to understand and maintain.
So we need to make a transformation, and we can do this.
First, let's modify the PayService interface by adding a method through which each concrete implementation class returns its unique identity.
Public interface PayService {PayResult epay (PayRequest request); String channel ();}
Specific examples of Alipay implementation class code, other implementation classes are similar.
@ Service ("aliPayService") public class AliPayService implements PayService {@ Override public PayResult epay (PayRequest request) {/ / business logic return new PayResult ();} @ Override public String channel () {return "00000001";}}
Finally, let's modify RouteService. The specific logic is as follows:
Servicepublic class RouteService {@ Autowired Set payServiceSet; Map payServiceMap; public PayResult epay (PayRequest payRequest) {PayService payService = payServiceMap.get (payRequest.getChannelNo ()); return payService.epay (payRequest);} @ PostConstruct public void init () {for (PayService payService: payServiceSet) {payServiceMap = new HashMap (); payServiceMap.put (payService.channel (), payService);}
The above code first automatically injects a collection of PayService, and then we convert it to a Map, so that the internal storage happens to be a unique mapping between the identity and the implementation class.
That's all for "how to use Spring to implement a strategic pattern". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.