spring AOP之重用切点表达式

spring AOP之重用切点表达式,第1张

概述在使用@Before(execution(value=""))使用切点时,如果是需要重复使用,可以进行统一的设置。 比如说现在有这么一个前置通知和后置通知: package com

在使用@Before(execution(value=""))使用切点时,如果是需要重复使用,可以进行统一的设置。

比如说现在有这么一个前置通知和后置通知:

package com.gong.spring.aop.impl;import java.util.Arrays;import java.util.List;import javax.management.RuntimeErrorException;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;//把这个类声明为一个切面:需要把该类放入到IOC容器中,再声明为一个切面@Aspect@Componentpublic class LoggingAspect {    声明该方法为一个前置通知,在目标方法之前执行    @Before("execution(public int com.gong.spring.aop.impl.Calculator.*(int,int))")    voID beforeMethod(JoinPoint joinPoint) {        String methodname = joinPoint.getSignature().getname();        List<Object> args = Arrays.asList(joinPoint.getArgs());        System.out.println(methodname+ begin with "+args);    }    后置通知:在目标方法执行后,无论是否发生异常,执行的通知    在后置通知中不能访问目标的执行结果    @After( afterMethod(JoinPoint joinPoint) {        获取名字        String methodname = joinPoint.getSignature().getname();        获取参数        List<Object> args = end with args);    }}

在@before里面的切点是一样的,我们可以将重复的用切点表达式表示。

在LoggingAspect 类中新建:

    @pointcut(voID declarJoinPointExpression() {}

然后在使用Before等注解时,就可以简化了:

@Before(declarJoinPointExpression())@After(")
总结

以上是内存溢出为你收集整理的spring AOP之重用切点表达式全部内容,希望文章能够帮你解决spring AOP之重用切点表达式所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1226950.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存