Spring AOP Advice Ordering


 

Spring AOP Advice Ordering

In this tutorial you will learn how to use more than one advices in a single application.

In this tutorial you will learn how to use more than one advices in a single application.

Spring AOP Advice Ordering

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"?>
<!DOCTYPE beans PUBLIC  "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

  <!--CONFIG-->
  <bean id="simpleBean" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
      <value>roseindia.net.bean.SimpleInterface</value>
    </property>
    <property name="target">
      <ref local="beanTarget" />
    </property>
    <property name="interceptorNames">
      <list>
        <value>beforeAdvisor</value>
        <value>aroundAdvisor</value>
        <value>afterReturnAdvisor</value>
        <value>afterThrowsAdvisor</value>
      </list>
    </property>
  </bean>

  <!--Interface Class-->
  <bean id="beanTarget" class="roseindia.net.bean.SimpleInterfaceClaz" />

  <!--Before Advisor-->
  <bean id="beforeAdvisor"
    class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
      <ref local="beforeAdvice" />
    </property>
    <property name="pattern">
      <value>.*</value>
    </property>
  </bean>

  <!--Around Advisor-->
  <bean id="aroundAdvisor"
    class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
      <ref local="aroundAdvice" />
    </property>
    <property name="pattern">
      <value>.*</value>
    </property>
  </bean>

  <!--After Return Advisor-->
  <bean id="afterReturnAdvisor"
    class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
      <ref local="afterReturnAdvice" />
    </property>
    <property name="pattern">
      <value>.*</value>
    </property>
  </bean>

  <!--After Throws Advisor-->
  <bean id="afterThrowsAdvisor"
    class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice">
      <ref local="afterThrowsAdvice" />
    </property>
    <property name="pattern">
      <value>.*</value>
    </property>
  </bean>

  <!--Advice Class Mapping-->
  <bean id="beforeAdvice" class="roseindia.net.advice.BeforeAdvice" />
  <bean id="aroundAdvice" class="roseindia.net.advice.AroundAdvice" />
  <bean id="afterReturnAdvice" class="roseindia.net.advice.AfterReturnAdvice" />
  <bean id="afterThrowsAdvice" class="roseindia.net.advice.AfterthrowsAdvice" />
</beans>

When you run this application it will display message as shown below:


*************************************
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

Download this example code

Ads