spring_AOP

例子代码

理解AOP

  AOP为Aspect Oriented Programming的缩写,意为:面向切面编程。大概意思就是在原有源代码的基础上,增加功能,而又不修改原有的代码。

术语

切面(Aspect):进行增加处理的类,比如对一功能增加日志管理操作,定义的日志类便是一个切面。

连接点(Joinpoint):明确的切入点,如com.xyz.myapp.SystemArchitecture.dataAccessOperation()

切入点(Pointcut):可以看成是连Joinpoint的集合,如@Pointcut("execution(public * com.bjsxt.service..*.add(..))")

增强处理(Advice):如Around,Before,After,AfterReturning,AfterThrowing

增强处理

Around:在切入点方法运行时先运行增强处理方法,在切入点方法运行结束后再运行增强处理方法。@Around("execution(* org.crazyit.app.service.impl.*.*(..))")

Before:在切入点主法运行时先运行增强处理方法,@Before("execution(* org.crazyit.app.service.impl.*.*(..))")

After:在切入点方法运行结束后再运行增强处理方法(不管目标方法如何结束(包知成功完成和遇到异常中止两种情况),都会被织入。@After("execution(* org.crazyit.app.service.*.*(..))"

AfterReturning:在切入点方法运行结束后再运行增强处理方法(只有在目标方法成功完成后才会被织入)。如下所示:

@AfterReturning(returning="rvt",pointcut="execution(* org.crazyit.app.service.impl.*.*(..))")
public void log(Object rvt){
  System.out.println("获取目标方法返回值:"+rvt);
  System.out.println("模拟记录日志功能...");    
}

AfterThrowing:在抛出异常时织入。

 AOP的annotation与xml

annotation

XML

  

原文地址:https://www.cnblogs.com/cnJun/p/3794205.html