Introduction to Spring’s Aspect Oriented Programming(AOP)

6) Sample Application 6.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. 6.2) 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. package net.javabeat.spring.aop.introduction.test; public interface Adder { public int add(int a,int b); } 6.3) 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. package net.javabeat.spring.aop.introduction.test; public class AdderImpl implements Adder { public int add(int a, int b){ return a+b; } } 6.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. 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”); } } 6.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. 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”); } } 6.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. 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()); } } 6.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(). 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(); } } 6.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 net.javabeat.spring.aop.introduction.test.Adder beforeCall afterCall throwCall aroundCall 6.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. package net.javabeat.spring.aop.introduction.test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import*; 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); } } 7) Conclusion This article provided information on how to use Spring AOP for programming the Aspects in an Application. It started with defining what Aspects are and what are problems in having the Aspects directly embedded into the Application and how to separate them using AOP. It then looked briefly into the various AOP Terminologies like Advice, Point Cut, Join Points etc. Then it moved on into the various support for creating Advices using Spring AOP. Also covered in brief are the Static Point Cuts like Name Method Match and Regular Expression Point Cuts. Finally the article concluded with a Sample Application that illustrates the usage of different Advices.

About eagle081183

Passionate, Loyal
This entry was posted in Integration, Java, Software architecture. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s