ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot aop使用方式

2022-02-07 17:35:23  阅读:205  来源: 互联网

标签:匹配 springboot 方式 DatabaseResourcedefine value Pointcut aop com annotation


  1. 定义切面类

  2. 切面类进行增强的两种写法

  3. 切点匹配注解和规则

 

切面类定义

  1. 引入相应的aop包

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

     

  2. 定义切面类(类名自定义)

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

    @Aspect
    @Component
    public class DatabaseResourceToggle {


    }

 

切入点定义注解说明

@Pointcut :用来定义一个切面(切入点),即所关注的某件事情的入口。切入点决定了连接点关注的内容,使得我们可以控制通知什么时候执行。

切入点表达式说明

先说明两个通配符:*、..

  1. *:主要用于匹配单个单词,或者是以某个词为前缀或后缀的单词

  2. ..:表示0个或多个项。主要用于类全路径或者参数,用于类全路径中表示匹配当前包及其子包,如果用于参数中,则表示匹配0个或多个参

1、匹配使用自定义注解的方法:
  @Pointcut("@annotation()")
  @Pointcut("@within()")
  @Pointcut("@args()")
  @Pointcut("@target()")
2、匹配对象下的方法:
  @Pointcut("this()")
  @Pointcut("bean()")
  @Pointcut("target()")
3、匹配方法和参数:
  @Pointcut("within()")
  @Pointcut("args()")
  @Pointcut("execution()")
@Pointcut("@annotation(com.gzt.annotation.DatabaseResourcedefine)"):匹配方法上有DatabaseResourcedefine注解的方法
@Pointcut("@within(com.gzt.annotation.DatabaseResourcedefine)"):匹配有DatabaseResourcedefine注解标记的类里面的方法(最常用)
@Pointcut("@args(com.gzt.annotation.DatabaseResourcedefine)"):匹配传入的参数有DatabaseResourcedefine注解标记的方法
@Pointcut("@target(com.gzt.annotation.DatabaseResourcedefine)"):匹配有DatabaseResourcedefine注解标记的类及其子类里面的方法
@Pointcut("this(com.gzt.annotation.DatabaseResourcedefine)"):匹配AOP对象的目标对象为指定类型的方法
@Pointcut("target(com.gzt.annotation.DatabaseResourcedefine)"):匹配实现DatabaseResourcedefine接口的目标对象里面的方法
@Pointcut("bean(DatabaseResourcedefine)"):匹配指定的bean
@Pointcut("within(com.gzt.annotation.DatabaseResourcedefine)"):匹配指定包下的方法
@Pointcut("args(Long,..)"):匹配任何以Long参数开头的方法(一般搭配其他匹配条件使用)
@Pointcut("execution(* com.gzt.service.implTestImpl.*(..))"):匹配某个方法(最常用)

@within、execution两个一般最常用,使用@within+自定义注解,可以很好的对某一个类下面的方法进行增强,execution用来匹配单个的

增强方式

@before(前置通知): 在方法开始执行前执行

@after(后置通知): 在方法执行后执行

@afterReturning(返回后通知): 在方法返回后执行

@afterThrowing(异常通知): 在抛出异常时执行

@around(环绕通知): 在方法执行前和执行后都会执行

例子:

public class DatabaseResourceToggle {
  final Logger logger = LoggerFactory.getLogger(getClass());

  @Pointcut("execution(* com.gzt.service.implTestImpl.*(..))")
  //@Pointcut("@within(com.gzt.annotation.DatabaseResourcedefine)")
  public void pointCut() {}


  @Before(value = "pointCut()")
  public void before1(JoinPoint joinPoint) {
     Object[] args = joinPoint.getArgs();
     for (Object arg : args) {
        System.out.println(arg.toString());
    }
     Object target = joinPoint.getTarget();
     System.out.println(target.getClass().getTypeName());
     Object target1 = joinPoint.getTarget();
     DatabaseResourcedefine annotation = target1.getClass().getAnnotation(DatabaseResourcedefine.class);
     String value = annotation.value();
     System.out.println("value:"+value);
     DatabaseAdapt.flag = value;
     logger.info(value);
  }
  //使用前置通知或者是环绕通知
  //@Before("@annotation(databaseResourcedefine) || @within(databaseResourcedefine)")
  //public void before(JoinPoint joinPoint, DatabaseResourcedefine databaseResourcedefine) {
  // String value = databaseResourcedefine.value();
  // DatabaseAdapt.flag = value;
  // logger.info(value);
  //}
}

 

标签:匹配,springboot,方式,DatabaseResourcedefine,value,Pointcut,aop,com,annotation
来源: https://www.cnblogs.com/zhuojiuyibei/p/15868579.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有