SpringBoot使用AOP,PointCut详解

aop依赖:


	org.springframework.boot
	spring-boot-starter-aop

1、相关注解介绍:

​​​​@Aspect          :把当前类标识为一个切面;

@Pointcut        :Pointcut是织入Advice的触发条件。每个Pointcut的定义包括2部分,一是表达式,二是方法签名。方法签名必须是public及void型。可以将Pointcut中的方法看作是一个被Advice引用的助记符,因为表达式不直观,因此我们可以通过方法签名的方式为此表达式命名。因此Pointcut中的方法只需要方法签名,而不需要在方法体内编写实际代码。

@Around         :环绕增强,目标方法执行前后分别执行一些代码;
@AfterReturning  :返回增强,目标方法正常执行完毕时执行;
@Before          :前置增强,目标方法执行之前执行;
@AfterThrowing   :异常抛出增强,目标方法发生异常的时候执行;
@After          :后置增强,不管是抛出异常或者正常退出都会执行。

2、@PointCut 表达式

        PointCut是指哪些方法需要被执行"AOP",PointCut表达式可以有一下几种方式。

2.1、 execution:

        用于指定方法的执行。

格式:

 execution( 方法类型(public等,可省略) 方法的返回值类型 包路径(可省略) 方法的名称(参数) 异常类型(可省略) )

方法类型包含:Public,Protected等,可省略。

方法返回值类型* 可以包含所有的返回值类型。

包路径    :如“com.demo…*”,表示"com.demo"包以及该包之下子包的所有类型。

方法名称:如“add*”,表示所有以add开头的方法,

参数       :(*)表示任意一个参数,(…)表示所有参数。

异常类型:如execution(* *(…) throws Exception)”匹配所有抛出Exception的方法。

2.2、within

        是用来指定类型的,指定类型中的所有方法将被拦截。

       within(com.demo.service.impl.UserServiceImpl)   : 匹配UserServiceImpl类对应对象的所有方法调用,并且只能是UserServiceImpl对象,不能是它的子对象。

       within(com.demo…*)匹配com.demo包及其子包下面的所有类的所有方法的外部调用。

2.3、this

        SpringAOP是基于代理的,this就代表代理对象,语法是this(type),当生成的代理对象可以转化为type指定的类型时表示匹配。

       this(com.demo.service.IUserService) :匹配生成的代理对象是IUserService类型的所有方法的外部调用。

  this(com.demo.service.AccountService)  :实现了AccountService 接口的代理对象的任意连接点。 

2.4、target

        SpringAOP是基于代理的,target表示被代理的目标对象,当被代理的目标对象可以转换为指定的类型时则表示匹配。

        target(com.demo.service.IUserService)  : 匹配所有被代理的目标对象能够转化成IuserService类型的所有方法的外部调用。

   target(com.demo.service.AccountService) :实现了AccountService 接口的目标对象的任意连接点。 

2.5、args

        args用来匹配方法参数。

args() : 匹配不带参数的方法。

args(java.lang.String) : 匹配方法参数是String类型的。

args(…)  :带任意参数的方法。

args(java.lang.String,…) :匹配第一个参数是String类型的,其他参数任意。

2.6 、@within 和 @target

        带有相应标注的所有类的任意方法,比如@Transactional

@within(org.springframework.transaction.annotation.Transactional)

@target(org.springframework.transaction.annotation.Transactional)

2.7、@annotation

        带有相应标注的任意方法,比如@Transactional

@annotation(org.springframework.transaction.annotation.Transactional)

@within和@target针对类的注解

@annotation针对方法的注解

2.8 、@args

        参数带有相应标注的任意方法,比如@Transactional

   @args(org.springframework.transaction.annotation.Transactional)

3、PointCut使用

3.1、基本使用:

    @Pointcut("execution(public * com.example.demo.controller.ProductController.*(..))")
    public void pointCut() {
    }

    @Before(value = "pointCut()")
    public void before(JoinPoint joinPoint) {
        log.info("@Before通知执行");
    }

3.2、PointCut中的运算符

        PointCut中可以使用&&、||、! 运算。

	@Pointcut("execution(public * com.example.demo.controller.UserController.*(..))")
	public void cutController(){
	}
	
	@Pointcut("execution(public * com.example.demo.Service.UserService.*(..))")
	public void cutService(){
	}
	
   //使用  && 运算符,则cutAll()的作用等同于  cutController  和 cutService 之和
	@Pointcut("cutController() && cutService()")
	public void cutAll(){
	}

你可能感兴趣的:(#,springboot,springcloud,spring,boot)