自定义注解及切面

1、自定义注解四基本:

只要理解和记住jdk内置的四个注解即可 (@Target,@Retention,@Documented,@Inherited)

  • (1)@Target:注解的使用范围 (ElementType)
    packages、types(类、接口、枚举、注解类)、类成员(方法、构造方法、成员变量、枚举值)
  • (2)@Retention:注解的保留时间范围 (RetentionPolicy)
    SOURCE:源文件保留(如@Override保留在源文件,编译后注解消失)
    CLASS:编译时保留(如lombok生成get/set)
    RUNTIME:运行时保留(如切面记录日志,或验证参数信息等)
  • (3)@Documented:保留注解信息
  • (4)@Inherited:子类注解自动继承该注解
    例子:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TestAnnotation {
    public String param() default "";
}

2、切面三要素:

  • (1)连接点 JoinPoint ,spring中允许通知的地方,也就是允许切入的地方,如方法执行前、执行后、异常抛出后,在连接点中可以进行切入
  • (2)切入点 Pointcut,实际选择的连接点进行通知,比如有100个连接点,你选了5个进行通知,这5个就是切入点
  • (3)切面 Aspect 就是切入点和通知的结合,也就是在哪切入(哪个类),通知的类型是在(类或方法)之前还是之后,befor,after,around

Spring AOP支持使用以下AspectJ切点标识符(PCD),用于切点表达式:
execution: 用于匹配方法执行连接点。 这是使用Spring AOP时使用的主要切点标识符。 可以匹配到方法级别 ,细粒度
within: 只能匹配类这级,只能指定类, 类下面的某个具体的方法无法指定, 粗粒度
this: 匹配实现了某个接口:this(com.xyz.service.AccountService)
target: 限制匹配到连接点(使用Spring AOP时方法的执行),其中目标对象(正在代理的应用程序对象)是给定类型的实例。
args: 限制与连接点的匹配(使用Spring AOP时方法的执行),其中变量是给定类型的实例。 AOP) where the arguments are instances of the given types.
@target: 限制与连接点的匹配(使用Spring AOP时方法的执行),其中执行对象的类具有给定类型的注解。
@args: 限制匹配连接点(使用Spring AOP时方法的执行),其中传递的实际参数的运行时类型具有给定类型的注解。
@within: 限制与具有给定注解的类型中的连接点匹配(使用Spring AOP时在具有给定注解的类型中声明的方法的执行)。
@annotation:限制匹配连接点(在Spring AOP中执行的方法具有给定的注解)

3、自定义注解

//  TestAnnotation.java
package com.xxx;

import java.lang.annotation.*;

// 作用在方法上
@Target(ElementType.METHOD)
// 运行时生效
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TestAnnotation {
    public String param() default "";
}

4、切面

  • (1)自定义注解
package com.xxx;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;


// 定义切面
@Aspect
// 交给spring管理
@Component
// 该切面的优先顺序,越小优先级越高
@Order(value = 1)
public class TestAspect {

    @Autowired
    private HttpServletRequest request;

    // 切点方式
    @Pointcut("@annotation(com.puyitou.fundmanager.hedge.system.aspect.TestAnnotation)")
    private void testAspectPoincut(){}

    @Before("testAspectPoincut()")
    public void before(JoinPoint joinPoint){
        System.out.println("执行了before。。。testAspectPoincut:" );
    }
    // 连接点方式
    /*
    @Before("within(com.puyitou.fundmanager.hedge.controller.FeeSettleController)")
    public void before(JoinPoint joinPoint){
        System.out.println("执行了before。。。testAnnotation:" );
    }

    // 方法正常退出时执行  通知类型是AfterReturning,切入点是在within范围内有operationLog注解的方法
    @AfterReturning("within(com.puyitou..*) && @annotation(testAnnotation)")
    public void afterReturning(JoinPoint joinPoint,TestAnnotation testAnnotation){
        System.out.println("执行了afterReturning。。。testAnnotation:" + testAnnotation.param());
    }

    @AfterThrowing("within(com.puyitou..*) && @annotation(testAnnotation)")
    public void afterThrowing(JoinPoint joinPoint,TestAnnotation testAnnotation){
        System.out.println("执行了afterThrowing... testAnnotation:" + testAnnotation.param());
    }
    */
}

  • (2)系统原本的注解


    /**
     * 定义一个切点:所有被 GetMapping 注解修饰的方法都会被织入 advice
     */
    @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
    private void logAdvicePointcut(){}

    /**
     * 表示 logAdvice 将在目标方法执行前执行
     */
    @Before("logAdvicePointcut()")
    public void logAdvice(){
        //这里只是一个示例,你可以写任何处理逻辑
        System.out.println("切面 @Before 执行了");
    }
    
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容