AspectJ支持5种类型的通知注解:

  • @Before:前置通知,在方法执行之前执行;
  • @After:后置通知,在方法执行之后执行;
  • @AfterRunning:返回通知,在方法返回结果之后执行(因此该通知方法在方法抛出异常时,不能执行);
  • @AfterThrowing:异常通知,在方法抛出异常之后执行;
  • @Around:环绕通知,围绕着方法执行。

示例项目新建:

第一步:新建spring aop项目

第二步:添加spring-aop.xml Spring配置文件:

<?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/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.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.dx.spring.beans.aop"></context:component-scan>
<!-- 配置是AspectJ注解起作用 :自动为匹配的类生成代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

第三步:添加接口类IArithmeticCalculator.java,ArithmeticCalculatorImpl.java Spring组件

package com.dx.spring.beans.aop;

/**
* Description:Addition, subtraction, multiplication, and division
*/
public interface IArithmeticCalculator {
int add(int i, int j); int sub(int i, int j); int multi(int i, int j); int div(int i, int j);
}
package com.dx.spring.beans.aop;

import org.springframework.stereotype.Component;

@Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements IArithmeticCalculator {
@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 multi(int i, int j) {
int result = i * j;
return result;
} @Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}

第四步:添加LoggingAspect.java切面

package com.dx.spring.beans.aop;

import java.util.List;
import java.util.Arrays; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; //把这个类声明为一个切面:需要把该类放入到IOC容器中、再声明为一个切面。
@Aspect
@Component
public class LoggingAspect {
// 在这里注册通知方法。
}

第五步:添加测试类Client.java

package com.dx.spring.beans.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {
public static void main(String[] args) {
// 1:创建Spring的IOC容器;
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-aop.xml");
// 2.从IOC容器中获取Bean的实例
IArithmeticCalculator arithmeticCalculator = (IArithmeticCalculator) ctx.getBean("arithmeticCalculator");
// 3.使用Bean }
}

@Before:前置通知

在切面类LoggingAspect中添加前置通知:

    // 声明该方法为一个前置通知:在目标方法开始之前执行
@Before("execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(int, int))")
public void beforeMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println("before method " + methodName + " with " + args);
}

Client.java添加测试代码&执行测试:

        int result = arithmeticCalculator.add(1, 3);
System.out.println(result); result = arithmeticCalculator.div(4, 1);
System.out.println(result);

before method add with [1, 3]
before method div with [4, 0]

@After:后置通知

在切面类LoggingAspect中添加后置通知方法:

    // 声明该方法为一个后置通知:在目标方法结束之后执行(无论方法是否抛出异常)。
// 但是因为当方法抛出异常时,不能返回值为null,因此这里无法获取到异常值。
@After(value = "execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(int, int))")
public void afterMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println("after method " + methodName);
}

Client.java添加测试代码&执行测试:

        int result = arithmeticCalculator.add(1, 3);
System.out.println(result); result = arithmeticCalculator.div(4, 0);
System.out.println(result);

执行结果:

备注:从执行结果中我们可以看出,即使方法抛出异常,后置通知方法也会执行。

@AfterRunning:返回通知

修改切面类LoggingAspect,添加返回通知方法:

    /**
* 声明该方法为一个返回通知:在目标方法返回结果时后执行(当方法抛出异常时,无法执行)。
* 但是因为当方法抛出异常时,类似代码
* {@code
* try <br/>
* { <br/>
* // before 前置通知 <br/>
* // do action method <br/>
* // after returning 返回通知,可以访问到方法的返回值 <br/>
* }
* catch(Exception e){
* e.printStackTrace();
* // after throwing 异常通知,可以访问到方法出现的异常
* }
* // after 后置通知,因为方法可以抛出异常,所以访问不到方法的返回值
* }
* */
@AfterReturning(value = "execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(int, int))",
returning = "result")
public void afterReturningMethod(JoinPoint joinpoint, Object result) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println(
"after method " + methodName + " with returning " + (result == null ? "NULL" : result.toString()));
}

注意:返回通知注解中,参数是多一个了returning参数,该参数定义通知方法接收返回值的别名。

在测试类Client.java中追加测试代码:

        int result = arithmeticCalculator.add(1, 3);
System.out.println(result); result = arithmeticCalculator.div(4, 0);
System.out.println(result);

@AfterThrowing:异常通知

修改切面类LoggingAspect,添加异常通知方法:

    /**
* 定义一个异常通知函数:
* 只有在方法抛出异常时,该方法才会执行,而且可以接受异常对象。
* */
@AfterThrowing(value = "execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(int, int))", throwing = "ex")
public void afterThrowingMethod(JoinPoint joinpoint, Exception ex) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println(
"after method " + methodName + " occurs exception: " + (ex == null ? "NULL" : ex.getMessage()));
}

