10. Spring 面向切面
10.1 产品清单概述
- 通知(Advice)
- 前置通知(Before)
- 后置通知(After)
- 返回通知(After-returning)
- 异常通知(After-throwing)
- 环绕通知(Around)
- 连接点(Join point)
- 切点(Poincut)
- 切面(Aspect)
- 引入(Introduction)
- 织入(Weaving)
- 编译期
- 类加载期
- 运行期
10.1 @Aspect定义切面、定义切入位置
import org.aspectj.lang.annotation.*; @Aspect public class Notifier { @Before("execution(static void *..*.main(String[]))") public void before(){ System.out.println("Application is running!"); } @After("execution(static void *..*.main(String[]))") public void after(){ System.out.println("execute end!"); } @AfterReturning("execution(static void *..*.main(String[]))") public void afterReturning(){ System.out.println("return end!"); } @AfterThrowing("execution(static void *..*.main(String[]))") public void afterThrowing(){ System.out.println("Exception"); } }
10.2 使用PointCup定义切入点、重复使用切入点
import org.aspectj.lang.annotation.*; @Aspect public class Notifier { @Pointcut("execution(static void *..*.main(String[]))") public void pointCup(){} @Before("pointCup()") public void before(){ System.out.println("Application is running!"); } @After("pointCup()") public void after(){ System.out.println("execute end!"); } @AfterReturning("pointCup()") public void afterReturning(){ System.out.println("return end!"); } @AfterThrowing("pointCup()") public void afterThrowing(){ System.out.println("Exception"); } }