Home Tutorial Spring Spring3 Aop Spring AOP Around Advice

 
 

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.

SimpleInterface.java

package roseindia.net.aop;

public interface SimpleInterface {
	void testMethod();
}

SimpleInterfaceImpl.java

package roseindia.net.aop;

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

AroundAdviceExample.java

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;
	}

}

AroundAdviceTest.java

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(
				"classpath:./springconfig/spring-config.xml");

		SimpleInterface simpleInterface = (SimpleInterface) applicationContext
				.getBean("proxyBean");

		simpleInterface.testMethod();
	}
}

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

    <!-- Bean configuration -->
    <bean id="proxyBean"
        class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces">
            <value>roseindia.net.aop.SimpleInterface</value>
        </property>
        <property name="target">
            <ref local="beanTarget" />
        </property>
        <property name="interceptorNames">
            <list>
                <value>theTracingAroundAdvisor</value>
            </list>
        </property>
    </bean>

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

    <!-- Advisor pointcut definition for before advice -->
    <bean id="theTracingAroundAdvisor"
        class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice">
            <ref local="theTracingAroundAdvice" />
        </property>
        <property name="pattern">
            <value>.*</value>
        </property>
    </bean>

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

</beans>

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:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.