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 .:: | ||
Comments
6 Responses to “Spring AOP Around Advice Example With Complete Explanation”
Why we have not mentioned the class name for i3 in spconfig.xml
@Abhishek
Yeah its happened by mistake, now its updated 🙂 good observation, thank you.
How to access point-cut parameters?
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…
super explanation sir , i studied many books as well as sites but this site is enough to starting people
Your examples and futher explaination is gud but i think there should be some explaination abt internal flow of execution.