您的位置:首页 > 编程语言 > Java开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