通过@Pointcut为切点命名,方便我们统一管理
在每个通知内定义切点,会造成工作量大,不易维护,对于重复的切点,可以使用@Pointcut进行定义
切点方法:private void无参方法,方法名为切点名
当通知多个切点时,可以使用||来进行连接
具体代码:
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import java.math.BigInteger; @Aspect public class aspectj { @Before(value = "execution(* com.AspecJ.xiaomaoDao.*(..))") public void before(){ System.out.println("我是前置通知!"); } @AfterReturning(value = "Mypoint2()",returning = "element") // 使用returning来接受返回值 public void After(Object element){ System.out.println("我删除了"+element); //打印输出了返回值 } @Around(value="MyPoint1()") public Object around(ProceedingJoinPoint joinPoint){ Object obj=null; System.out.println("环绕前"); try { obj=joinPoint.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println("环绕后"); return obj; } @AfterThrowing(value = "MyPoint()",throwing = "throwinfo") public void throwafter(Throwable throwinfo){ System.out.println("发生了异常,异常信息如下:"); System.out.println(throwinfo.getMessage()); } @After(value = "MyPoint()") public void after(){ System.out.println("最终通知执行了!"); } @Pointcut(value = "execution(* com.AspecJ.xiaomaoDao.find())") public void MyPoint(){} @Pointcut(value = "execution(* com.AspecJ.xiaomaoDao.update())") public void MyPoint1(){} @Pointcut(value = "execution(* com.AspecJ.xiaomaoDao.delete())") public void Mypoint2(){} }
原文地址:https://www.cnblogs.com/xiaolaha/p/11371509.html
时间: 2024-10-10 17:59:34