jenad
Jun 14th, 2011, 03:37 AM
Hi,
I am new to Spring java config with aop.So please help me to resolve the issue.
I have created a simple project where i am using Spring Java config instead of xml config and spring aop to print something in console when exception thrown in any method.
<Code>
My Configuration class is :
import org.springframework.aop.aspectj.annotation.Annotat ionAwareAspectJAutoProxyCreator;
import org.springframework.context.annotation.Bean;
public class AppConfig {
@Bean
public ExceptionTest exceptionTest() {
return new ExceptionTest();
}
@Bean
public AopException aopException() {
return new AopException();
}
@Bean
public AnnotationAwareAspectJAutoProxyCreator annotationAwareAspectJAutoProxyCreator(){
AnnotationAwareAspectJAutoProxyCreator aop=new AnnotationAwareAspectJAutoProxyCreator();
return aop;
}
My AOP Exception class is :
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.aop.ThrowsAdvice;
@Aspect
public class AopException implements ThrowsAdvice {
@AfterThrowing(pointcut="execution(* com.acs.AopException.method1())",throwing="ex")
public void afterThrowing(Throwable ex)
{
System.out.println("Inside afterThrowing()");
}
}
My Business Java class is :
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationC onfigApplicationContext;
public class ExceptionTest {
public void method1()
{
try {
int x=10;
int y=x/0;
} catch (ArithmeticException ex) {
System.out.println("Exception Occured");
ex.printStackTrace();
}
}
public static void main(String args[])
{
ApplicationContext con=new AnnotationConfigApplicationContext(AppConfig.class );
ExceptionTest t =(ExceptionTest)con.getBean(ExceptionTest.class);
t.method1();
}
}
When i run the Exception java class, it is throwing the arithmatic exception and come out of the execution.It is not invoking the afterThrowing(Throwable ex) method of AopException class.
So could you please let me know what is wrong in my code.
I am new to Spring java config with aop.So please help me to resolve the issue.
I have created a simple project where i am using Spring Java config instead of xml config and spring aop to print something in console when exception thrown in any method.
<Code>
My Configuration class is :
import org.springframework.aop.aspectj.annotation.Annotat ionAwareAspectJAutoProxyCreator;
import org.springframework.context.annotation.Bean;
public class AppConfig {
@Bean
public ExceptionTest exceptionTest() {
return new ExceptionTest();
}
@Bean
public AopException aopException() {
return new AopException();
}
@Bean
public AnnotationAwareAspectJAutoProxyCreator annotationAwareAspectJAutoProxyCreator(){
AnnotationAwareAspectJAutoProxyCreator aop=new AnnotationAwareAspectJAutoProxyCreator();
return aop;
}
My AOP Exception class is :
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.aop.ThrowsAdvice;
@Aspect
public class AopException implements ThrowsAdvice {
@AfterThrowing(pointcut="execution(* com.acs.AopException.method1())",throwing="ex")
public void afterThrowing(Throwable ex)
{
System.out.println("Inside afterThrowing()");
}
}
My Business Java class is :
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationC onfigApplicationContext;
public class ExceptionTest {
public void method1()
{
try {
int x=10;
int y=x/0;
} catch (ArithmeticException ex) {
System.out.println("Exception Occured");
ex.printStackTrace();
}
}
public static void main(String args[])
{
ApplicationContext con=new AnnotationConfigApplicationContext(AppConfig.class );
ExceptionTest t =(ExceptionTest)con.getBean(ExceptionTest.class);
t.method1();
}
}
When i run the Exception java class, it is throwing the arithmatic exception and come out of the execution.It is not invoking the afterThrowing(Throwable ex) method of AopException class.
So could you please let me know what is wrong in my code.