AfterThrowing增强处理
使⽤@AfterThrowing Annotation 可以标注⼀个AfterThrowing增强,处理程序中未处理的异常
在使⽤@AfterThrowing Annotation时,可以指定以下两个属性
(1)pointcut:指定切⼊点对应的切⼊表达式
(2)throwing:功能是⼀个返回值形参名,增强处理定义的⽅法可以通过该形参名来访问⽬标⽅法中抛出的异常对象
public interface IUserDao {
public void save();
public void update();
public void delete();
}
@Repository
public class UserDaoIm implements IUserDao{
public void save() {
int i=5/0;
System.out.println("保存⽤户信息");
}
public void update() {
System.out.println("更新⽤户信息");
}
public void delete() {
System.out.println("删除⽤户信息");
}
}
@Aspect
public class Throw {
@AfterThrowing(throwing="ex",pointcut="execution(* save(..))")
public void dealException(Throwable ex){
System.out.println("⽬标函数抛出异常"+ex.getMessage());
}
}
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:component-scan base-package="annotation.**"></context:component-scan>
<bean id="advice" class="annotation.Advice"></bean>
<bean id="throw" class="annotation.Throw"></bean>
<!-- <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean> -->
public class Test {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("l");
//获取代理lei
IUserDao userDao=(Bean("userDaoIm");
userDao.save();
}
}
开始事务提交
⽬标函数抛出异常/ by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero
at annotation.UserDaoIm.save(UserDaoIm.java:10)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(Unknown Source)
flect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at flect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80) at annotation.Advice.adviceMethod(Advice.java:13)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(Unknown Source)
flect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at flect.Method.invoke(Unknown Source)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
unknown怎么处理at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy9.save(Unknown Source)
at annotation.Test.main(Test.java:13)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论