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 -->
springboot aop  </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;
  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);
  }
  }
  执⾏结果

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