Monday, December 28, 2009

AOP in Spring Example

package aop;

/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 1:49:12 AM
* To change this template use File | Settings | File Templates.
*/

public interface Adder {

public int add(int a, int b);


}



=====================================================

package aop;

/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 1:51:13 AM
* To change this template use File | Settings | File Templates.
*/


public class AdderImpl implements Adder {

public int add(int a, int b) {
return a + b;
}

}

===============================================================

package aop;/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 1:53:20 AM
* To change this template use File | Settings | File Templates.
*/

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class LogAfterReturningAdvice implements AfterReturningAdvice {

public void afterReturning(Object returnValue, Method method, Object[] args,
Object target) throws Throwable {

System.out.println("After Normal Return from Method (here you can remove attribute in session )");
}

}
=============================================================
package aop;/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 1:53:53 AM
* To change this template use File | Settings | File Templates.
*/

import org.springframework.aop.ThrowsAdvice;

import java.lang.reflect.Method;

public class LogAfterThrowsAdvice implements ThrowsAdvice {

public void afterThrowing(Method method, Object[] args, Object target,
Exception exception) {

System.out.println("Exception is thrown on method " + method.getName());
}

}
======================================================
package aop;/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 1:54:23 AM
* To change this template use File | Settings | File Templates.
*/

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class LogAroundAdvice implements MethodInterceptor {

public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("LogAroundAdvice invoke method (here you can check method Arguments validation) ");
Object arguments[] = methodInvocation.getArguments();
int number1 = ((Integer) arguments[0]).intValue();
int number2 = ((Integer) arguments[1]).intValue();

if (number1 == 0 && number2 == 0) {
throw new Exception("Dont know how to add 0 and 0!!!");
}

if(number2==0){
throw new Exception("Dont know how to divide by 0 !!!");
}

return methodInvocation.proceed();
}
}

==========================================================
package aop;/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 1:52:11 AM
* To change this template use File | Settings | File Templates.
*/

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class LogBeforeCallAdvice implements MethodBeforeAdvice {

public void before(Method method, Object[] args, Object target) {
System.out.println("Before Calling the Method (here you can check user sessions) ");
}

}
=======================================================
package aop;

/**
* Created by IntelliJ IDEA.
* User: Gayan-Laptop
* Date: Dec 22, 2009
* Time: 2:18:15 AM
* To change this template use File | Settings | File Templates.
*/

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

public class AdderTest {

public static void main(String args[]) {
try {
Resource resource = new FileSystemResource("./aop-test.xml");
BeanFactory factory = new XmlBeanFactory(resource);

try{
Adder adder = (Adder) factory.getBean("adder");
System.out.println("------------- TEST CASE 1 ---------------");
int result = adder.add(10, 10);
System.out.println("Result = " + result);
System.out.println("------------- TEST CASE 2 ---------------");
result = adder.add(100, 10);
System.out.println("Result = " + result);
System.out.println("------------- TEST CASE 3 ---------------");
result = adder.add(101, 110);
System.out.println("Result = " + result);
System.out.println("------------- TEST CASE 4 ---------------");
result = adder.add(0, 0);
System.out.println("Result = " + result);
}catch(Exception e){
e.printStackTrace();
}

try{
Divider divider = (Divider) factory.getBean("divider");

System.out.println("------------- TEST CASE 5 ---------------");
int result2 = divider.divide(6,3);
System.out.println("Result = " + result2);
System.out.println("------------- TEST CASE 6 ---------------");
result2 = divider.divide(12,3);
System.out.println("Result = " + result2);
System.out.println("------------- TEST CASE 7 ---------------");
result2 = divider.divide(16,3);
System.out.println("Result = " + result2);
System.out.println("------------- TEST CASE 9 ---------------");
result2 = divider.divide(6,0);
System.out.println("Result = " + result2);


}catch(Exception e){
e.printStackTrace();
}



} catch (Exception e) {
e.printStackTrace();
}
}
}


================================================================

aop-test.xml file


<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-2.0.xsd">





<!-- Advices -->

<bean id="beforeCall" class="aop.LogBeforeCallAdvice"/>

<bean id="afterCall" class="aop.LogAfterReturningAdvice"/>

<bean id="throwCall" class="aop.LogAfterThrowsAdvice"/>

<bean id="aroundCall" class="aop.LogAroundAdvice"/>



<!-- Implementation Class -->

<bean id="adderImpl" class="aop.AdderImpl"/>

<bean id="dividerImpl" class="aop.DividerImpl"/>



<!-- Proxy Implementation Class -->

<bean id="adder" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="proxyInterfaces">

<value>aop.Adder</value>

</property>

<property name="interceptorNames">

<list>

<value>beforeCall</value>

<value>afterCall</value>

<value>throwCall</value>

<value>aroundCall</value>

</list>

</property>

<property name="target">

<ref bean="adderImpl"/>

</property>

</bean>



<bean id="divider" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="proxyInterfaces">

<value>aop.Divider</value>

</property>

<property name="interceptorNames">

<list>

<value>beforeCall</value>

<value>afterCall</value>

<value>throwCall</value>

<value>aroundCall</value>

</list>

</property>

<property name="target">

<ref bean="dividerImpl"/>

</property>

</bean>



</beans>

No comments: