Using the AOP exception Mount feature, you can uniformly handle the exceptions thrown by the method, and reduce the number of duplicate code, implemented as follows:
1. Realize Throwadvice
1 Public classExceptionhandlerImplementsThrowsadvice {2 3 Private StaticLogger Logger = Loggerfactory.getlogger (Exceptionhandler.class);4 5 Public voidAfterthrowing (Exception e)throwsThrowable {6Logger.debug ("Exception's Coming!" ");7 }8}
2. Configure in the Application.xml file
1 <BeanID= "Exceptionhandler"class= "Com.lz.cgw.api.service.exception.ExceptionHandler" />2 3 <Aop:config>4 <Aop:aspectref= "Exceptionhandler">5 <Aop:pointcutID= "Exceptionservice"expression= "Execution (* com.lz.cgw.api.service.apiuserserviceimpl.* (..))" />6 <aop:after-throwingPointcut-ref= "Exceptionservice"Method= "Afterthrowing"throwing= "E" />7 </Aop:aspect>8 </Aop:config>
Be careful not to miss the throwing configuration, and the parameter name to go to the advice in the first place, or the binding will error.
Spring AOP implements exception blocking