2508-AOP- springboot中使用-使用注解方式

Springboot中使用aop,与SSM中使用AOP,整体配置与编写方式都是类似的。但是Springboot简化了很多xml配置,切点的表达式可以直接进行javaconfig。

记录一些示例
springboot示例:
版本1.5.9.RELEASE
pom文件中添加aop的依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

自定义注解:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Target指定注解的目标为方法级
 * Retention指定注解可以在运行时被获取(利用反射)
 *
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CountAopAnnotation {
}

aop类:


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * aop
 * Created by hxy 2018/5/5.
 */

@Component
@Aspect
@Order(10) //构建执行顺序
public class CountAopHelper {

    /*
    * 定义一个切入点
    */
    @Pointcut("@annotation(com.company.project.core.annotation.CountAopAnnotation)")
    public void myInfoAnnotation() {
    }

    // 用@Pointcut来注解一个切入方法
    @Pointcut("execution(* com.company.project.web.*.*(..))")
    public void excudeController() {
    }

    @Before("myInfoAnnotation()")
    public void deBefore(JoinPoint joinPoint) throws Throwable {
        System.out.println("deBefore");

        // 通过RequestContextHolder获取HttpServletRequest 可以获取请求头相关的内容
        // HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // StringBuffer requestURL = request.getRequestURL();
    }

    @After("myInfoAnnotation()")
    public void doAfter(JoinPoint joinPoint) throws Throwable {
        System.out.println("doAfter");

        // 通过RequestContextHolder获取HttpServletRequest 可以获取请求头相关的内容
        // HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // StringBuffer requestURL = request.getRequestURL();
    }

    /**
     * Around(环绕通知)是在Before(前置通知)前面执行
     * &&@annotation(annotation) 这个是对方法参数的形参进行注入
     * <p>
     * value可以是多种 1纯注解形式 myInfoAnnotation()  2 混合 myInfoAnnotation()&&@annotation(annotation)&& excudeController()
     * 使用场景  1大面积使用aop  使用Pointcut来写匹配表达式  2精准定位 使用注解形式
     */
    @Around(value = "myInfoAnnotation()")
    public Object doAround(ProceedingJoinPoint thisJoinPoint) throws Throwable {
        System.out.println("doAround");

        // 获取切点的参数
        Object[] args = thisJoinPoint.getArgs();

        //环绕通知必须执行,否则不进入注解的方法
        return thisJoinPoint.proceed();
    }


}

使用:
在接口上加上注解

   @CountAopAnnotation
    @GetMapping("/testAnno")
    public Result testAnno() {

        System.out.println("here is api testAnno");

        return ResultGenerator.genSuccessResult("dsds");
    }

执行结果是:
先执行了环绕通知,再执行前置通知,再执行被aop代理的方法,再执行后置通知;

doAround
deBefore
here is api testAnno
doAfter

猜你喜欢

转载自blog.csdn.net/zzxx1994617/article/details/80505558