Sunteți pe pagina 1din 9

AOP How TO????

Before Advice
Define one Interface.. Define one class that implements the interfaceREF@ Define mainclient class Define one advisior class REF!!! Create xml file in that Add the beans for advisior & ur impl classREF@ Create the Proxy as
<bean id="aopimplproxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.infointr</value> </property> <property name="target" ref="aop" />the class on which aop applies <property name="interceptorNames">//advisior classREF!!! <list> <value>adv</value> </list> </property> </bean>

For After Advice ::


Create the class before advice * implement the AfterReturningAdvice
method provided i.e. public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { System.out.println("after method..."); // 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> interface & add the

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 bean for this class in spring.xml 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>

Lastly add the name aopproxyptcut to list of interceptors..


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

Source Code total_AOP Work..:

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;

import net.sf.cglib.proxy.MethodProxy; public class aroundadvice implements MethodInterceptor {

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; } } }

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!"); } }

XML FILE App.xml:


<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--**********************************************************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>

S-ar putea să vă placă și