Advice ordering is required when you use more than one advice in your application. Spring AOP follows some precedence rules to determine the order of advice execution. A simple example given below which illustrates the execution of multiple advices in a single application.
SimpleInterface.java
package roseindia.net.bean;
public interface SimpleInterface {
void sayHi();
void greet();
}
SimpleInterfaceClaz.java
package roseindia.net.bean;
public class SimpleInterfaceClaz implements SimpleInterface {
@Override
public void greet() {
// TODO Auto-generated method stub
System.out.println("Have a nice Day");
}
@Override
public void sayHi() {
// TODO Auto-generated method stub
System.out.println("Hello Friend");
}
}
AfterReturnAdvice.java
package roseindia.net.advice;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterReturnAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object object1, Method method, Object[] objects,
Object object2) throws Throwable {
// TODO Auto-generated method stub
System.out.println("*************************************");
System.out.println("Inside AfterReturn Advice");
System.out.println("Invoking Method " + method.getName());
}
}
AfterthrowsAdvice.java
package roseindia.net.advice;
import org.springframework.aop.ThrowsAdvice;
public class AfterthrowsAdvice implements ThrowsAdvice {
public void afterThrowing(RuntimeException runtimeException) {
System.out.println("*************************************");
System.out.println("Inside Throws Advice ");
}
}
AroundAdvice.java
package roseindia.net.advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AroundAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
// TODO Auto-generated method stub
System.out.println("*************************************");
System.out.println("Inside Around Advice");
System.out.println("Invoking Method "
+ methodInvocation.getMethod().getName());
Object object = methodInvocation.proceed();
return object;
}
}
BeforeAdvice.java
package roseindia.net.advice;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object object)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("*************************************");
System.out.println("Inside Method Before Advice");
System.out.println("Invoking Method " + method);
}
}
MainClaz.java
package roseindia.net.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import roseindia.net.bean.SimpleInterface;
public class MainClaz {
public static void main(String[] args) {
ApplicationContext applicationContext = new FileSystemXmlApplicationContext(
"classpath:./config/config.xml");
SimpleInterface simpleObject = (SimpleInterface) applicationContext
.getBean("simpleBean");
simpleObject.sayHi();
simpleObject.greet();
}
}
config.xml
<?xml version="1.0" encoding="UTF-8"?>
|
| ************************************* Inside Method Before Advice Invoking Method public abstract void roseindia.net.bean.SimpleInterface.sayHi() ************************************* Inside Around Advice Invoking Method sayHi Hello Friend ************************************* Inside AfterReturn Advice Invoking Method sayHi ************************************* Inside Method Before Advice Invoking Method public abstract void roseindia.net.bean.SimpleInterface.greet() ************************************* Inside Around Advice Invoking Method greet Have a nice Day ************************************* Inside AfterReturn Advice Invoking Method greet |