当前位置:网站首页>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 .
边栏推荐
- 卡特兰数(Catalan)的应用场景
- 服务器如何设置多界面和装IIS呢?甜甜给你解答!
- Practical plug-ins in idea
- Inno setup production and installation package
- Realize PDF to picture conversion with C #
- The list of "I'm crazy about open source" was released in the first week, with 160 developers on the list
- Jenkins
- dataworks自定義函數開發環境搭建
- How to migrate or replicate VMware virtual machine systems
- Shim and Polyfill in [concept collection]
猜你喜欢

机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)

JMeter JSON extractor extracts two parameters at the same time

2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution

Software testing assignment - the next day

卡特兰数(Catalan)的应用场景

dataworks自定义函数开发环境搭建

(翻译)异步编程:Async/Await在ASP.NET中的介绍

Inno Setup 制作安装包
![[5g NR] UE registration process](/img/e3/f881d51fba03010de8c45ea480f3f0.png)
[5g NR] UE registration process

Example of joint use of ros+pytoch (semantic segmentation)
随机推荐
Practical plug-ins in idea
mysql误删root账户导致无法登录
The difference between CONDA and pip
Personally design a highly concurrent seckill system
JMeter test result output
这两种驱蚊成份对宝宝有害,有宝宝的家庭,选购驱蚊产品要注意
Getting started with pytest
opencv
The pressure of large institutions in the bear market has doubled. Will the giant whales such as gray scale, tether and micro strategy become 'giant thunder'?
Code management tools
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)
2022年华东师范大学计科考研复试机试题-详细题解
Sorting out the core ideas of the pyramid principle
My 2020 summary "don't love the past, indulge in moving forward"
dataworks自定义函数开发环境搭建
Practice of enterprise ab/testing platform
Know flex box
Shim and Polyfill in [concept collection]
Inno Setup 制作安装包
A letter to graduating college students