Can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Spring AOP Around Advice Example With Complete Explanation

Spring » On Jul 29, 2012 | { 3 Comments }

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:

 

What you are thinkig....

3 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?

If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum

Name*
Ask a Question ?
or
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
Servlets, Jsps
is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2013 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by str-Graphics.com