Interception Around Advice Example
In this tips we explore how to use the Interception Around Advice in Spring’s Aspect Oriented Programming(AOP). Interception Around Advice is fundamental advice type in the Spring framework. Also this advice type is derived from other frameworks like AspectJ. So it is interoporable with other frameworks, other advices in the Spring AOPs are specific to Spring Framework and cannot be used in the other similar frameworks.
also read:
Interception Around Advice is called on before method calls and after method calls. So, when you need advice for both entry and exit then only use thie advice, Otherwise use other availble simple advices Before Advice or After Returning Advice.
The below example programs demonstrated how to write one simple Around Advice. To write Around Advice you have to implement MethodInterceptor and override the invoke method which takes MethodInvocation as the parameter. To understand more on how it works, please look into the example programs.
SpringAopMain.java
package javabeat.net.spring.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; /** * source : www.javabeat.net */ public class SpringAopMain { public static void main(String[] args) { // Read the configuration file ApplicationContext ctx = new FileSystemXmlApplicationContext( "spring-config.xml"); // Instantiate an object BusinessInterface businessInterface = (BusinessInterface) ctx.getBean("businesslogicbean"); // Execute the public method of the bean System.out.println(businessInterface.businessLogicMethod()); } }
BusinessInterface.java
package javabeat.net.spring.aop; /** * source : www.javabeat.net */ public interface BusinessInterface { String businessLogicMethod(); }
BusinessInterfaceImpl.java
package javabeat.net.spring.aop; /** * source : www.javabeat.net */ public class BusinessInterfaceImpl implements BusinessInterface{ public String businessLogicMethod() { System.out.println("BusinessLogic Method Called"); return "returnVal"; } }
AroundAdviceExample.java
package javabeat.net.spring.aop; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * source : www.javabeat.net */ public class AroundAdviceExample implements MethodInterceptor{ public Object invoke(MethodInvocation method) throws Throwable { System.out.println("Before Invoking Method"); Object val = method.proceed(); System.out.println("After Invoking Method"); return val + "updated value"; } }
also read:
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="businesslogicbean" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>javabeat.net.spring.aop.BusinessInterface</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="javabeat.net.spring.aop.BusinessInterfaceImpl" /> <!-- 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="javabeat.net.spring.aop.AroundAdviceExample" /> </beans>