备注:只有在方法抛出异常时,异常通知方法才会执行,而且可以接受异常对象。

在测试类Client.java中追加测试代码:

        int result = arithmeticCalculator.add(1, 3);
System.out.println(result); result = arithmeticCalculator.div(4, 0);
System.out.println(result);

打印信息

@Around:环绕通知

环绕通知其他方式不同,它相当于代理对象使用时效果一样。可以在方法前、后、返回、异常打印信息。

我们先看代理方法打印信息时如何处理,之后再看环绕通知如何处理。

代理方法实现通知:

package com.dx.spring.beans.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; public class ArithmeticCalculatorProxy implements InvocationHandler {
private Object obj; public ArithmeticCalculatorProxy(Object obj) {
this.obj = obj;
} public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object object = null;
try {
// *** 前置通知,在方法执行之前执行
System.out.println("before " + method); // 真实的调用方法操作
object = method.invoke(obj, args); // *** 返回通知,在方法返回结果之后执行(因此该通知方法在方法抛出异常时,不能执行);
System.out.println("before returning " + method);
} catch (Exception ex) {
ex.printStackTrace();
// *** 异常通知,在方法抛出异常之后执行;
System.out.println("before throwing " + method);
throw ex;
}
// *** 后置通知,在方法执行之后执行;因为方法可以抛出异常,所以访问不到方法的返回值
System.out.println("after " + method); return object;
}
}

环绕通知:

修改切面类LoggingAspect,添加环绕通知:

package com.dx.spring.beans.aop;

import java.util.List;
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; //把这个类声明为一个切面:需要把该类放入到IOC容器中、再声明为一个切面。
@Aspect
@Component
public class LoggingAspect {
// // 声明该方法为一个前置通知:在目标方法开始之前执行
// @Before("execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(int, int))")
// public void beforeMethod(JoinPoint joinpoint) {
// String methodName = joinpoint.getSignature().getName();
// List<Object> args = Arrays.asList(joinpoint.getArgs());
// System.out.println("before method " + methodName + " with " + args);
// }
//
// // 声明该方法为一个后置通知:在目标方法结束之后执行(无论方法是否抛出异常)。
// // 但是因为当方法抛出异常时,不能返回值为null,因此这里无法获取到异常值。
// @After(value = "execution(public intcom.dx.spring.beans.aop.IArithmeticCalculator.*(int,int))")
// public void afterMethod(JoinPoint joinpoint) {
// String methodName = joinpoint.getSignature().getName();
// List<Object> args = Arrays.asList(joinpoint.getArgs());
// System.out.println("after method " + methodName);
// }
//
// /**
// * 声明该方法为一个返回通知:在目标方法返回结果时后执行(当方法抛出异常时,无法执行)。 但是因为当方法抛出异常时,类似代码 {@code
// * try <br/>
// * { <br/>
// * // before 前置通知 <br/>
// * // do action method <br/>
// * // after returning 返回通知,可以访问到方法的返回值 <br/>
// * } catch(Exception e){ e.printStackTrace(); // after throwing
// * 异常通知,可以访问到方法出现的异常 } // after 后置通知,因为方法可以抛出异常,所以访问不到方法的返回值 }
// */
// @AfterReturning(value = "execution(public intcom.dx.spring.beans.aop.IArithmeticCalculator.*(int,int))", returning = "result")
// public void afterReturningMethod(JoinPoint joinpoint, Object result) {
// String methodName = joinpoint.getSignature().getName();
// List<Object> args = Arrays.asList(joinpoint.getArgs());
// System.out.println(
// "after method " + methodName + " with returning " + (result == null ? "NULL" : result.toString()));
// }
//
// /**
// * 定义一个异常通知函数: 只有在方法跑吹异常时,该方法才会执行,而且可以接受异常对象。
// */
// @AfterThrowing(value = "execution(public intcom.dx.spring.beans.aop.IArithmeticCalculator.*(int,int))", throwing = "ex")
// public void afterThrowingMethod(JoinPoint joinpoint, Exception ex) {
// String methodName = joinpoint.getSignature().getName();
// List<Object> args = Arrays.asList(joinpoint.getArgs());
// System.out.println(
// "after method " + methodName + " occurs exception: " + (ex == null ? "NULL" : ex.getMessage()));
// } @Around(value = "execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(int, int))")
public Object aroundMethod(ProceedingJoinPoint pJoinPoint) throws Exception {
String methodName = pJoinPoint.getSignature().getName();
List<Object> args = Arrays.asList(pJoinPoint.getArgs()); Object result = null;
try {
// 前置通知
System.out.println("before method " + methodName + " with " + args);
// 执行目标方法
result = pJoinPoint.proceed();
// 返回通知
System.out.println("after method " + methodName + " returning " + result);
} catch (Throwable ex) {
// 异常通知
System.out.println("after method " + methodName + " occurs exception: " + ex.getMessage());
throw new Exception(ex);
}
// 后置通知
System.out.println("after method " + methodName);
return result;
}
}

