How to write Interception Around Advice in Spring AOP?

July 15, 2008

Spring Framework

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.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

1
2
3
4
5
6
7
8
package javabeat.net.spring.aop;
 
/**
 * source : www.javabeat.net
 */
public interface BusinessInterface {
    String businessLogicMethod();
}

BusinessInterfaceImpl.java

1
2
3
4
5
6
7
8
9
10
11
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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";
    }
 
}

spring-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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>
email

Comments

comments