Krishna Rajendra Alapati

IT Professional having 4+ years of experience on Java and JEE technologies. Good experience in working with Servlets, JSP, EJB2, XML, XSLT and JEE Design patterns. Good experience in working with open source frameworks like Struts, Spring, Hibernate, ANT, JUnit and JMock.

My Photo
Name:
Location: London, Ilford, United Kingdom

Monday, July 21, 2008

Exception handling using Spring AOP

The implementation of the aspect is called as Advice and these Advice will be applied or woven at different pointcuts(i.e. before,after or around the method or class). For Exception handling no need to have these pointcuts defined as these advice should be woven upon an Exception. Exceptions can also be handled using the AOP. When you use spring AOP we have an advice called ThrowsAdvice which is a marker interface and can 'n' number of "afterThrowing( Throwable th)" methods with different kind of custom exceptions as parameter.

Here is an example of ThrowsAdvice. I have a MyThrowsAdvice which can handle NullPointerException, NumberFormatException. In this example i am using the Runtime Exceptions rather than customException, you can use them as well.


package com.janus.examples.aop.advice;

import org.springframework.aop.ThrowsAdvice;

public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing( NullPointerException npe ){
System.out.println("There is some NullPointer Exception is thrown");
}

public void afterThrowing( NumberFormatException nfe ){
System.out.println("There is some NumberFormat Exception is thrown");
}
}



Here is the Service Interface & Implementations class

package com.janus.examples.aop;

public interface HelloService {
String sayHello( String aName, String aNum );
String sayWelcome(String aName,String aNum);
}



package com.janus.examples.aop;
public class HelloServiceImpl implements HelloService {

public String sayHello(String aName,String aNum) {
return "Hello! "+ aName + " It's great that you are running your first AOP program. Argument Passed" + Integer.parseInt(aNum) ;
}

public String sayWelcome(String aName,String aNum) {

return "Welcome!! " + aName +" to Aspect Oriented program. Argument passed "+ aNum.toLowerCase() ;
}

}


Here is the Configuration XML


<beans>
<bean id="helloServiceImpl"
class="com.janus.examples.aop.HelloServiceImpl"/>

<bean id="myThrowsAdvice"
class="com.janus.examples.aop.advice.MyThrowsAdvice"/>


<bean id="helloService"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.janus.examples.aop.HelloService</value>
</property>
<property name="interceptorNames">
<list>
<value>myThrowsAdvice</value>
</list>
</property>
<property name="target">
<ref bean="helloServiceImpl"/>
</property>
</bean>
</beans>

Here is the client and pretty simple to execute, as it's a main program. Try to send a string as second argument for sayHello it throws an NumberFormatException.
And if you send a null as second argument to sayWelcome it will throw the NullPointerException.

import java.io.FileInputStream;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import com.janus.examples.aop.HelloService;

public class helloclient

{

public static void main(String args[]) throws Exception

{
try

{

BeanFactory factory = new XmlBeanFactory(new FileInputStream(
"D:/SpringTraining/config/Mybeans.xml"));
HelloService bean1 = (HelloService) factory.getBean("helloService");
String s = bean1.sayHello("Krishna Rajendra", "123");
System.out.println(s);

String s1 = bean1.sayWelcome("Krishna Rajendra", null);
System.out.println(s1);

}

catch (Exception e1)

{
System.out.println("" + e1);
}

}

}

Hope This gives you a very good idea on Exception handling using AOP

--
Regards,
Krishna Rajendra A.