1.1) Introduction
Let is illustrate the various types of Advices (Before Advice, After Advice, Throws Advice and Around Advice) that we saw before in this sample Application. For this sample application let us define Adder Service which provides logic for adding two numbers. The various classes involved in Application along with the Advices in the subsequent sections.
1.2) Addder.java
This is the interface definition for the Add Service. The interface name is Adder and it has one single method called add() taking two arguments both of type int.
Adder.java
package net.javabeat.spring.aop.introduction.test;
public interface Adder {
public int add(int a,int b);
}
1.3) AdderImpl.java
The implementation class for the Add Service. The logic is as simple as it returns the summation of the two numbers given as arguments. Note that, in the later section we will see how Advices get bound with this Implementation Class.
AdderImpl.java
package net.javabeat.spring.aop.introduction.test;
public class AdderImpl implements Adder {
public int add(int a, int b){
return a+b;
}
}
1.4) Before Advice Implementation
This is the Before Advice for the Adder Implmentation class. This class implements the before() method in the MethodBeforeAdvice interface by simply outputting a message telling that this advice is called.
LogBeforeCallAdvice.java
package net.javabeat.spring.aop.introduction.test;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class LogBeforeCallAdvice implements MethodBeforeAdvice{
public void before(Method method, Object[] args, Object target) {
System.out.println("Before Calling the Method");
}
}
1.5) After Advice Implementation
The After Method Call Advice implements the AfterReturningAdvice interface providing implementation for the afterReturning() method. Like the Before Advice implementation, this Advice also outputs a simple message to the console.
LogAfterReturningAdvice.java
package net.javabeat.spring.aop.introduction.test;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
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");
}
}
1.6) Throws Advice Implementation
This Advice will be called when some kind of Exception is caught during the method invocation. We have added a simple logic to simlate the exception when the user inputs are 0 and 0.
LogAfterThrowsAdvice.java
package net.javabeat.spring.aop.introduction.test;
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
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());
}
}
1.7) Around Advice Implementation
This Advice takes the entire control during the Method Execution. It decides whether the add() method should be called or not based on the user inputs. Note that, only if the user inputs are not 0 and 0, then the add() method will be called through MethodInvocation.proceed().
LogAroundAdvice.java
package net.javabeat.spring.aop.introduction.test;
import org.aopalliance.intercept.*;
public class LogAroundAdvice implements MethodInterceptor{
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
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!!!");
}
return methodInvocation.proceed();
}
}
1.8) Configuration File
The Configuration File has 3 sections. One section is the Advice Bean Definition Section which is the definition set for all the 4 advices which we saw before. All the advices are given identifiers like 'beforeCall', 'afterCall', 'throwCall' and 'aroundCall'. Then contains the Bean Definition for the Add implementation class which is giving the identifier 'adderImpl'.
The next interesting section is how to bind these advices to the implementation code. For this, we have to depend on ProxyFactory Bean. This Bean is used to create Proxy objects for the Add Implementation class along with the Advice implementation. Note that the property 'proxyInterfaces' contains the Interface Name for which the proxy class has to ge generated. In our case, it is going to be the Adder interface. The 'interceptorNames' property takes a list of Advices to be applied to the dynamically generated proxy class. We have given all the 4 advices to this property. Finally the implementation class for the Adder service is given in the 'target' property.
aop-test.xml
1.9) Test Class
Following is the test class for the Adder Service. The code loads the Bean Definition File by depending on the BeanFactory class. Watch carefully in the output for the various Advices getting called. Also, we have made to activate the Simulated Exception by passing 0 and 0 as arguments to the add() method call thereby making use of the Throws Advice.
AdderTest.java
package net.javabeat.spring.aop.introduction.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;
public class AdderTest {
public static void main(String args[]){
Resource resource = new FileSystemResource("./src/aop-test.xml");
BeanFactory factory = new XmlBeanFactory(resource);
Adder adder = (Adder)factory.getBean("adder");
int result = adder.add(10,10);
System.out.println("Result = " + result);
result = adder.add(0,0);
System.out.println("Result = " + result);
}
}
2) Conclusion
This article provided information on how to use Spring AOP for programming the Aspects in an Application.
No comments:
Post a Comment