Exception handling in spring and SPIRNGMVC

Source: Internet
Author: User

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

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.