10. Spring 面向切面
Posted 侯光辉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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"); } }
以上是关于10. Spring 面向切面的主要内容,如果未能解决你的问题,请参考以下文章