Newsletter

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…

  • MyImplClass.java
  • MyInterFace.java
  • MyAroundAdvice.java
  • OurLogic.java
  • spconfig.xml

MyInterFace.java

package java4s;

public interface MyInterFace
{
    void m1();
}

MyImplClass.java

package java4s;

public class MyImplClass implements MyInterFace
{
    public void m1(){

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

    }
}

MyThrowsAdvice.java

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;

    }

}

OurLogic.java

package java4s;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

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");
        inter.m1();
    }
}

spconfig.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
            "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

 <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" >
                  <list>
                       <value>id2</value>
                   </list>
       </property>
       <property name="target">
               <ref bean="id1"/>       
       </property>     

 </bean>

</beans>

Output:

​​

You Might Also Like

  ::. About the Author .::

Java4s_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.

Comments

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:

    @Abhishek

    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 http://www.compiletimeerror.com/2013/05/spring-aop-around-advice-example.html 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.

Name*
Mail*
Website



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.