定义切点,切点表达式(execution(权限访问符 返回值类型 方法所属的类名包路径.方法名(形参类型) 异常类型))
@Aspect
@Component
public class AfterThrowingAspect {
@Pointcut("execution(* com.yh.test.*.*(..))")
public void pointcut() {}
.......
}
@Before
前置增强,配合@Pointcut一起使用
@Aspect
@Component
public class AfterThrowingAspect {
//全路径execution(public String com.cnblogs.hellxz.test.Test.access(String,String))
@Pointcut("execution(* com.yh.test.*.*(..))")
public void pointcut() {}
//前置增强
@Before("pointcut()")
public void before(JoinPoint jp){
System.out.println("我是方法"+jp.getSignature().getName()+"的前置增强!");
}
}
@AfterReturning
后置增强,配合@Pointcut一起使用
@Aspect
@Component
public class AfterThrowingAspect {
@Pointcut("execution(* com.yh.test.*.*(..))")
public void pointcut() {}
//前置增强
@Before("pointcut()")
public void before(JoinPoint jp){
System.out.println("我是方法"+jp.getSignature().getName()+"的前置增强!");
}
//后置增强
@AfterReturning(value = "pointcut()",returning = "obj")
public void afterReturning(JoinPoint jp,Object obj){
System.out.println("我是方法"+jp.getSignature().getName()+"的后置增强!"+",返回值为"+obj);
}
}
@Around
环绕增强,配合@Pointcut一起使用**
@Aspect
@Component
public class AfterThrowingAspect {
//全路径
@Pointcut("execution(* com.yh.test.*.*(..))")
public void pointcut() {}
//前置增强
@Before("pointcut()")
public void before(JoinPoint jp){
System.out.println("我是方法"+jp.getSignature().getName()+"的前置增强!");
}
//后置增强
@AfterReturning(value = "pointcut()",returning = "obj")
public void afterReturning(JoinPoint jp,Object obj){
System.out.println("我是方法"+jp.getSignature().getName()+"的后置增强!"+",返回值为"+obj);
}
//环绕增强
@Around("pointcut()")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("我是环绕增强中的前置增强!");
Object proceed = jp.proceed();//植入目标方法
System.out.println("我是环绕增强中的后置增强!");
}
}
@AfterThrowing
异常抛出增强,配合@Pointcut一起使用**
@Aspect
@Component
public class AfterThrowingAspect {
@Pointcut("execution(* com.yh.test.*.*(..))")
public void pointcut() {}
//前置增强
@Before("pointcut()")
public void before(JoinPoint jp){
System.out.println("我是方法"+jp.getSignature().getName()+"的前置增强!");
}
//后置增强
@AfterReturning(value = "pointcut()",returning = "obj")
public void afterReturning(JoinPoint jp,Object obj){
System.out.println("我是方法"+jp.getSignature().getName()+"的后置增强!"+",返回值为"+obj);
}
//环绕增强
@Around("pointcut()")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("我是环绕增强中的前置增强!");
Object proceed = jp.proceed();//植入目标方法
System.out.println("我是环绕增强中的后置增强!");
}
//异常抛出增强
@AfterThrowing(value = "pointcut()",throwing = "e")
public void error(JoinPoint jp,Exception e){
System.out.println("我是异常抛出增强"+",异常为:"+e.getMessage());
}
}
@After
最终增强(最后执行),配合@Pointcut一起使用**
@Aspect
@Component
public class AfterThrowingAspect {
//全路径execution(public String com.cnblogs.hellxz.test.Test.access(String,String))
@Pointcut("execution(* com.cnblogs.hellxz.test.*.*(..))")
public void pointcut() {}
//前置增强
@Before("pointcut()")
public void before(JoinPoint jp){
System.out.println("我是方法"+jp.getSignature().getName()+"的前置增强!");
}
//后置增强
@AfterReturning(value = "pointcut()",returning = "obj")
public void afterReturning(JoinPoint jp,Object obj){
System.out.println("我是方法"+jp.getSignature().getName()+"的后置增强!"+",返回值为"+obj);
}
//环绕增强
@Around("pointcut()")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("我是环绕增强中的前置增强!");
Object proceed = jp.proceed();//植入目标方法
System.out.println("我是环绕增强中的后置增强!");
}
//异常抛出增强
@AfterThrowing(value = "pointcut()",throwing = "e")
public void error(JoinPoint jp,Exception e){
System.out.println("我是异常抛出增强"+",异常为:"+e.getMessage());
}
//最终增强
@After("pointcut()")
public void after(JoinPoint jp){
System.out.println("我是最终增强");
}
}