spring aop1

aspect 方面,切面, 共同调用的接口

 

pointcut 切点,具体触发的点 (切点表达式的写法)

1、springboot 最简aop

最简aop:

 

  1.  <dependency>  

  2.             <groupId>org.springframework.boot</groupId>  

  3.             <artifactId>spring-boot-starter-aop</artifactId>  

  4.         </dependency>  

 

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ControllerLog {

    @Before("execution(public * com.example.demo1st.*.*(..))")
    public void startTime() {
        System.out.println("计时开始~~~~~");
    }
}

2、加入切点里的参数

 

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Aspect
@Component
public class ControllerLog1 {

    @Pointcut("execution(public * com.example.demo1st.*.*(..))")
    public void webLog(){}

    //@Before("execution(public * com.example.demo1st.*.*(..))")
    @Before("webLog()")
    public void startTimee(JoinPoint joinPoint) throws Throwable {

        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        System.out.println("URL : " + request.getRequestURL().toString());
        System.out.println("HTTP_METHOD : " + request.getMethod());
        System.out.println("IP : " + request.getRemoteAddr());
        System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        System.out.println("ARGS : " + Arrays.toString(joinPoint.getArgs()));

        System.out.println("计时开始~~~~~");
    }
}

 

 3、后面的及自定义注解

https://www.cnblogs.com/bigben0123/p/7779357.html


posted @ 2018-11-26 20:00  cnchengv  阅读(77)  评论(0)    收藏  举报