By
using after throwing advice, we can print the exception in the TrackEmployee
class. Let's see the example of AspectJ AfterThrowing advice.
Create
the class that contains business logic.
Employee.java
==========
package
com.mohan;  
public  class Employee{  
public
void validate(int age)throws Exception{  
    if(age<18){  
        throw new ArithmeticException("Not
valid age");  
    }  
    else{ 
        System.out.println("Thanks for
vote");  
    }  
    }  
}     
Create
the aspect class that contains after throwing advice.
Here,
we need to pass the Throwable reference also, so that we can intercept the
exception here.
TrackEmployee.java
============= 
package
com.mohan;  
import
org.aspectj.lang.JoinPoint;
import
org.aspectj.lang.annotation.AfterThrowing;
import
org.aspectj.lang.annotation.Aspect;  
@Aspect  
public
class TrackEmployee{  
    @AfterThrowing(  
              pointcut = "execution(*
Employee.*(..))",  
              throwing= "error")  
    public void myadvice(JoinPoint jp,Throwable
error)//it is advice  
    {  
        System.out.println("additional
concern");  
        System.out.println("Method
Signature: "  +
jp.getSignature());  
        System.out.println("Exception is:
"+error);  
        System.out.println("end of after
throwing advice...");  
    }  
}  
Now
create the Beans.xml file that defines beans.
Beans.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/beans   
      
http://www.springframework.org/schema/beans/spring-beans.xsd   
      
http://www.springframework.org/schema/aop   
      
http://www.springframework.org/schema/aop/spring-aop.xsd">  
    <bean id="empBean"
class="com.mohan.Employee">  
</bean>  
    <bean id="trackEmp"
class="com.mohan.TrackEmployee"></bean>  
    <bean
class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>  
</beans>   
Now
create the Test class that calls the actual methods.
Test.java
======
package
com.mohan;  
import
org.springframework.context.ApplicationContext; 
import
org.springframework.context.support.ClassPathXmlApplicationContext;  
public
class Test{  
    public static void main(String[]
args){  
        ApplicationContext context = new
ClassPathXmlApplicationContext("Beans.xml");  
        Employee emp = (Employee)
context.getBean("empBean");  
       System.out.println("calling
validate...");  
        try{ 
            emp.validate(19);  
        }catch(Exception
e){System.out.println(e);}  
        System.out.println("calling
validate again...");  
        try{ 
            emp.validate(11);  
        }catch(Exception
e){System.out.println(e);}  
    }  
}        
Expected Output
calling
validate...  
Thanks
for vote  
calling
validate again...  
additional
concern  
Method
Signature: void com.javatpoint.Operation.validate(int)  
Exception
is: java.lang.ArithmeticException: Not valid age  
end
of after throwing advice...  
java.lang.ArithmeticException:
Not valid age  
No comments:
Post a Comment