SpringAOP

SpringAOP相关概念

今天面试了上海萌奕,发现自己在SpringAOP这一块概念有点不清晰,于是打算今晚打算整理+复习一遍。

  1. Aspect(切面): Aspect 声明类似于 Java 中的类声明,在 Aspect 中会包含着一些 Pointcut 以及相应的 Advice。

  2. Joint point(连接点):表示在程序中明确定义的点,典型的包括方法调用,对类成员的访问以及异常处理程序块的执行等等,它自身还可以嵌套其它 joint point。

  3. Pointcut(切点):表示一组 joint point,这些 joint point 或是通过逻辑关系组合起来,或是通过通配、正则表达式等方式集中起来,它定义了相应的 Advice 将要发生的地方。

  4. Advice(增强):Advice 定义了在 Pointcut 里面定义的程序点具体要做的操作,它通过 before、after 和 around 来区别是在每个 joint point 之前、之后还是代替执行的代码。

  5. Target(目标对象):织入 Advice 的目标对象.。

  6. Weaving(织入):将 Aspect 和其他对象连接起来, 并创建 Adviced object 的过程。

     

Jointpoint可以里额为所有可能被织入Advice的点,在SpringAOP中所有方法执行点都是Jointpoint。Pointcut的作用可以理解为寻找满足特定规则的Jointpoint来执行Advice。

总结就是:pointcut寻找满足条件的jointpoint来执行advice,advice 是在 joinpoint 上执行的, 而 pointcut 规定了哪些 joinpoint 可以执行哪些 advice。pointcut和advice合称为aspect。

 

Advice 的类型

  • before advice, 在 join point 前被执行的 advice. 虽然 before advice 是在 join point 前被执行, 但是它并不能够阻止 join point 的执行, 除非发生了异常(即我们在 before advice 代码中, 不能人为地决定是否继续执行 join point 中的代码)

  • after return advice, 在一个 join point 正常返回后执行的 advice

  • after throwing advice, 当一个 join point 抛出异常后执行的 advice

  • after(final) advice, 无论一个 join point 是正常退出还是发生了异常, 都会被执行的 advice.

  • around advice, 在 join point 前和 joint point 退出后都执行的 advice. 这个是最常用的 advice.

  • introduction,introduction可以为原有的对象增加新的属性和方法。  后续补充。

 

 后续补充。

你可能感兴趣的:(Spring)