Exception handling in spring is using the ASPECTJ
@Aspect @component/** * * @author* * * @createData July 13, 2017 8:36:23 * @ Description: There are some empty values ... */ Public classAjaxentityhandler {//@Pointcut ("@annotation (Org.zkdg.utils.annotations.AfterHandler)")@Pointcut ("@annotation (org.zkdg.utils.spring.annotations.NotNullVariable)") //@Pointcut ("Execution (* org.dcexam.*.service.*.* (..))") Public voidBeforecall () {//before the service method call, the detection parameter is limited to the first parameter only, not to a null value } /*** Called when service exception occurs*/@Pointcut ("Execution (* org.dcexam.*.service.*.* (..))") Public voidAfterthrowex () {System.out.println ("************\n\n\n\n\n\n\n\n\n\n\n\n*******"); } @Around (Value= "Beforecall ()") PublicAjaxentity Dobefore (Proceedingjoinpoint point)throwsThrowable {//TODO auto-generated Method Stubobject[] args =Point.getargs (); if(args = =NULL|| Args[0] = =NULL) { return NewAjaxentity ("Warning", "No data selected ... "); } if(Args[0]instanceofString) {String str= (String) args[0]; if(Str.equalsignorecase ("")) return NewAjaxentity ("Warning", "No data selected ... "); } ajaxentity Ajax=(ajaxentity) point.proceed (args); returnAjax = =NULL? Ajaxentity.error ("Operation failed"): Ajax; } /** * * @paramJoinpoint * Connection point *@paramEX * exception *@returnAjaxentity Exception Information*/@AfterThrowing (Value= "Afterthrowex ()", throwing = "Ex") Public voidDoafterthrowex (Joinpoint joinpoint, Exception ex) {ajaxentity ajax=Newajaxentity (); if(Ex.getcause ()instanceofSQLException) { //Database Operation ExceptionAjax = Ajaxentity.error ("Exception occurred while manipulating database"); } }}
Configuration in Spring.xml
<!--annotated AOP, support annotations--
<aop:aspectj-autoproxy/>
Transaction Pointcut Configuration
<!--Configure the class that participates in the transaction--
<aop:config expose-proxy= "true" proxy-target-class= "true" >
<aop:pointcut id= "Txpointcut"
expression= "Execution (* org.dcexam.*.service.*.* (..))"/>
<aop:advisor pointcut-ref= "Txpointcut" advice-ref= "Txadvice"/>
</aop:config>
Note expose-proxy= "true" proxy-target-class= "true" is ASPECTJ proxy
In Springmvc, because spring and SPRINGMVC are different containers. Try not to use the ASPECJ proxy to handle exceptions using spring MVC's own handlerexceptionresolver
/** * * @author* @createData July 13, 2017 12:27:19 * @ Description: Springmvc exception Handling*///annotations, scanned by spring to@Component Public classExinterceptorImplementsHandlerexceptionresolver {@Override PublicModelandview resolveexception (httpservletrequest request, httpservletresponse response, Object handler, Ex Ception ex) {//TODO auto-generated Method Stub if(Ex! =NULL) { Try{throwable cause=Ex.getcause (); if(CauseinstanceofSQLException) {Response.setstatus (200); //JSON outputresponse.getwriter (). Write (Jsonutils.objecttojson (Ajaxentity.error ("Database operation failed!<br>" +cause.getmessage ()))); } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //returns an empty Modelandview () that must be returned, otherwise the exception handling configuration is invalid: return NewModelandview (); }}
Have to say, spring is really too strong!!!!
Exception handling in spring and SPIRNGMVC