Spring AOP Around Advice

In this tutorial you will learn about Spring AOP Around Advice and its mapping in XML file.


Share on Google+Share on Google+

Spring AOP Around Advice


In this tutorial you will learn about Spring AOP Around Advice and its mapping in XML file.

Spring AOP Around Advice

Around Advice performs the custom behavior before and after method invocation by surrounding a join point. It is called before and After method execution when you need advice for both entry and exit then only use this advice. To implement around advice you need to implement MethodInterceptor of import org.aopalliance.intercept.MethodInterceptor package.


package roseindia.net.aop;

public interface SimpleInterface {
	void testMethod();


package roseindia.net.aop;

public class SimpleInterfaceImpl implements SimpleInterface {
	public void testMethod() {
		System.out.println(" This is Test Method();");


package roseindia.net.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AroundAdviceExample implements MethodInterceptor {
	public Object invoke(MethodInvocation method) throws Throwable {
		System.out.println(" Advice Before method");
		Object obj = method.proceed();
		System.out.println("Advice After Method");
		return null;



package roseindia.net.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class AroundAdviceTest {
	public static void main(String[] args) {

		ApplicationContext applicationContext = new FileSystemXmlApplicationContext(

		SimpleInterface simpleInterface = (SimpleInterface) applicationContext



<?xml version="1.0" encoding="UTF-8"?>


    <!-- Bean configuration -->
    <bean id="proxyBean"
        <property name="proxyInterfaces">
        <property name="target">
            <ref local="beanTarget" />
        <property name="interceptorNames">

    <!-- Bean Classes -->
    <bean id="beanTarget" class="roseindia.net.aop.SimpleInterfaceImpl" />

    <!-- Advisor pointcut definition for before advice -->
    <bean id="theTracingAroundAdvisor"
        <property name="advice">
            <ref local="theTracingAroundAdvice" />
        <property name="pattern">

    <!-- Advice classes -->
    <bean id="theTracingAroundAdvice"
        class="roseindia.net.aop.AroundAdviceExample" />


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

Advice Before method
This is Test Method();
Advice After Method

Download this example code



Posted on: September 3, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles