Anomalous Transformation of Spring AOP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.