Spring AOP surround notification logging app's logs

Source: Internet
Author: User
Tags throwable

The framework used is spring mvc+spring

Recently want to take advantage of spring AOP surround notification to handle the Web log problem, in general, if the controller layer to make a cut, it is difficult to monitor the actual operation, in the service layer to cut into the service layer can only monitor the situation, Logging is logged by capturing the exception that is thrown by the service, which is sufficient for the current application to be logged as follows:

Code:

@Component @aspect Public classExceptionlog {/*** 61 * Surround notifications need to carry Proceedingjoinpoint type parameters * 62 * Surround notification is similar to the whole process of dynamic agents: Parameters of the Proceedingjoinpoint type can decide whether to execute the target method     。 * 63 * And the surround notification must have a return value, which is the return value of the target method **/@Around ("Execution (* *com.test.service). *.*(..))")     PublicObject Aroundmethod (Proceedingjoinpoint PJD)throwsthrowable {Object result=NULL; String MethodName= "Class:" + Pjd.gettarget (). GetClass (). GetName () + "method:" + pjd.getsignature (). GetName ();//Get method NameHttpServletRequest Request= ((servletrequestattributes) requestcontextholder.getrequestattributes ()). Getrequest ();//gets the requested URL//Execute target Method        Try {            //front-facing notificationsresult = Pjd.proceed ();//Back to Notifications}Catch(Throwable e) {//Exception Notification//System.out.println ("the method" + MethodName + "occurs expection:" + e); throw e; /Let Spring handle exceptions}finally {           //Record Log        }        returnResult//The result must be returned, otherwise the controller cannot get the result returned by the service (normally)    }}

Spring Configuration:

<aop:aspectj-autoproxy/>

Web. XML configuration (primarily for getting httpservletrequest)

< Listener >        < Listener-class >             org.springframework.web.context.request.RequestContextListener         </  Listener-class>    </listener>

Spring AOP surround notification logging app's logs

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.