Implementing after returning advice

The after returning advice works just like after advice. The only difference is that this advice will be executed on matching methods only on normal exit. If an exception occurs, this advice will not be applied.

We will look at a scenario to understand the need for this advice. Suppose you want to send a message (email) to a concerned person when a particular method is executed successfully on the target class. Since this is a new concern (sending a message on successful execution of the target method), we have created a new aspect class as follows:

package com.packt.spring.aop.aspects;
import org.aspectj.lang.JoinPoint;
public class SendMessage {

//Advice method after successful existing of target method.
public void sendMessageOnSuccessExit(JoinPoint joinPoint) {
System.out.println(" ****** Method '"+joinPoint.getSignature().getName()+"' of "+joinPoint.getTarget().getClass()+" is executed successfully...");
}
}

The SendMessage aspect has one method called sendMessageOnSuccessExit. We want this method to be called on a normal method exit (without an exception) from the target class. You can write the logic to send the message (email) in this method. The new aspect will be configured in the application context (XML) file as follows:

<aop:pointcut id="hrServiceMethods"
expression="execution(* com.packet.spring.aop.service.HRService.*(..))" />

<aop:aspect id="sendMsg" ref="sendMsgAspect">
<aop:after-returning pointcut-ref="hrServiceMethods"
method="sendMessageOnSuccessExit"/>
</aop:aspect>

We have created a new point-cut that will match all the methods of the HRService class. This class will be as follows:

package com.packt.spring.aop.service;

public class HRService {

public void showHolidayList() {
System.out.println("This is holiday list method...");
}

public void showMyLeave() throws Exception {
System.out.println("Showing employee leaves...");
throw new Exception();
}
}

When you get an object of HRService from Spring and call the showHolidayList method, you will get output as follows:

This is holiday list method...
****** Method 'showHolidayList' of class com.packet.spring.aop.service.HRService is executed successfully...

If the target method returns a value and you want to modify it with AOP, you can do this  with after returning advice. For this, you need to specify the parameter name in the <aop:aspect> element as follows:

<aop:aspect id="sendMsg" ref="sendMsgAspect">
<aop:after-returning pointcut-ref="hrServiceMethods"
returning="retVal"
method="sendMessageOnSuccessExit"/>
</aop:aspect>

In this code, the value of the returning attribute says that the sendMessageOnSuccessExit method must declare a parameter called retVal.  Spring AOP will pass the return value from the method of the target object to this parameter (retVal) while applying this advice. Therefore, the type of the return value from the method of the target object must be compatible with the type of the parameter (retVal, in our case) in the advice method. Let's update the showHoliday method of the SendMessage advice as follows:

  public String showHolidayList() {
System.out.println("This is holiday list method...");
return "holidayList";
}

The return value type of this method is String. To update the return value, you need to change the advice method as follows:

//Advice method after successful existing of target method.
public String sendMessageOnSuccessExit(JoinPoint joinPoint,String retVal) {
System.out.println(" ****** Method '"+joinPoint.getSignature().getName()+"' of "+joinPoint.getTarget().getClass()+" is executed successfully...");
System.out.println(" The return value is -->"+retVal);
return "Successfully exited with return val is -->"+retVal;
}

When you get the HRService object from Spring and call its showHolidayList() method, you will get the following updated return value:

This is holiday list method...
****** Method 'showHolidayList' of class com.packet.spring.aop.service.HRService is executed successfully...
The return value is -->holidayList
..................Content has been hidden....................

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