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("annotation/beam.xml");
		//获取代理lei
		IUserDao userDao=(IUserDao)context.getBean("userDaoIm");
		userDao.save();
	}
}

 

开始事务提交
目标函数抛出异常/ by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at annotation.UserDaoIm.save(UserDaoIm.java:10)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.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)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.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)
	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)

 

你可能感兴趣的:(AFTER)