In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The content of this article mainly focuses on how to kill too much if else in Spring Boot. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
Demand
Here is a virtual business requirement, which is easy for everyone to understand. Suppose there is an order system in which one function is to make different processing according to different types of orders.
Order entity:
Service interface:
Traditional realization
Write a bunch of if else based on the order type:
Policy pattern implementation
With the policy pattern, it takes only two lines to implement the business logic:
You can see that HandlerContext is injected into the above method, which is a processor context that holds different business processors, as explained below. We get an abstract processor AbstractHandler from it and call its methods to implement the business logic.
You can now see that our main business logic is implemented in the processor, so there should be as many processors as there are order types. After the demand changes, the order type is added, and you only need to add the corresponding processor. The above OrderServiceV2Impl does not need to be changed at all.
Let's first look at the writing of the business processor:
First, each processor must be added to the spring container, so you need to add the @ Component annotation. Secondly, you need to add a custom annotation @ HandlerType to identify which order type the processor corresponds to. * inherits AbstractHandler to implement your own business logic.
Custom annotation @ HandlerType:
Abstract processor AbstractHandler:
Custom annotations and abstract handlers are simple, so how do you register the processor with the spring container?
The specific ideas are:
1. Scan the class marked @ HandlerType in the specified package
2. Save the type value in the annotation as key and the corresponding class as value in Map
3. Initialize the HandlerContext with the above map as the constructor parameter, and register it in the spring container
We encapsulate the core functions in the HandlerProcessor class to complete the above functions.
HandlerProcessor:
ClassScaner:
ClassScaner: scanning tool class source code
HandlerProcessor needs to implement BeanFactoryPostProcessor and register the custom bean with the container before spring processes the bean.
Now that the core work is done, take a look at how HandlerContext gets the corresponding processor:
HandlerContext:
BeanTool: get the bean utility class
The # getInstance method gets the corresponding class according to the type, and then obtains the bean registered in the spring based on the class type.
Finally, note that HandlerProcessor and BeanTool must be scanned or explicitly registered through @ Bean to work at project startup.
The use of policy pattern can simplify the complicated if else code and facilitate maintenance, while the use of custom annotations and self-registration can facilitate the response to changes in requirements. The editor only provides a general idea, and there are many details that can be changed flexibly, such as using enumerated types or static constants as the type of order. I believe you can think of more and better ways.
Thank you for your reading. I believe you have a certain understanding of "how to kill too much if else in Spring Boot". Go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!
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.