当前位置:网站首页>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 .
边栏推荐
- Journal quotidien des questions (11)
- C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
- Operation principle of lua on C: Foundation
- How to plan well?
- Architecture notes
- Getting started with pytest
- Winter vacation work of software engineering practice
- Personally design a highly concurrent seckill system
- php安装composer
- Thoughts in Starbucks
猜你喜欢
[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle
Software testing assignment - the next day
Asynchronous programming: async/await in asp Net
Golang operation redis: write and read hash type data
EasyExcel
MySQL installation
Yolov3 learning notes
Jenkins
How to migrate or replicate VMware virtual machine systems
Operation principle of lua on C: Foundation
随机推荐
How to plan well?
POI excel percentage
机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
JS date comparison
多个全局异常处理类,怎么规定执行顺序
The list of "I'm crazy about open source" was released in the first week, with 160 developers on the list
In depth analysis of reentrantlock fair lock and unfair lock source code implementation
Integration test practice (1) theoretical basis
JMeter test result output
Inno setup production and installation package
[day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
mysql误删root账户导致无法登录
Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
Scroll view specifies the starting position of the scrolling element
Redis command
How can the server set up multiple interfaces and install IIS? Tiantian gives you the answer!
Tool class static method calls @autowired injected service
[untitled] 5 self use history
Laravel Web框架
Operation principle of lua on C: Foundation