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");
}
}
}
TrackEmployee.java
============
package
com.mohan;
import
org.aspectj.lang.JoinPoint;
public
class TrackEmployee{
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...");
}
}
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<aop:aspectj-autoproxy
/>
<bean
id="empBean" class="com.mohan.Employee"> </bean>
<bean
id="trackAspect"
class="com.mohan.TrackEmployee"></bean>
<aop:config>
<aop:aspect id="myaspect"
ref="trackAspect" >
<!-- @AfterThrowing -->
<aop:pointcut
id="pointCutAfterThrowing" expression="execution(*
com.mohan.Employee.*(..))" />
<aop:after-throwing
method="myadvice" throwing="error"
pointcut-ref="pointCutAfterThrowing" />
</aop:aspect>
</aop:config>
</beans>
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 op = (Employee)
context.getBean("empBean");
System.out.println("calling
validate...");
try{
op.validate(19);
}catch(Exception
e){System.out.println(e);}
System.out.println("calling validate
again...");
try{
op.validate(11);
}catch(Exception
e){System.out.println(e);}
}
}
No comments:
Post a Comment