Monday, March 25, 2013

Spring Framework- Execption handling in AOP


Hureeeee!!!!!  Today in spring framework we learn some intresting with AOP, so today topic is Execption handling. AOP contain a method proceed() . If this method is proceed with exception then it pass the controll of the program to the catch block, In That type examples we can also used @AfterThrowing , @AfterReturning ,@Around . here we use @Around annotation to follow the AOP. So here below is the example --




Class circle.java-

package com.test.App;

public class Circle {
String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Class Triangle.java-

package com.test.App;

public class Triangle {
String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Class Shape.java-

package com.test.service;

import com.test.App.Circle;
import com.test.App.Triangle;

public class Shape {

Circle circle;
Triangle triangle;
public Circle getCircle() {
throw (new RuntimeException());
}
public void setCircle(Circle circle) {
this.circle = circle;
}
public Triangle getTriangle() {
return triangle;
}
public void setTriangle(Triangle triangle) {
this.triangle = triangle;
}

}
Class LogAspect.java-


package com.test.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LogAspect {

   @Before("allcircle()")
public void loggingAdvice(JoinPoint joinpoint)
{
  System.out.println("advice");
}
 
   @Pointcut("execution(* get*())")
public void allGetters()
{
System.out.println("Wel Come With....allgetters");

}
 
   @Pointcut("within(com.test.App.Circle)")
   public void allcircle()
   {
System.out.println("Wel Come With....circle");

   }
 

 @AfterReturning(pointcut="args(name)",returning="returnstring")
   public void strigArgs(String name,Object returnstring)
   {
  System.out.println("string args is used"+name+"return string is="+returnstring);
   }
 @AfterThrowing(pointcut="args(name)",throwing="ex")
 public void throwEX(String name,RuntimeException ex)
 {
System.out.println("exception is call "+ex);

 }
 @Around("allGetters()")
 public Object aroundProcess(ProceedingJoinPoint process)
 {
try {
System.out.println("before advice");
process.proceed();
System.out.println("atfer advice");
} catch (Throwable e) {
// TODO Auto-generated catch block
System.out.println("After throw");

}
System.out.println("After finally");

return null;
 }
}

Class Main.java


package com.test.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.service.Shape;

public class Main {


public static void main(String[] args) {

ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Shape sh=context.getBean("shape", Shape.class);
sh.getCircle();
}

}

Spring.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">

  <aop:aspectj-autoproxy/>
<bean id="triangle" class="com.test.App.Triangle">
 <property name="name" value="triangle drawn"></property>
 </bean>
 <bean id="circle" class="com.test.App.Circle">
  <property name="name" value="circle drawn"></property>
 </bean>
<bean id="shape" class="com.test.service.Shape" autowire="byName">
</bean>
<bean id="log" class="com.test.aspect.LogAspect"></bean>
</beans>

Output-
before advice
After throw
After finally


For Further Reading,
General, Java, spring

0 comments:

Post a Comment


 

Site Status

Man Behind Technical Today

Hello, I am Navin Bansal. I am a student of MCA in Rajsthan Institute of Engineering and Technology and owner of this blog. I share my view and ideas among people.