AopService.java
Code:
package com.spring.aop;
import org.springframework.stereotype.Component;
@Component
public class AopService {
public void service() {
System.out.println("xxxxxxxxxx: AopService.service()");
throw new IllegalArgumentException("test exception");
}
}
TestAspect.java
Code:
package com.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class TestAspect {
@AfterThrowing(pointcut = "execution(* com.spring.aop.AopService.*(..))", throwing = "e")
public void advice(JoinPoint joinPoint, Throwable e) {
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
System.out.println("xxxxxxxxx: TestAspect.advice(). Return type is " + signature.getReturnType());
}
}
spring-config.xml
HTML Code:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<context:component-scan base-package="com.spring.aop"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
SpringStart.java
Code:
package com.spring;
import com.spring.aop.AopService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringStart {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
AopService aopService = (AopService)context.getBean("aopService");
aopService.service();
}
}
Output:
Code:
xxxxxxxxxx: AopService.service()
xxxxxxxxx: TestAspect.advice(). Return type is void
Exception in thread "main" java.lang.IllegalArgumentException: test exception
at com.spring.aop.AopService.service(AopService.java:10)
at com.spring.aop.AopService$$FastClassByCGLIB$$a6d02733.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:54)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
at com.spring.aop.AopService$$EnhancerByCGLIB$$e28809a7.service(<generated>)
at com.spring.SpringStart.main(SpringStart.java:11)