• 【Java Web】切面@Aspect


    【Java Web】@Aspect

    注意:服务调用的方法才会走切面,this调用的不会走切面,原因是服务对象是spring注入的代理对象和this不是同一东西。

    切点

    @Pointcut(value=“execution(* ycx.UserService.*(…))”)

    表达式

    execution():用于匹配方法执行的连接点
    args(): 用于匹配当前执行的方法传入的参数为指定类型的执行方法
    this(): 用于匹配当前AOP代理对象类型的执行方法;注意是AOP代理对象的类型匹配,这样就可能包括引入接口也类型匹配;
    target(): 用于匹配当前目标对象类型的执行方法;注意是目标对象的类型匹配,这样就不包括引入接口也类型匹配;
    within(): 用于匹配指定类型内的方法执行;
    @args():于匹配当前执行的方法传入的参数持有指定注解的执行;
    @target():用于匹配当前目标对象类型的执行方法,其中目标对象持有指定的注解;
    @within():用于匹配所以持有指定注解类型内的方法;
    @annotation:用于匹配当前执行方法持有指定注解的方法;

    execution格式:带问号是可选项

    execution(
      modifier-pattern? 修饰符 public private protected
      ret-type-pattern  返回值
      declaring-type-pattern? 类路径
      name-pattern(param-pattern) 方法名 (..)代表所有参数,()代表一个参数,(,String)代表第一个参数为任何值,第二个为String类型
      throws-pattern? 异常类型
    )
    execution(public * *(..)) 定义任意公共方法的执行
    execution(* set*(..)) 定义任何一个以 set 开始的方法的执行
    execution(* ycx.service.AccountService.*(..)) 定义AccountService 接口的任意方法的执行
    execution(* ycx.service.*.*(..)) 定义在service包里的任意方法的执行
    execution(* ycx.service ..*.*(..)) 定义在service包和所有子包里的任意类的任意方法的执行
    execution(* com.test.spring.aop.pointcutexp…JoinPointObjP2.*(…)) 定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行

    * 匹配任何数量字符;
    .. 匹配任何数量字符的重复,如在类型模式中匹配任何数量子包;而在方法参数模式中匹配任何数量参数。
    + 匹配指定类型的子类型;仅能作为后缀放在类型模式后边。

    计算方法运行时间的例子

    启动注解

    package ycx.common.aspect;
    
    import org.springframework.context.annotation.Import;
    
    import java.lang.annotation.*;
    
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Import(RunTimeAspect.class)
    public @interface EnableRunTime {
    }

    方法注解

    package ycx.common.aspect;
    
    import java.lang.annotation.*;
    
    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    public @interface RunTime {
    }

    切面

    package ycx.common.aspect;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.Signature;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    
    @Component // 使用 @Component 或者在启动类追加 @EnableRunTime
    @Aspect
    public class RunTimeAspect {
        @PostConstruct
        public void init() {
        }
        /**
         * 定义切点 @Pointcut("execution(* *.findPage(..))")
         */
        @Pointcut("@annotation(ycx.common.aspect.EnableRunTime)")
        public void pointcut() {
        }
        @Around(value = "pointcut()")
        public Object interceptor(ProceedingJoinPoint joinPoint) throws Throwable {
            Signature signature = joinPoint.getSignature();
            String name = signature.getName();
            System.out.println(name);
            long startTime = System.nanoTime();
            Object object = joinPoint.proceed(joinPoint.getArgs());
            long runTime = System.nanoTime() - startTime;
            String ms = (runTime / 1000000) + "毫秒";
            System.out.println("耗时:" + ms);
            return object;
        }
    }

    https://blog.csdn.net/justlpf/article/details/103400452

  • 相关阅读:
    Java框架-mybatis02基本的crud操作
    Java框架-mybatis01查询单个数据
    MAC常用命令
    性能测试工具Jmeter13-Jmeter跨线程组调用token
    性能测试工具Jmeter12-Jmeter连接配置带跳板机(SSH)的mysql服务器
    Java基础29-子父类中的成员变量
    Java基础28-继承
    Java基础27-单例设计模式
    启动项目时报spawn cmd ENOENT
    npm安装教程
  • 原文地址:https://www.cnblogs.com/yangchongxing/p/16595564.html
Copyright © 2020-2023  润新知