当前位置:网站首页>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 .
边栏推荐
猜你喜欢

10000小時定律不會讓你成為編程大師,但至少是個好的起點

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

Integration test practice (1) theoretical basis

Software testing learning - day 3

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

10000小时定律不会让你成为编程大师,但至少是个好的起点

Use the jvisualvm tool ----- tocmat to start JMX monitoring

Software testing assignment - day 1

Practice of enterprise ab/testing platform

On the practice of performance optimization and stability guarantee
随机推荐
crontab定时任务
Summary of the design and implementation of the weapon system similar to the paladin of vitality
In depth analysis of reentrantlock fair lock and unfair lock source code implementation
opencv
[untitled] 8 simplified address book
JS date comparison
Software testing assignment - day 1
Tool class static method calls @autowired injected service
[untitled]
Learning notes -- principles and comparison of k-d tree and IKD tree
Scroll view specifies the starting position of the scrolling element
Troubleshooting of high CPU load but low CPU usage
Arctic code vault contributor
[day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
Unit test framework + Test Suite
php安装swoole扩展
Simple password lock
(翻译)异步编程:Async/Await在ASP.NET中的介绍
Error c2017: illegal escape sequence
php artisan