当前位置: 代码迷 >> 综合 >> Spring AOP 建议类型
  详细解决方案

Spring AOP 建议类型

热度:69   发布时间:2024-02-10 19:03:02.0

Spring AOP 包括如下类型的建议:

  • Before advice:在连接点之前执行的建议。它没有能力阻止连接点的执行。

  • After returning advice:在连接点正常执行后执行。

  • After throwing advice:在方法抛出异常的时候执行。

  • After (finally) advice:在一个连接点执行后执行,不管这个连接点是怎样退出的。正常或者异常返回,都会执行。

  • Around advice:一个在连接点周围执行的建议。这个是最强大的一类建议。

  • Around advice  能够在方法调用的时候自定义一下行为。他负责选择继续执行这个连接点,或者通过返回自己的值或者抛出异常来截断这个被建议的方法的执行。

Around advice is the most general kind of advice. Since Spring AOP, like AspectJ, provides a full range of advice types, we recommend that you use the least powerful advice type that can implement the required behavior. For example, if you need only to update a cache with the return value of a method, you are better off implementing an after returning advice than an around advice, although an around advice can accomplish the same thing. Using the most specific advice type provides a simpler programming model with less potential for errors. For example, you do not need to invoke the proceed() method on the JoinPoint used for around advice, and, hence, you cannot fail to invoke it.

All advice parameters are statically typed so that you work with advice parameters of the appropriate type (e.g. the type of the return value from a method execution) rather than Object arrays.

The concept of join points matched by pointcuts is the key to AOP, which distinguishes it from older technologies offering only interception. Pointcuts enable advice to be targeted independently of the object-oriented hierarchy. For example, you can apply an around advice providing declarative transaction management to a set of methods that span multiple objects (such as all business operations in the service layer).

  相关解决方案