Around Advice Example-1
Around advice is most powerful and useful advice. In all other advices we can not control the execution of the method. Around advice can be used to get the use of all types of advices with in a single advice. The below example will demonstrate how a simple around advice can be used to implement before, after returning, after-throwing and finally advices.
Example:-
Service.java
package com.visualbuilder.aop;
public interface Service {
void actualMethod();
}
|
ServiceImpl.java
package com.visualbuilder.aop;
public class ServiceImpl implements Service{
public void actualMethod() {
System.out.println("This is the Actual Method in the service class.");
}
}
|
AdviceClass.java
package com.visualbuilder.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class AdviceClass {
public Object pointcutMethod(ProceedingJoinPoint pjp) throws Throwable {
Object retVal=null;
try{
System.out.println("This is Before Advice Text");
retVal = pjp.proceed();
System.out.println("This is After Retruning Advice Text");
}catch(Exception e){
System.out.println("This is After throwing Advice Text");
}finally{
System.out.println("This is After (finally) Advice Text");
return retVal;
}
}
}
|
Note:- All the around advice pointcut methods need to accept the ProceedingJoinPoint which is actual a control for taregt method.