AOP aspect implementation

package com.sunshijia.angry.manager.aop;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.sunshijia.angry.manager.common.Message;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/ **
* @ClassName: ControllerLogAspect
* @Description: log recording layer control section calls
* no abnormality: @Around (the part before () proceed) → @Before → method is performed (part after proceed ()) → @Around @After @AfterReturning → →
* there is an exception: @Around (proceed (the part before)) → @Before → → throw an exception @After → @AfterThrowing ING
* @author
* @date 9:26 2019 Nian 5 morning of May 8: 44 is
* @version 1.0
* /
@Aspect
@Component
public class ControllerLogAspect {

  Logger LoggerFactory.getLogger = log (ControllerLogAspect.class);

  // This is the entry point for service description entry point packet
  @Pointcut ( "Execution (public com.sunshijia.angry.controller.LoginController.login * (..))")
  public void ctrlLog () {
  }

  / **
  * @Description: Normal termination method
  * @param joinPoint entry point
  * @param msg the end of the return value (Uniform Type)
  * @author 
  * @date 2019 Nian 5 morning of May 8 9:49:01
  * /
  @AfterReturning ( = the pointcut "ctrlLog ()", returning = "MSG")
  public void logTheEnd (the JoinPoint Joinpoint, the Message <Object> MSG) {
    // get the current logged on user
    String this.getUserName the userName = ();
    // Get request method name
    String . methodName = joinPoint.getSignature () getName ();
    // Get request parameter
    List <Object> = args Arrays.asList (joinPoint.getArgs ());
    //logger.info ( "currently logged on user:" + userName + " , the request is a method: "+ methodName +", for the parameters: "+ args +", status code: "+ msg.getCode () +" , message: "+ msg.getMsg ());
    log.info ( "currently logged: {}, is a method request: {}, the parameters are: {}, status code: {}, message: {}", userName, methodName , args, msg.getCode (), msg.getMsg ());
  }

  / **
  * @Description: abnormal termination method
  * @param joinPoint entry point
  * @param ex anomalies
  * @author 
  * @date 2019 Nian 5 morning of May 8 9:49:32
  * /
  @AfterThrowing (pointcut = "ctrlLog ( ) ", the throwing =" EX ")
  public void logError (the JoinPoint Joinpoint, Exception EX) {
    // get the current logged on user
    String this.getUserName the userName = ();
    // Get request method name
    String methodName = joinPoint.getSignature (). getName ();
    // Get request parameter
    List <Object> = args Arrays.asList (joinPoint.getArgs ());
    //logger.error ( "currently logged on user:" + userName + ", the request for the method:" + methodName + "parameters are:" + + args "abnormality:" EX +);
    log.error ( "currently logged: {}, is a method request: {}, the parameters are: {} The exception is: {} "userName,methodName,args,ex.getMessage());
  }

  / **
  * @Description: Get the current logged-on user
  * @return Username
  * @author 
  * @date 2019 Nian 5 Yue 8 Ri 9:57:14 AM
  * /
  Private String getUserName () {
    // Gets Request
    RequestAttributes RequestAttributes = RequestContextHolder.getRequestAttributes ();
    the HttpServletRequest Request = ((ServletRequestAttributes) RequestAttributes) .getRequest ();
    // get token
    String token = request.getHeader ( "token");
    // get the current logged-on user based on token
    String userName = token;
    return the userName;
  }
}

Guess you like

Origin www.cnblogs.com/sunshijia1993/p/11318959.html