• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to write Interception Around Advice in Spring AOP?

July 15, 2008 //  by Krishna Srinivasan//  Leave a Comment

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:

  • Spring Tutorials
  • Spring 4 Tutorials
  • Spring Interview Questions

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 Books
  • Introduction to Spring Framework
  • Introduction to Spring MVC Framework

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>

Category: Spring FrameworkTag: Spring AOP

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Simple example for Before advice and After Advice in Spring Framework
Next Post: How to write Throws Advice in Spring AOP? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact