Application of ASPECTJ in spring--monitoring of interface

Source: Internet
Author: User

First, background:

1. Since the company recently split the project before the plan to change the project to a front-end separation mode, so our back side is doing interface development

2. Recently also docking their interfaces with other companies, in the process of docking to find that their interface calls are log storage, so the whim of the spring AOP to do this thing to play (in fact, as if the interceptor can also be implemented).

Second, the process record

1. Add an XSD file to the spring configuration file

Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xmlns:task= "Http://www.springframework.org/schema/task" xmlns:p= "http://www.springframework.org/schema/p"

xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"

xmlns:context= "Http://www.springframework.org/schema/context"

Xsi:schemalocation= "Http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

Http://www.springframework.org/schema/task

Http://www.springframework.org/schema/task/spring-task-3.2.xsd

Http://www.springframework.org/schema/context

Http://www.springframework.org/schema/context/spring-context-3.2.xsd

Http://www.springframework.org/schema/mvc

Http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">

2. Modify the configuration file to add:

3. Writing the Aspect class

Package com.dreyer.ssm.aspect;

Import java.util.Enumeration;

Import Javax.servlet.http.HttpServletRequest;

Import Org.apache.log4j.Logger;

Import Org.aspectj.lang.ProceedingJoinPoint;

Import Org.aspectj.lang.annotation.Around;

Import Org.aspectj.lang.annotation.Aspect;

Import org.springframework.stereotype.Component;

Import Org.springframework.web.context.request.RequestContextHolder;

Import org.springframework.web.context.request.ServletRequestAttributes;

/**

* @Description monitoring of API calls

* @author Dreyer

* @date October 25, 2015 9:04:45

* @version 1.0

*/

@Aspect

@Component

public class Apimonitoraspect {

Private Logger Logger = Logger.getlogger (Apimonitoraspect.class);

@Around (value = "Execution" (public * com.dreyer.ssm.controller). *.*(..))")

Public Object Monitor (Proceedingjoinpoint proceedingjoinpoint) throws Throwable {

HttpServletRequest request = ((servletrequestattributes) requestcontextholder.getrequestattributes ()). GetRequest () ;

The requested server

String serverName = Request.getservername ();

The requested port

int serverport = Request.getserverport ();

Context Path

String ContextPath = Request.getcontextpath ();

Path of the servlet

String Servletpath = Request.getservletpath ();

Logger.info ("Request path:" + request.getscheme () + ":" + ServerName + ":" + serverport + contextpath + servletpath);

Enumeration parameters = Request.getparameternames ();

String element = "";

while (Parameters.hasmoreelements ()) {

element = Parameters.nextelement ();

Logger.info ("param key:" + element + "= =" + request.getparameter (Element));

}

Long start = System.currenttimemillis ();

try {

Return Proceedingjoinpoint.proceed (Proceedingjoinpoint.getargs ());

} catch (Throwable e) {

Throw e;

} finally {

Logger.info ("This request is time consuming:" + (System.currenttimemillis ()-start) + "MS");

}

}

}

4. Testing

The call method for any of the methods in the controller outputs the log (the last line is ignored)

[Com.dreyer.ssm.aspect.ApiMonitorAspect] Request path: http:localhost:8080/ssm/user/login.do

[Com.dreyer.ssm.aspect.apimonitoraspect]param Key:phone = = 13122222222

[Com.dreyer.ssm.aspect.apimonitoraspect]param Key:password = = 123456

[Com.dreyer.ssm.aspect.ApiMonitorAspect] This request is time consuming: 6 ms

[Com.dreyer.ssm.exception.handler.GlobalExceptionHandler] System error, error code: 1002, error message: User does not exist

The application of Spring AOP can be divided into two kinds, one is based on ASPECTJ, need to be configured in configuration file, Pointcut expression is written in class, the initialization of aspect class needs to be given to spring management.

One is schema-based AOP support, which needs to be written in the configuration file by writing the information of the Aspect class

...

...

As for the pros and cons of both, refer to the documentation: http://shouce.jb51.net/spring/

Application of ASPECTJ in spring--monitoring of interface

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.