SPRING-AOP's Unusual conversion primer
Recently the project encountered a problem, that is, the business layer to the presentation layer needs to be converted to a uniform exception class, and throw an exception, but because of the business layer of the exception class, so the business exception conversion code is flooded with exception conversion code, in the program Ape can write code on the principle of writing code, decided to use spring AOP to do a slicing, business exception class conversion.
Most original code
The most primitive code, called V1.0.
@Override public gnareavo Selectbyid(gnareacondition condition) throws Callerexception{Try{//Business processing if(Stringutils.isempty (Condition.getareacode ()))Throw NewBusinessexception ("10001","zone encoding cannot be empty"); Gson Gson =NewGson ();//Processing results returnGson.fromjson (Gson.tojson (Ignareabusinessservice.selectbyid (Condition.getareacode ())), GnAreaVo.class); }Catch(Businessexception ex) {// Throw NewCallerexception ("100001", Ex.geterrormessage ()); }Catch(SystemException ex) {// Throw NewCallerexception ("100001", Ex.getmessage ()); }Catch(Exception ex) {// Throw NewCallerexception ("10001","System Exception"); } }
Upgrade version
Upgrade version, referred to as V1.1, extracts a public class to handle
@Override public gnareavo Selectbyid(gnareacondition condition) throws Callerexception{Try{//Business processing if(Stringutils.isempty (Condition.getareacode ()))Throw NewBusinessexception ("10001","zone encoding cannot be empty"); Gson Gson =NewGson ();//Processing results returnGson.fromjson (Gson.tojson (Ignareabusinessservice.selectbyid (Condition.getareacode ())), GnAreaVo.class); }Catch(Businessexception ex) {// ThrowDubboexceptassembler.assemble (ex); }Catch(SystemException ex) {// ThrowDubboexceptassembler.assemble (ex); }Catch(Exception ex) {// ThrowDubboexceptassembler.assemble (ex); } }
Final version
The code is much simpler, and can support more of the exception class conversion, reduce the business program's useless code, below to see how to implement.
First write an AOP
ImportCom.ai.runner.base.exception.BusinessException;ImportCom.ai.runner.base.exception.SystemException;ImportCom.ai.runner.utils.util.DubboExceptAssembler;ImportOrg.apache.logging.log4j.LogManager;ImportOrg.apache.logging.log4j.Logger;ImportOrg.aspectj.lang.JoinPoint; Public class dubboexceptionconvertinterceptor {Private Static FinalLogger Logger = Logmanager.getlogger (Dubboexceptionconvertinterceptor.class);Public void convertexception(joinpoint joinpoint, Exception error) {Logger.error ("The {} method in the Execute {} class has an error because of an error: {}", Joinpoint.gettarget (). GetClass (). GetName (), Joinpoint.getsignature (). GetName ());if(ErrorinstanceofSystemException) {ThrowDubboexceptassembler.assemble ((systemexception) error); }if(ErrorinstanceofBusinessexception) {ThrowDubboexceptassembler.assemble ((businessexception) error); }ThrowDubboexceptassembler.assemble (Error); }}
Spring Configuration:
<bean id= "dubboexceptionconvertor" class=" Dubboexceptionconvertinterceptor "/> <aop:config> <aop:aspect id= "aspectloggging" ref=" Dubboexceptionconvertor "> <aop:pointcut id = "dubboexceptionthrowing" expression = "Execution (* Com.ai.runner.center.common.api.*.impl.*.* (..)) " /> <aop:after-throwing Method="convertexception" throwing="Error" pointcut-ref="dubboexceptionthrowing"/> </aop:aspect> </aop:config>
Business code:
@Override public gnareavo Selectbyid(gnareacondition condition) throws Callerexception{if(Stringutils.isempty (Condition.getareacode ()))Throw NewBusinessexception ("10001","zone encoding cannot be empty"); Gson Gson =NewGson ();returnGson.fromjson (Gson.tojson (Ignareabusinessservice.selectbyid (Condition.getareacode ())), GnAreaVo.class); }
done!
Anomalous Transformation of Spring AOP