Home Tutorial Spring Spring3 Aop Spring AOP Around Advice


Share on Google+Share on Google+
Spring AOP Around Advice
Posted on: September 3, 2010 at 12:00 AM
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


Related Tags for Spring AOP Around Advice:

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 3, 2010

Recommend the tutorial

Advertisements Advertisements