springaop中9种切入点表达式的写法

springaop中9种切⼊点表达式的写法  本⽂主要介绍spring aop中9种切⼊点表达式的写法
  execute
  within
  this
  target
  args
  @target
  @within
  @annotation
  @args
  0. ⽰例代码git地址
  1.execute表达式
  拦截任意公共⽅法
  execution(public * *(..))
  拦截以set开头的任意⽅法
  execution(* set*(..))
  拦截类或者接⼝中的⽅法
  execution(* service.AccountService.*(..))
  拦截AccountService(类、接⼝)中定义的所有⽅法
  拦截包中定义的⽅法,不包含⼦包中的⽅法
  execution(* service.*.*(..))
  拦截service包中所有类中任意⽅法,不包含⼦包中的类
  拦截包或者⼦包中定义的⽅法
  execution(* service..*.*(..))
  拦截service包或者⼦包中定义的所有⽅法
  2.within表达式
  拦截包中任意⽅法,不包含⼦包中的⽅法
  service.*)
  拦截service包中任意类的任意⽅法
  拦截包或者⼦包中定义的⽅法
  service..*)
  拦截service包及⼦包中任意类的任意⽅法
  3.this表达式
  代理对象指定的类型会被拦截
  ⽬标对象使⽤aop之后⽣成的代理对象必须是指定的类型才会被拦截,注意是⽬标对象被代理之后⽣成的代理对象和指定的类型匹配才会被拦截
  service.AccountService)
  ⽰例
  this表达式的使⽤,可能不是很好理解,⽤⽰例说明⼀下:
  <?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="www.thd178 /POM/4.0.0" xmlns:xsi="www.suoLaiervip
/2001/XMLSchema-instance"
  xsi:schemaLocation="/POM/4.0.0 www.365soke /xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.4.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.ms</groupId>
  <artifactId>spring-aop-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring-aop-demo</name>
  <description>Demo project for Spring Boot</description> 
  <properties>
  <java.version>1.8</java.version>
  </properties>
  <dependencies>
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-aop</artifactId>
  </dependency>
  <dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  </dependency>
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
  </dependency>
  </dependencies>
  <build>
  <plugins>
  <plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  </plugin>
  </plugins>
  </build>
  package com.ms.aop.jthis.demo1;
  public interface IService {
  void m1();
  }
  package com.ms.aop.jthis.demo1;
  slf4j.Slf4j;
  import org.springframework.stereotype.Component;
  @Slf4j
  @Component
  public class ServiceImpl implements IService {
  @Override
  public void m1() {
  log.info("切⼊点this测试!");
  }
  }
  package com.ms.aop.jthis.demo1;
  slf4j.Slf4j;
  import org.aspectj.lang.ProceedingJoinPoint;
  import org.aspectj.lang.annotation.Around;
  import org.aspectj.lang.annotation.Aspect;
  import org.aspectj.lang.annotation.Pointcut;
  import org.springframework.stereotype.Component;
  @Aspect
  @Component
  @Slf4j
  public class Interceptor1 {
  @Pointcut("this(com.ms.aop.jthis.demo1.ServiceImpl)")
  public void pointcut() {
  }
  @Around("pointcut()")
  public Object invoke(ProceedingJoinPoint invocation) throws Throwable {
  log.info("⽅法执⾏之前");
  Object result = invocation.proceed();
  log.info("⽅法执⾏完毕");
  return result;
  }
  }
  package com.ms.aop.jthis.demo1;
  slf4j.Slf4j;
  import t.annotation.AnnotationConfigApplicationContext; 
  import t.annotation.ComponentScan;msinfo
  import t.annotation.EnableAspectJAutoProxy;
  @ComponentScan(basePackageClasses = {Client.class})
  @EnableAspectJAutoProxy
  @Slf4j
  public class Client {
  public static void main(String[] args) {
  AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Client.class);
  IService service = Bean(IService.class);
  service.m1();
  log.info("{}", service instanceof ServiceImpl);
  }
  }
  执⾏结果

本文发布于:2024-09-22 08:21:29,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/1/380291.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:包中   拦截   任意   对象   指定   类型   表达式
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议