环绕通知的注解为@Around
around方法的返回值就是目标代理方法执行返回值
参数ProceedingJoinPoint可以拦截目标方法的执行
切面类配置如下
i
mport org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import java.math.BigInteger; @Aspect public class aspectj { // @Before(value = "execution(* com.AspecJ.xiaomaoDao.*(..))") // public void before(){ // System.out.println("我是前置通知!"); // } // @AfterReturning(value = "execution(* com.AspecJ.xiaomaoDao.delete())",returning = "element") // 使用returning来接受返回值 // public void After(Object element){ // System.out.println("我删除了"+element); //打印输出了返回值 // } @Around(value="execution(* com.AspecJ.xiaomaoDao.update())") 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; } }
目标类方法如下:
public class xiaomaoDao { public void save(){ System.out.println("save xiaomao!"); } public void find(){ System.out.println("find xiaomao!"); } public String delete(){ System.out.println("delete xiaomao!"); return "xiaomao"; } public void update(){ System.out.println("update xiaomao!"); } }
执行结果如下:
delete xiaomao! find xiaomao! save xiaomao! 环绕前 update xiaomao! 环绕后
原文地址:https://www.cnblogs.com/xiaolaha/p/11370854.html
时间: 2024-10-10 17:59:33