修改Client.java测试类,添加代码:

        int result = arithmeticCalculator.add(1, 3);
System.out.println(result); result = arithmeticCalculator.div(4, 0);
System.out.println(result);

执行结果

Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)的更多相关文章

  1. AOP 环绕通知 集成了前置 后置 返回通知等功能

    AOP 环绕通知 集成了前置 后置 返回通知等功能

  2. Jmeter(十八) - 从入门到精通 - JMeter后置处理器 -下篇(详解教程)

    1.简介 后置处理器是在发出“取样器请求”之后执行一些操作.取样器用来模拟用户请求,有时候服务器的响应数据在后续请求中需要用到,我们的势必要对这些响应数据进行处理,后置处理器就是来完成这项工作的.例如 ...

  3. Spring AOP前置通知和后置通知

    Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...

  4. spring 切面 前置后置通知 环绕通知demo

    环绕通知: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  5. Spring Bean前置后置处理器的使用

    Spirng中BeanPostProcessor和InstantiationAwareBeanPostProcessorAdapter两个接口都可以实现对bean前置后置处理的效果,那这次先讲解一下B ...

  6. NeHe OpenGL教程 第十八课:二次几何体

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  7. spring3: Aspectj后置返回通知

    Aspectj后置返回通知 接口: package chapter1.server; public interface IHelloService2 { public int sayAfterRetu ...

  8. pytest_前置后置

    今天总结下pytest,pytest简直就是python自动化中的高富帅,各种操作,哈哈 这次总结主要涉及到了以下几点: 1.unittest中的setUp.tearDown.setUpClass.t ...

  9. unittest的前置后置,pytest的fixture和共享机制conftest.py

    Unittest setUp/tearDown setUp当中得到的变量,用self.xxx = value传递给测试用例 setUpClass/tearDownClass setupClass当中得 ...

  10. [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

随机推荐

  1. mysql|表row_format的静态与动态,Compact

    innodb 一般对应 Compact  ,MyISAM 一般对应静态与动态 mysql中若一张表里面存在varchar.text以及其变形.blob以及其变形的字段的话,那么这个表其实也叫动态表,即 ...

  2. 使用 IntraWeb (14) - 基本控件之 TIWHRule、TIWRectangle

    TIWHRule //一条横线, 对应 Html 中的 <hr/> TIWRectangle //矩形; 中间可以有行文本, 文本可任意对齐 TIWHRule 所在单元及继承链: IWHT ...

  3. 如何用万用表判断一个12V蓄电池是否没电

    常用的方法如下: 1.找一根细铜丝,接触电瓶的正负极柱,冒火花说明电瓶有电,不冒火花说明没有电. 2.用万用表测量电瓶的电压,12.7V说明满电,11.50V一下说明没有电. 3.找一个12V的灯泡或 ...

  4. [原创]互联网金融App测试介绍

    [原创]互联网金融App测试介绍 前端时间非常忙,终于非常忙的时间过去了,抽时间总结下我现在所在公司理财软件App测试,也各位分享下,也欢迎大家提建议,谢谢! 先介绍下我所在公司的产品特点,公司所研发 ...

  5. 【Go命令教程】2. go build

    go build 命令用于编译我们 指定的  源码文件 或 代码包 以及它们的依赖包. 例如,如果我们在执行 go build 命令时不后跟任何代码包,那么命令将试图编译当前目录所对应的代码包.例如, ...

  6. ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket

    在安装好了MySQL之后,使用了新的配置文件后.MySQL服务器能够成功启动,但在登陆的时候出现了ERROR 2002 (HY000): Can't connect to local MySQL se ...

  7. win10镜像重装,快速设置之后无限重启怎么办?

    我得操作过程: 1.下载老毛桃最win8pe装机版 2.安装老毛桃到U盘, 3.格式化U盘,用extFat格式,拷贝win10镜像文件(你也可以拷贝到硬盘上) 4.插入电脑,通过U盘启动,进入PE 5 ...

  8. cmd 递归删除目录或文件

    递归删目录 for /r <TARGET DIR> %i in (<DIR NAME or Pattern>) do rd /s /q %i 递归删文件 for /r < ...

  9. AutoMapper在MVC中的运用05-映射中的忽略、处理null、多种映射转换

    本篇AutoMapper使用场景: ※ 动态实现接口方法或属性 ※ 目标中的属性如果比源多,可以忽略多出的属性 ※ 目标有virtual属性,可忽略 ※ 目标属性值为null的解决办法 ※ int转s ...

  10. Android之 MTP框架和流程分析

    概要 本文的目的是介绍Android系统中MTP的一些相关知识.主要的内容包括:第1部分 MTP简介            对Mtp协议进行简单的介绍.第2部分 MTP框架            介绍 ...