CN112083952A - Spring architecture-based exception handling method and system - Google Patents
Spring architecture-based exception handling method and system Download PDFInfo
- Publication number
- CN112083952A CN112083952A CN202010871621.2A CN202010871621A CN112083952A CN 112083952 A CN112083952 A CN 112083952A CN 202010871621 A CN202010871621 A CN 202010871621A CN 112083952 A CN112083952 A CN 112083952A
- Authority
- CN
- China
- Prior art keywords
- exception
- exception handling
- class
- global
- handling structure
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 55
- 238000012545 processing Methods 0.000 claims description 36
- 238000004590 computer program Methods 0.000 claims description 12
- 239000012141 concentrate Substances 0.000 abstract description 4
- 238000010586 diagram Methods 0.000 description 11
- 238000004891 communication Methods 0.000 description 3
- 238000012986 modification Methods 0.000 description 2
- 230000004048 modification Effects 0.000 description 2
- 230000003287 optical effect Effects 0.000 description 2
- 238000004364 calculation method Methods 0.000 description 1
- 230000007547 defect Effects 0.000 description 1
- 230000000694 effects Effects 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 238000003672 processing method Methods 0.000 description 1
- 238000006467 substitution reaction Methods 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/70—Software maintenance or management
- G06F8/72—Code refactoring
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/07—Responding to the occurrence of a fault, e.g. fault tolerance
- G06F11/0703—Error or fault processing not based on redundancy, i.e. by taking additional measures to deal with the error or fault not making use of redundancy in operation, in hardware, or in data representation
- G06F11/0751—Error or fault detection not based on redundancy
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/30—Information retrieval; Database structures therefor; File system structures therefor of unstructured textual data
- G06F16/35—Clustering; Classification
- G06F16/355—Class or cluster creation or modification
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Software Systems (AREA)
- Quality & Reliability (AREA)
- Data Mining & Analysis (AREA)
- Databases & Information Systems (AREA)
- Stored Programmes (AREA)
Abstract
The embodiment of the invention provides an exception handling method and system based on a spring framework, wherein the method comprises the following steps: acquiring an original exception handling structure; and modifying the original exception handling structure into a global exception handling structure, wherein the global exception handling structure comprises a plurality of classes for handling the business exception. The embodiment of the invention converts the program exception into the exception readable by the user by introducing the global exception handling class, improves the simplicity of the code, and also improves the reusability and maintainability of the code, so that developers can concentrate on handling the service logic better and improve the coding efficiency.
Description
Technical Field
The invention relates to the technical field of data processing, in particular to an exception handling method and system based on a spring framework.
Background
During the software development process, various exception bugs are inevitably required to be processed.
In order to ensure the robustness of the code, a large number of try-catch code blocks have to be added in the normal encoding process to capture the exception, so as to ensure that the program can normally run, but the processing mode is not elegant, a large number of redundant codes exist in the code, and the readability of the code is influenced greatly.
Therefore, it is necessary to provide a suitable processing method for the exception program, so that the code becomes more concise and easier to understand.
Disclosure of Invention
The embodiment of the invention provides an exception handling method and system based on a spring framework, which are used for solving the defect that a large amount of redundant codes are easy to generate when an exception is handled in software development in the prior art and realizing the purpose of processing exception data elegantly.
In a first aspect, an embodiment of the present invention provides an exception handling method based on a spring architecture, including:
acquiring an original exception handling structure;
and modifying the original exception handling structure into a global exception handling structure, wherein the global exception handling structure comprises a plurality of classes for handling the business exception.
Preferably, the modifying the original exception handling structure into a global exception handling structure, where the global exception handling structure includes several classes for handling the service exception specifically includes:
determining a preset exception tool class in the global exception handling structure;
adding the plurality of classes for processing the business exceptions in the preset exception tool class, wherein the plurality of classes for processing the business exceptions comprise an interface return and exception return general class, an enumeration class and an exception global processing class.
Preferably, the interface return and exception return general class is used for returning interface status information and exception status information.
Preferably, the enumeration class is used for recording return codes and return information of various exceptions and adding new exception handling information and state codes.
Preferably, the exception global processing class is used for performing class annotation and method annotation, the class annotation acts on the whole spring project, the method annotation acts on a controller level, and the priority of the method annotation is higher than that of the class annotation.
Preferably, the interface return and exception return general class is further used for modifying according to a preset requirement when the interface is called.
In a second aspect, an embodiment of the present invention further provides an exception handling system based on a spring architecture, including:
the acquisition module is used for acquiring an original exception handling structure;
and the processing module is used for reconstructing the original exception handling structure into a global exception handling structure, and the global exception handling structure comprises a plurality of classes for handling business exceptions.
Preferably, the processing module is specifically configured to:
the determining submodule is used for determining a preset exception tool class in the global exception handling structure;
and the adding submodule is used for adding the classes for processing the business exceptions in the preset exception tool class, wherein the classes for processing the business exceptions comprise an interface return and exception return general class, an enumeration class and an exception global processing class.
In a third aspect, an embodiment of the present invention further provides an electronic device, including a memory, a processor, and a computer program stored on the memory and executable on the processor, where the processor implements the steps of the exception handling method based on the spring architecture when executing the program.
In a fourth aspect, an embodiment of the present invention further provides a non-transitory computer readable storage medium, on which a computer program is stored, where the computer program, when executed by a processor, implements the steps of the spring architecture-based exception handling method as described in any one of the above.
According to the exception handling method and system based on the spring framework, provided by the embodiment of the invention, the exception of the program is converted into the exception readable by the user by introducing the global exception handling class, so that the simplicity of the code is improved, the reusability and maintainability of the code are also improved, developers can concentrate on handling good business logic, and the coding efficiency is improved.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, and it is obvious that the drawings in the following description are some embodiments of the present invention, and those skilled in the art can also obtain other drawings according to the drawings without creative efforts.
Fig. 1 is a schematic flowchart of an exception handling method based on a spring architecture according to an embodiment of the present invention;
FIG. 2 is a diagram of an original call division procedure provided by an embodiment of the present invention;
FIG. 3 is a schematic diagram of a global exception handling method according to an embodiment of the present invention;
FIG. 4 is a schematic diagram of an exception tool class provided by an embodiment of the present invention;
FIG. 5 is a generic class diagram of interface returns and exception returns provided by embodiments of the present invention;
FIG. 6 is a schematic diagram of an enumeration class according to an embodiment of the present invention;
FIG. 7 is a schematic diagram of an exception global handling class provided by an embodiment of the present invention;
FIG. 8 is a schematic diagram of an interface modification provided by an embodiment of the present invention;
fig. 9 is a schematic structural diagram of an exception handling system based on a spring architecture according to an embodiment of the present invention;
fig. 10 is a schematic structural diagram of an electronic device according to an embodiment of the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
Aiming at the problems in the prior art, the embodiment of the invention provides an exception handling method based on a spring framework.
Fig. 1 is a schematic flow chart of an exception handling method based on a spring architecture according to an embodiment of the present invention, as shown in fig. 1, including:
s1, acquiring an original exception handling structure;
s2, changing the original exception handling structure into a global exception handling structure, wherein the global exception handling structure comprises a plurality of classes for handling the business exception.
Specifically, in an original exception handling structure, a VO object class or an enumeration class for returning page result information is defined, and the VO object class or the enumeration class comprises an exception state code and exception information; defining a global exception handling class by self, and aiming at different exception self-defining methods, using a spring aop tangent plane technology to specify a corresponding exception method, or specifying through @ controlleraddview + @ ExceptionHandler annotation, and returning a result set which can be processed by a page in the exception method.
Note that the differences between @ ControllerAdvice and @ ExceptionHandler are as follows:
the ExceptionHandler is a method annotation, acts on the Controller level, and defines an exception handler for a Controller;
the controlleramdvice is a class annotation and acts on the whole Spring project, and the controlleramdvice annotation defines a global exception handler;
the priority of the ExceptionHandler is higher than that of the controllerrAdvice, namely, if the exception thrown by the Controller can be processed by the method labeled by the ExceptionHandler and the method in the class labeled by the controllerrAdvice, the method labeled by the ExceptionHandler is preferentially processed.
After the @ controllerservice + @ ExceptionHandler is used for carrying out global Controller layer exception processing, only the attention of business logic is needed, and a large amount of energy is not spent on writing redundant try catch code blocks, so that template codes are reduced, the code amount is reduced, and the expansibility and the maintainability are improved.
The embodiment of the invention converts the program exception into the exception readable by the user by introducing the global exception handling class, improves the simplicity of the code, and also improves the reusability and maintainability of the code, so that developers can concentrate on handling the service logic better and improve the coding efficiency.
Based on the above embodiment, step S2 specifically includes:
determining a preset exception tool class in the global exception handling structure;
adding the plurality of classes for processing the business exceptions in the preset exception tool class, wherein the plurality of classes for processing the business exceptions comprise an interface return and exception return general class, an enumeration class and an exception global processing class.
Specifically, for example, when a division interface needs to be called, if the divisor is 0, an exception is thrown, and if the divisor is not processed at this time, the system crashes or returns to a large stack of information that cannot be understood, at this time, it is necessary to use try-catch to process, if the divisor is 0, capture is performed, and if there are multiple services that require try-catch or a complex service that adds multiple catches in a try, the redundant code will be more increased, as shown in fig. 2.
At this time, a global exception handling manner needs to be modified, a class required for handling the service exception may be defined first, if there are multiple services, multiple exception classes may also be defined, and finally, the classes are identified in the global exception handling controller in a unified manner, as shown in fig. 3.
Here, an exception tool class is defined for handling the case where an exception occurs in the business, and all exception mode decisions in subsequent projects may be added to this class, as shown in fig. 4.
Based on any of the above embodiments, the interface return and exception return general class is used to return interface status information and exception status information.
Specifically, a generic class is defined for interface returns and exception returns, as shown in FIG. 5.
Based on any of the above embodiments, the enumeration class is used to record the return code and return information of various exceptions, and add new exception handling information and status code.
Specifically, an enumeration class is defined for recording return codes and return information of various exceptions, and new exception handling information and status codes may be added in the process of encoding, as shown in fig. 6.
Based on any of the above embodiments, the exception global processing class is used for performing class annotation and method annotation, the class annotation acts on the entire spring project, the method annotation acts on the controller level, and the priority of the method annotation is higher than that of the class annotation.
Specifically, an exception global processing class is defined, the annotation @ controlleraddress indicates that this class is an exception global processing class, and the annotation @ exception handler (value) indicates that as long as a BaseException exception is thrown in the previous method, the exception global processing class can be captured and processed by the method, as shown in fig. 7.
Based on any of the above embodiments, the interface return and exception return general class is further used for modifying according to a preset requirement when the interface is called.
Specifically, the interface class may be modified according to actual requirements, as shown in fig. 8.
After defining each category, the calling effect when no exception is thrown is as follows, and if the receiving parameter b is equal to 1, the interface returns a calculation result value c which is equal to 6.0; if the receiving parameter b is equal to 0, triggering BaseException through a checkZero method, capturing the BaseException in the exception global processing class, and finally returning the exception state code and exception information defined in the user enumeration class.
The following describes the exception handling system based on the spring architecture according to the embodiment of the present invention, and the exception handling system based on the spring architecture described below and the exception handling method based on the spring architecture described above may be referred to correspondingly.
Fig. 9 is a schematic structural diagram of an exception handling system based on a spring architecture according to an embodiment of the present invention, as shown in fig. 9, including: an acquisition module 91 and a processing module 92; wherein:
the obtaining module 91 is configured to obtain an original exception handling structure; the processing module 92 is configured to modify the original exception handling structure into a global exception handling structure, which includes classes for handling the business exceptions.
The embodiment of the invention converts the program exception into the exception readable by the user by introducing the global exception handling class, improves the simplicity of the code, and also improves the reusability and maintainability of the code, so that developers can concentrate on handling the service logic better and improve the coding efficiency.
Based on the above embodiment, the processing module 92 is specifically configured to:
the determining sub-module 921 is configured to determine a preset exception tool class in the global exception handling structure; the adding submodule 922 is configured to add the classes for processing the service exceptions in the preset exception tool class, where the classes for processing the service exceptions include an interface return and exception return general class, an enumeration class, and an exception global processing class.
Fig. 10 illustrates a physical structure diagram of an electronic device, and as shown in fig. 10, the electronic device may include: a processor 1020, a memory (memory)1030, and a communication bus (bus)1040, wherein the processor 1010, the communication interface 1020, and the memory 1030 communicate with each other via the communication bus 1040. The processor 1010 may call logic instructions in the memory 1030 to perform a spring architecture based exception handling method comprising: acquiring an original exception handling structure; and modifying the original exception handling structure into a global exception handling structure, wherein the global exception handling structure comprises a plurality of classes for handling the business exception.
Furthermore, the logic instructions in the memory 1030 can be implemented in software functional units and stored in a computer readable storage medium when the logic instructions are sold or used as independent products. Based on such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and the like.
In another aspect, an embodiment of the present invention further provides a computer program product, where the computer program product includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions, when the program instructions are executed by a computer, the computer is capable of executing the spring architecture-based exception handling method provided by the above-mentioned method embodiments, where the method includes: acquiring an original exception handling structure; and modifying the original exception handling structure into a global exception handling structure, wherein the global exception handling structure comprises a plurality of classes for handling the business exception.
In yet another aspect, an embodiment of the present invention further provides a non-transitory computer-readable storage medium, on which a computer program is stored, where the computer program is implemented by a processor to perform the spring architecture-based exception handling method provided in the foregoing embodiments, where the method includes: acquiring an original exception handling structure; and modifying the original exception handling structure into a global exception handling structure, wherein the global exception handling structure comprises a plurality of classes for handling the business exception.
The above-described embodiments of the apparatus are merely illustrative, and the units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of the present embodiment. One of ordinary skill in the art can understand and implement it without inventive effort.
Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware. With this understanding in mind, the above-described technical solutions may be embodied in the form of a software product, which can be stored in a computer-readable storage medium such as ROM/RAM, magnetic disk, optical disk, etc., and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the methods described in the embodiments or some parts of the embodiments.
Finally, it should be noted that: the above examples are only intended to illustrate the technical solution of the present invention, but not to limit it; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.
Claims (10)
1. An exception handling method based on spring architecture is characterized by comprising the following steps:
acquiring an original exception handling structure;
and modifying the original exception handling structure into a global exception handling structure, wherein the global exception handling structure comprises a plurality of classes for handling the business exception.
2. The spring architecture-based exception handling method according to claim 1, wherein the modifying the original exception handling structure into a global exception handling structure, the global exception handling structure including a plurality of classes for handling the service exception, specifically comprises:
determining a preset exception tool class in the global exception handling structure;
adding the plurality of classes for processing the business exceptions in the preset exception tool class, wherein the plurality of classes for processing the business exceptions comprise an interface return and exception return general class, an enumeration class and an exception global processing class.
3. The spring architecture-based exception handling method according to claim 2, wherein the interface return and exception return general class is used for returning interface status information and exception status information.
4. The spring architecture-based exception handling method according to claim 2, wherein the enumeration class is used for recording return codes and return information of various exceptions and adding new exception handling information and status codes.
5. The exception handling method based on a spring architecture according to claim 2, wherein the exception global processing class is used for performing class annotation and method annotation, the class annotation acts on the whole spring project, the method annotation acts on a controller level, and the priority of the method annotation is higher than that of the class annotation.
6. The spring architecture-based exception handling method according to claim 3, wherein the interface return and exception return generic class is further configured to be modified according to a preset requirement when an interface is called.
7. An exception handling system based on a spring architecture, comprising:
the acquisition module is used for acquiring an original exception handling structure;
and the processing module is used for reconstructing the original exception handling structure into a global exception handling structure, and the global exception handling structure comprises a plurality of classes for handling business exceptions.
8. The spring architecture-based exception handling system according to claim 7, wherein the processing module is specifically configured to:
the determining submodule is used for determining a preset exception tool class in the global exception handling structure;
and the adding submodule is used for adding the classes for processing the business exceptions in the preset exception tool class, wherein the classes for processing the business exceptions comprise an interface return and exception return general class, an enumeration class and an exception global processing class.
9. An electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the steps of the spring architecture based exception handling method according to any one of claims 1 to 6 when executing the program.
10. A non-transitory computer readable storage medium, on which a computer program is stored, wherein the computer program, when executed by a processor, implements the steps of the spring architecture based exception handling method according to any one of claims 1 to 6.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010871621.2A CN112083952A (en) | 2020-08-26 | 2020-08-26 | Spring architecture-based exception handling method and system |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010871621.2A CN112083952A (en) | 2020-08-26 | 2020-08-26 | Spring architecture-based exception handling method and system |
Publications (1)
Publication Number | Publication Date |
---|---|
CN112083952A true CN112083952A (en) | 2020-12-15 |
Family
ID=73728979
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202010871621.2A Pending CN112083952A (en) | 2020-08-26 | 2020-08-26 | Spring architecture-based exception handling method and system |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN112083952A (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN114579194A (en) * | 2022-03-08 | 2022-06-03 | 杭州每刻科技有限公司 | Spring remote call-based exception handling method and system |
Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN109522222A (en) * | 2018-10-26 | 2019-03-26 | 深圳点猫科技有限公司 | It is a kind of for educating the exception catching method and device of cloud platform |
CN110069297A (en) * | 2019-03-28 | 2019-07-30 | 平安科技(深圳)有限公司 | Abnormality eliminating method, device, computer equipment and storage medium based on Spring MVC |
-
2020
- 2020-08-26 CN CN202010871621.2A patent/CN112083952A/en active Pending
Patent Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN109522222A (en) * | 2018-10-26 | 2019-03-26 | 深圳点猫科技有限公司 | It is a kind of for educating the exception catching method and device of cloud platform |
CN110069297A (en) * | 2019-03-28 | 2019-07-30 | 平安科技(深圳)有限公司 | Abnormality eliminating method, device, computer equipment and storage medium based on Spring MVC |
Non-Patent Citations (1)
Title |
---|
陈学明: "《Spring+Spring MVC+MyBatis整合开发实战》", 北京:机械工业出版社, pages: 192 - 195 * |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN114579194A (en) * | 2022-03-08 | 2022-06-03 | 杭州每刻科技有限公司 | Spring remote call-based exception handling method and system |
CN114579194B (en) * | 2022-03-08 | 2024-04-05 | 杭州每刻科技有限公司 | Exception handling method and system based on Spring remote call |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN108958714B (en) | Business component type development method and device, computer equipment and storage medium | |
CN108804215B (en) | Task processing method and device and electronic equipment | |
CN106557470B (en) | Data extraction method and device | |
CN110673924B (en) | Multi-architecture container cloud mirror image selection method, device, equipment and storage medium | |
CN110162344B (en) | Isolation current limiting method and device, computer equipment and readable storage medium | |
CN111931172A (en) | Financial system business process abnormity early warning method and device | |
CN111143446A (en) | Data structure conversion processing method and device of data object and electronic equipment | |
CN107239325B (en) | Document data processing method and device | |
CN111666088A (en) | Pod replacement method and device, electronic equipment and computer-readable storage medium | |
CN111338716A (en) | Data processing method and device based on rule engine and terminal equipment | |
CN112083952A (en) | Spring architecture-based exception handling method and system | |
CN112540837B (en) | Service processing component calling method, system, electronic equipment and storage medium | |
CN108595178B (en) | Hook-based data acquisition method, device and equipment | |
CN113886590A (en) | Data summarizing method and device, computer equipment and storage medium | |
CN110908644A (en) | Configuration method and device of state node, computer equipment and storage medium | |
CN116226078A (en) | Data migration method, device, equipment and storage medium | |
CN111324645A (en) | Data processing method and device for block chain | |
CN115185576A (en) | Process customization method and system based on process application system | |
CN110221952B (en) | Service data processing method and device and service data processing system | |
CN114416305A (en) | Robot engine implementation method and system and electronic equipment | |
CN113297149A (en) | Method and device for monitoring data processing request | |
CN112631920A (en) | Test method, test device, electronic equipment and readable storage medium | |
CN114546793A (en) | Log generation method and device and computer readable storage medium | |
CN112486481A (en) | Multi-module layered architecture implementation method, device, electronic equipment and medium | |
CN111538491A (en) | Data event processing method, device, equipment and storage medium |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |