Declaring Advice (interceptor)

The next step is to define Advice. This is an action performed on a join-point. Advice is also referred as an interceptor. Spring AOP supports various types of Advice, as shown here:

  • Before Advice: This type of Advice is executed just before the execution starts at the join-point. In the case of an exception, Spring will stop further execution of this Advice.
  • After Advice: As its name suggests, this type of Advice is executed after completion of join-point execution (either a normal exit, or in the case of an exception, from the join-point).
  • Around Advice: This type of Advice is executed around the join-point (before and/or after the advised method). Because of this, you have the control to execute the join-point and return the original method value, or bypass the flow and return a custom value. In general, around Advice is used to execute some logic before and/or after a method's main logic. Due to this, it's the most powerful type of Advice.
  • After returning Advice: This is similar to after Advice. The difference is that it's being executed on a normal exit from a join-point.
  • After throwing Advice: This is also similar to after Advice, but is executed when an exception occurred during execution of the Advice's method.
In XML-based Spring AOP, you need to be careful of the order of AOP elements while defining them within <aop:config>. For example, <aop:pointcut> must be defined before or within the <aop:aspect> element or else Spring will show an error. Similarly, AOP Advice must be defined within the <aop:aspect> element.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.227.9