关于aop:AOP进行通知切面的步骤

1、根本配置
1)在Spring的配置文件或配置类中,开启注解扫描
2)应用注解创立被加强类和加强类对象
3)在加强类上增加注解@Aspect
4)在Spring配置文件中开启生成代理对象
xml配置文件形式:
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

2、配置不同类型的告诉
加强类(代理类)

@Component
@Aspect
public class UserProxy{
  //前置告诉
  @Before(value="execution(* com.zong.spring.User.add(..))")
  public void before(){
    System.out.println("before...");
  }
  
  //后置告诉
  @AfterReturning(value="execution(* com.zong.spring.User.add(..))")
  public void afterReturning(){
    System.out.println("afterReturning...");
  }
  
  //最终告诉
  @After(value="execution(* com.zong.spring.User.add(..))")
  public void after(){
    System.out.println("after...");
  }
  
  //异样告诉
  @AfterThrowing(value="execution(* com.zong.spring.User.add(..))")
  public void afterThrowing(){
    System.out.println("afterThrowing...");
  }
  
  //盘绕告诉
  @Around(value="execution(* com.zong.spring.User.add(..))")
  public void around(ProceedingJoinPoint proceedingJoinPoint){
    System.out.println("盘绕之前...");
    //执行被加强的办法
    proceedingJoinPoint.proceed();
    System.out.println("盘绕之后...");
  }
}

执行后果

盘绕之前...
before..
add..
盘绕之后..
after...
afterReturning...

异样执行后果

盘绕之前...
Before...
after...
afterThrowing..

.

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理