Spring AOP Around Advice Example With Complete Explanation

Spring » on Jul 29, 2012 { 6 Comments } By Sivateja

Will see few points about Around Advice before going to do an example ūüôā

  • Around Advice is combination of both Before and After Advice.
  • In a single Advice it is possible to implement both Before and After services.
  • Around Advice is not given by spring framework and it is from Open Source implementation called AOP alliance.
  • Around Advice can be used by any framework which supports AOP.
  • To create Around Advice, our class should implement an interface called MethodInterceptor.
  • In Around Advice, we implement Before and After Advice in a single method called invoke(), in order to separate Before an After services to execute business logic, in the middle we call proceed() method.
  • Around Advice can access the return value of business method and it can modify the value and it can return a different value back to the client, as return type is Object, but in the After Advice its not possible right, as its return type is void.

Syntax For AroundAdvice Implementation

public class Client implements MethodInterceptor
    public Object invoke(MethodInvocation mi)throws Throwable
        //Before Logic     
        Object ob = mi.proceed();
        //After logic
        return ob;


Example On Spring AOP ThrowsAdvice

Files required…

  • spconfig.xml

package java4s;

public interface MyInterFace
    void m1();

package java4s;

public class MyImplClass implements MyInterFace
    public void m1(){

        System.out.println("Am in business method..");


package java4s;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class MyMethodAdvice implements MethodInterceptor
    public Object invoke(MethodInvocation mi)throws Throwable

        System.out.println("Am before proceed() method");        
        Object ob = mi.proceed();
        System.out.println("Am after proceed() method");
        return ob;



package java4s;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

public class OurLogic
    public static void main(String args[])
        Resource res = new ClassPathResource("spconfig.xml");
        BeanFactory factory = new XmlBeanFactory(res);

        MyInterFace inter =(MyInterFace)factory.getBean("id3");




 <bean id="id1" class="java4s.MyImplClass" />
 <bean id="id2" class="java4s.MyMethodAdvice" />
 <bean id="id3" class="org.springframework.aop.framework.ProxyFactoryBean">

      <property name="proxyInterfaces" value="java4s.MyInterFace" />
      <property name="interceptorNames" >
       <property name="target">
               <ref bean="id1"/>       




‚Äč ‚Äč‚Äč

You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


6 Responses to “Spring AOP Around Advice Example With Complete Explanation”
  1. abhishek says:

    Why we have not mentioned the class name for i3 in spconfig.xml

  2. Java4s says:


    Yeah its happened by mistake, now its updated ūüôā good observation, thank you.

  3. Shridevi says:

    How to access point-cut parameters?

  4. Meghna says:

    Thanks for the clean example Sivateja… Even this might help… Have a look…

  5. mahendra says:

    super explanation sir , i studied many books as well as sites but this site is enough to starting people

  6. savita says:

    Your examples and futher explaination is gud but i think there should be some explaination abt internal flow of execution.


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.