Spring 返回通知&异常通知&环绕通知(注解)
2018-01-02 16:26
495 查看
public interface ArithmeticCalculator { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); } @Component("arithmeticCalculator") public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override public int add(int i, int j) { int result = i + j; return result; } @Override public int sub(int i, int j) { int result = i - j; return result; } @Override public int mul(int i, int j) { int result = i * j; return result; } @Override public int div(int i, int j) { int result = i / j; return result; } } package com.spring.aop.Impl; import java.util.Arrays; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { /** * 在 com.spring.aop.Impl.ArithmeticCalculator 接口的每一个实现类的每一个方法开始之前执行一段代码. */ @Before("execution(* com.spring.aop.Impl.*.*(..))") public void beforeMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); System.out.println("The Method " + methodName + " begins with " + Arrays.asList(args)); } /** * 在方法执行之后执行的代码. 无论该方法是否出现异常 */ @After("execution(* com.spring.aop.Impl.*.*(..))") public void afterMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); System.out.println("The Method " + methodName + " ends"); } /** * 在方法正常结束后执行的代码 * 返回通知是可以访问到方法的返回值的 */ //返回通知 @AfterReturning(value="execution(* com.spring.aop.Impl.*.*(..))",returning="result") public void afterReturning(JoinPoint joinPoint,Object result){ String methodName = joinPoint.getSignature().getName(); System.out.println("The Method " + methodName + " ends with " + result); } /** * 在目标方法出现异常时, 会执行的代码. * 可以访问到异常对象, 且可以指定再出现特定异常时再执行通知代码 */ @AfterThrowing(value="execution(* com.spring.aop.Impl.*.*(..))", throwing="ex") public void afterThrowing(JoinPoint joinPoint, Exception ex){ String methodName = joinPoint.getSignature().getName(); System.out.println("The Method " + methodName + " occurs excetion: " + ex); } /** * 环绕通知需要携带 ProceedingJoinPoint 类型的参数. * 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法 * 且环绕通知必须有返回值, 返回值即为目标方法的返回值. */ @Around("execution(* com.spring.aop.Impl.*.*(..))") public Object aroundMethod(ProceedingJoinPoint joinPoint){ Object result = null; String methodName = joinPoint.getSignature().getName(); //执行目标方法 try { //前置通知 System.out.println("The method " + methodName + " begins with " + Arrays.asList(joinPoint.getArgs())); //执行目标方法 result = joinPoint.proceed(); //后置通知 System.out.println("The method " + methodName + " ends with " + result); } catch (Throwable e) { //异常通知 System.out.println("The method " + methodName + " occurs exception: " + e); throw new RuntimeException(e); } //后置通知 System.out.println("The method " + methodName + " ends"); return result; } } public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-aop-config.xml"); ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class); int add = arithmeticCalculator.add(1, 2); System.out.println("Result: " + add); add = arithmeticCalculator.div(1000, 0); System.out.println("Result: " + add); } } <?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.spring.aop.Impl"></context:component-scan> <!-- 配置自动为匹配 aspectJ 注解的 Java 类生成代理对象 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
2018-1-2 16:26:32 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@22c84d9: startup date [Tue Jan 02 16:26:32 CST 2018]; root of context hierarchy 2018-1-2 16:26:32 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [spring-aop-config.xml] The method add begins with [1, 2] The Method add begins with [1, 2] The method add ends with 3 The method add ends The Method add ends The Method add ends with 3 Result: 3 The method div begins with [1000, 0] The Method div begins with [1000, 0] The method div occurs exception: java.lang.ArithmeticException: / by zero The Method div ends The Method div occurs excetion: java.lang.RuntimeException: java.lang.ArithmeticException: / by zero Exception in thread "main" java.lang.RuntimeException: java.lang.ArithmeticException: / by zero at com.spring.aop.Impl.LoggingAspect.aroundMethod(LoggingAspect.java:81) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) 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:68) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:43) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at $Proxy11.div(Unknown Source) at com.spring.aop.Impl.Main.main(Main.java:14) Caused by: java.lang.ArithmeticException: / by zero at com.spring.aop.Impl.ArithmeticCalculatorImpl.div(ArithmeticCalculatorImpl.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85) at com.spring.aop.Impl.LoggingAspect.aroundMethod(LoggingAspect.java:75) ... 19 more
相关文章推荐
- Spring AOP学习笔记(3):AOP返回通知&异常通知&环绕通知
- 19Spring返回通知&异常通知&环绕通知
- Spring_17-19前置通知&后置通知&返回通知&异常通知&环绕通知
- Spring 通过来AOP 实现前置,环绕,异常通知,注解
- 13.返回通知&异常通知&环绕通知
- Spring 通过AOP 来实现前置,环绕,异常通知,注解
- Spring通知(前置通知,后置通知,返回通知,异常通知,环绕通知)
- 基于注解的返回通知、异常通知、环绕通知-佟刚老师《Spring4视频教程》学习笔记(19)
- spring的返回通知、异常通知、环绕通知、切面的优先级与重用切点表达式
- spring学习笔记(17)——返回通知&异常通知&环绕通知
- [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知
- Spring AOP--返回通知,异常通知和环绕通知(顶)
- Spring AOP--返回通知,异常通知和环绕通知
- Spring 通过来AOP 实现前置,环绕,异常通知,注解(转)
- Spring返回通知、异常通知和环绕通知
- Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理
- Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理
- Spring 基于注解,实现 前置通知、后置通知、返回通知、 异常通知 、后置通知
- spring_AOP前置通知&后置通知&返回通知&环绕通知
- Spring AOP--返回通知,异常通知和环绕通知