Spring Framework

Spring Projects

Spring Project 1

Example of After Throws Advice
Previous Home Next

Introduction: An after throws advice is used to performed some operation between throwing and catching an exception.

Technology use to run this source code

  1. Spring 2.5 jar files
  2. Eclipse IDE
  3. Tomcat Server

Source Code:

applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="t" class="org.r4r.One"/>
<bean id="a2" class="org.r4r.ThrowAdvisor" />
<bean id="proxy" 
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="t"/>
<property name="interceptorNames">
<list>
<value>a2</value>
</list>
</property>
</bean>
</beans>

Showable.java

package org.r4r;
public interface Showable {
String show(int x);
}

 

One.java

package org.r4r;
public class One implements Showable {
@Override
public String show(int x){
if(x>0){
System.out.println("Show() of one invoked returning success");
return "success";
}
else{
System.out.println("Shows() of one invoked, throwing exception");
throw(new IllegalArgumentException("It is thrown by show()"));
}
}
}

ThrowAdvisor.java

package org.r4r;
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class ThrowAdvisor implements ThrowsAdvice {
/*public void afterThrowing(Exception e){
System.out.println("In throws advice because
of following exception"+e);
}*/
public void afterThrowing
	(Method m,Object args[],Object target,Exception e){
System.out.println("In throws advice because"+m.getName()+
"() throw following exception "+e.getMessage()+
" When invoked on the object of "
+target.getClass().getName()+" Class.");
}
}

AroundAdviser.java

package org.r4r;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AroundAdviser implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
System.out.println("Before advice is applied...");
Object temp=mi.proceed();
System.out.println
("After advice is applied, "+temp+"\tis return by the method.");
System.out.println
("Returning failure from the advice");
return "failure";
}
}

ProxyTest.java

package org.r4r;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class ProxyTest {
public static void main(String[] args) {
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory f=new XmlBeanFactory(r);
System.out.println("Obtaining proxy of one..");
Showable p=(Showable)f.getBean("proxy");
try{
System.out.println("proxy obtained invoking show() with positive value");
String s=p.show(5);
System.out.println(s+" Is received by the client");
System.out.println("Invoking show() with negative value");
p.show(-2);
}catch(Exception e){
System.out.println("Following exception caught in main"+e);
}
}
}

output:

Obtaining proxy of one..
proxy obtained invoking show() with positive value
Show() of one invoked returning success
success Is received by the client
Invoking show() with negative value
Shows() of one invoked, throwing exception
In throws advice becauseshow() throw following 
exception It is thrown by show() When invoked on
the object of org.r4r.One Class.
Following exception caught in 
mainjava.lang.IllegalArgumentException: It is thrown by show()
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: