Aop in Spring
Aop in Spring
// TODO Auto-generated method stub In xml file just add the bean releted to afterclass & in list add this like <list> <value>adv</value> <value>adv2</value> </list> ThrowAdvice Add the third bean for throwadvice class implement the method as public void afterThrowing(IllegalArgumentException e) throws Throwable { System.out.println("HijackThrowException : Throw exception hijacked!"); } In list add this bean id for catching.
AroundAdvice Add one class implement with MethodInterceptor add method of it ..Invoke as
public Object invoke(MethodInvocation methodInvocation) throws Throwable { // // // // System.out.println("Method name : " + methodInvocation.getMethod().getName()); System.out.println("Method arguments : " + Arrays.toString(methodInvocation.getArguments())); // same with MethodBeforeAdvice System.out.println("Before.............around!"); try { // proceed to original method call Object result = methodInvocation.proceed(); // same with AfterReturningAdvice System.out.println("After..................around"); return result; } catch (IllegalArgumentException e) { // same with ThrowsAdvice System.out.println("HijackAroundMethod : Throw exception hijacked!"); throw e; } }
Add to list.
AOP PointCut..Example
In pointcut it it used for applying advice on only one of the methods.as in above all advices, advice gets applied to each & every methodso using pointcut we can specify particular methodas Define the pointcut first
<bean id="myPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut"> <property name="mappedName" value="m1" /> </bean>
Value contains the name of the method. Then Now define the advisior for this pointcut as <bean id="aopproxyptcut" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="pointcut" ref="myPointcut" /> <property name="advice" ref="adv4" />the advice that we want to apply </bean>
<value>
aopproxyptcut</value>
AfterAdvice Class
package com; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; public class AfterAdv implements AfterReturningAdvice { @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { System.out.println("after method..."); // TODO Auto-generated method stub } }
Class aopimpl
package com; public class aopimpl implements infointr { @Override public void say() { // TODO Auto-generated method stub System.out.println("Hello world frm class"); //m1(); } public void m1() { int a=3/3;
System.out.println(a); } }
Interface :
package com; public interface infointr { public void say(); public void m1(); }
Class aroundadvice
package com; import import import import java.lang.reflect.Method; java.util.Arrays; org.aopalliance.intercept.MethodInterceptor; org.aopalliance.intercept.MethodInvocation;
public Object invoke(MethodInvocation methodInvocation) throws Throwable { // // // // System.out.println("Method name : " + methodInvocation.getMethod().getName()); System.out.println("Method arguments : " + Arrays.toString(methodInvocation.getArguments())); // same with MethodBeforeAdvice System.out.println("Before.............around!"); try { // proceed to original method call Object result = methodInvocation.proceed(); // same with AfterReturningAdvice System.out.println("After..................around"); return result; } catch (IllegalArgumentException e) { // same with ThrowsAdvice
class beforeadv
package com; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class beforeadv implements MethodBeforeAdvice{ @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { // TODO Auto-generated method stub System.out.println("before method......"); }
class Client
package com; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) { ApplicationContext c= new ClassPathXmlApplicationContext("app.xml"); infointr itr=(infointr)c.getBean("aopimplproxy");
itr.say(); itr.m1(); }}
class throwadvice
package com; import org.springframework.aop.ThrowsAdvice; public class throwadvice implements ThrowsAdvice{ public void afterThrowing(IllegalArgumentException e) throws Throwable { System.out.println("HijackThrowException : Throw exception hijacked!"); } public void afterThrowing(Exception e) throws Throwable { System.out.println("HijackThrowException : Throw exception hijacked!"); } }
<!--**********************************************************Code for Before advice applies... --> <bean id="aop" class="com.aopimpl"/> <bean id="adv" class="com.beforeadv"></bean> <bean id="aopimplproxy"
class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.infointr</value> </property> <property name="target" ref="aop" /> <property name="interceptorNames"> <list> <value>adv</value> <value>adv2</value> <value>adv3</value> <value>aopproxyptcut</value>
</list> </property> </bean> <bean id="adv2" class="com.AfterAdv"></bean> <bean id="adv3" class="com.throwadvice"/> <bean id="adv4" class="com.aroundadvice"></bean> <!--**********************************************************Code for Before advice applies... --> <!--**********************************************************Code for after advice applies... --> <!-- After the Advice now.....
<bean id="aopimplproxy2" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.infointr</value> </property> <property name="target" ref="aop" /> <property name="interceptorNames"> <list> <value>adv2</value> </list> </property> </bean> --> <!-- Performing the pointcut operations.....first add the pointcut as: --> <!-- defining the pointcut for specying the name of method --> <bean id="myPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut"> <property name="mappedName" value="m1" /> </bean>
<!-- defining the advisior to the pointcut....--> <bean id="aopproxyptcut" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="pointcut" ref="myPointcut" /> <property name="advice" ref="adv4" /> </bean> </beans>