当前位置:网站首页>How to specify the execution order for multiple global exception handling classes
How to specify the execution order for multiple global exception handling classes
2022-07-03 06:55:00 【Gentleman jiejie】
background
In the project , We usually define a global exception handling class to handle exceptions uniformly , While standardizing the code, it also improves the development efficiency , With the global exception handling class , Except in special circumstances , Most of the time , We just need to throw the anomaly out , Subsequently, the exception handling class will uniformly log , Encapsulate the exception information into the unified response object and then return .
But sometimes , Large projects , Will encapsulate different basic project dependencies , Put some commonly used , Common functions are encapsulated into different projects , Then the rest of the projects introduce dependencies , To use these features , This can ensure the uniformity and standardization of relevant projects , And it is easier to expand these general functions in the future , In fact, it is also an abstract thought , If there is a global exception handling class in the basic dependency , The project introduces this basic dependency , This global exception handling class will be injected at startup , But this global exception handling class is not a special case exception handling that can meet individual projects , For example, it's not right sql abnormal , Special treatment shall be carried out for parameter verification exceptions , It will cause these exceptions to be finally caught and handled by the bottom exception , The output response result may not be what we want , At that time , We need to define the global exception handling classes required by our project .
Why should we specify the order of execution
Suppose we also define a global exception handling class in the project , When the project starts , Two global exception handling classes will be injected , and spring The logic for handling exceptions is straightforward , It is to traverse the global exception handler list in order , If the exception handler can handle the exception , Subsequent exception handlers will no longer handle this exception , So if the basic exception handler is at the top of the list , The special exception handler we defined is behind the basic exception handler , Then the specific exception we throw , It will still not be processed by a specific processor , The response result obtained is also the response information encapsulated by the basic exception handler . Therefore, it is important to specify the execution order of global exception handling classes .
How to specify the execution sequence ?
adopt @Order() annotation , The annotation is on the global exception handling class , The smaller the order , Earlier injection , We just need to guarantee @Order() The value of is smaller than the basic global exception handling class , You can control the order of the global exception handler list .
Provide a way to check and confirm
If used @Order() Annotations have not yet implemented the requirements , Then enter first DispatcherServlet.class -> Enter the processDispatchResult() Method -> Enter processHandlerException() Method , you 're right , This processHandlerException() Method is the method of handling exceptions .
Interception part processHandlerException() The code in the method , We can break here :
By looking at HandlerExceptionResolver Class exceptionHandlerAdviceCache attribute , We can view the global exception handler list , The general structure of this attribute is as follows :
private final Map<ControllerAdviceBean, ExceptionHandlerMethodResolver> exceptionHandlerAdviceCache = new LinkedHashMap();
You can find that the global exception handler list is through LinkedHashMap For maintenance. , So we can see the global exception handler in LinkedHashMap The sorting , We can know whether the global exception handler we define is successfully sorted in front of the basic global exception handler .
边栏推荐
- How to plan well?
- php安装swoole扩展
- Software testing learning - day one
- Mise en place d'un environnement de développement de fonctions personnalisées
- 2022年华东师范大学计科考研复试机试题-详细题解
- Thoughts in Starbucks
- Example of joint use of ros+pytoch (semantic segmentation)
- Tool class static method calls @autowired injected service
- Reading notes of "learn to ask questions"
- Class and object summary
猜你喜欢

熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?

Integration test practice (1) theoretical basis

The 10000 hour rule won't make you a master programmer, but at least it's a good starting point

HMS core helps baby bus show high-quality children's digital content to global developers

Sorting out the core ideas of the pyramid principle

Software testing assignment - the next day

golang操作redis:写入、读取hash类型数据

New knowledge! The virtual machine network card causes your DNS resolution to slow down

IC_ EDA_ All virtual machine (rich Edition): questasim, vivado, VCs, Verdi, DC, Pt, spyglass, icc2, synthesize, innovative, ic617, mmsim, process library

On the practice of performance optimization and stability guarantee
随机推荐
centos php7.2.24升级到php7.3
Integration test practice (1) theoretical basis
Yolov3 learning notes
爬虫代码基础教学
Unit test notes
UTC time, GMT time, CST time
The list of "I'm crazy about open source" was released in the first week, with 160 developers on the list
On the practice of performance optimization and stability guarantee
Use the jvisualvm tool ----- tocmat to start JMX monitoring
Example of joint use of ros+pytoch (semantic segmentation)
Golang operation redis: write and read hash type data
MySQL installation
Yolov1 learning notes
Unit test framework + Test Suite
SQL implementation merges multiple rows of records into one row
Golang operation redis: write and read kV data
dataworks自定義函數開發環境搭建
Understand software testing
卡特兰数(Catalan)的应用场景
[LeetCode]404. Sum of left leaves