一、通知介绍

1. 前置通知(Before)

  在目标方法执行之前执行的通知。

  前置通知方法,可以没有参数,也可以额外接收一个JoinPoint,Spring会自动将该对象传入,代表当前的连接点,通过该对象可以获取目标对象 和 目标方法相关的信息。

  注意,如果接收JoinPoint,必须保证其为方法的第一个参数,否则报错。

2. 环绕通知(Around)

  在目标方法执行之前和之后都可以执行额外代码的通知。

  在环绕通知中必须显式的调用目标方法,目标方法才会执行,这个显式调用是通过ProceedingJoinPoint来实现的,可以在环绕通知中接收一个此类型的形参,spring容器会自动将该对象传入,注意这个参数必须处在环绕通知的第一个形参位置。

  注:只有环绕通知可以接收ProceedingJoinPoint,而其他通知只能接收JoinPoint。环绕通知需要返回返回值,否则真正调用者将拿不到返回值,只能得到一个null。

  环绕通知有控制目标方法是否执行、有控制是否返回值、有改变返回值的能力。环绕通知虽然有这样的能力,但一定要慎用,不是技术上不可行,而是要小心不要破坏了软件分层的“高内聚 低耦合”的目标。

3. 后置返回通知(AfterReturning)

  在目标方法正常执行完成后执行,如果目标方法抛出异常,则不会执行。

  在后置通知中也可以选择性的接收一个JoinPoint来获取连接点的额外信息,但是这个参数必须处在参数列表的第一个。

4. 最终通知(After)

  在目标方法执行完成后执行,不管是正常执行完成,还是抛出异常都会执行

  最终通知无法得到返回值

  最终通知也可以额外接收一个JoinPoint参数,来获取目标对象和目标方法相关信息,但一定要保证必须是第一个参数。

5. 异常通知(AfterThrowing)

  在目标方法抛出异常时执行的通知。

  可以配置传入JoinPoint获取目标对象和目标方法相关信息,但必须处在参数列表第一位。

  另外,还可以配置参数,让异常通知可以接收到目标方法抛出的异常对象。

二、基于XML配置的通知

xml配置:

<!-- 注册bean 和 切面bean -->
<bean id="cola" class="top.demo.xmlaop.Cola"></bean>
<bean id="aopim" class="top.demo.xmlaop.AopIm"></bean> <!--aop:config标签用来配置有关切面的配置 -->
<aop:config>
<!-- 设置切点表达式 以便下面引用 -->
<aop:pointcut expression="execution(* top.demo.xmlaop.Cola.Open(int))" id="cut"/>
<!-- 配置切面所用的bean 和优先级 -->
<aop:aspect ref="aopim" order="1" >
<!-- 配置切面方法 -->
<aop:before method="beforeCheck" pointcut-ref="cut"/>
<aop:after method="afterCheck" pointcut-ref="cut"/>
<aop:after-returning method="afterReturn" pointcut-ref="cut" returning="res"/>
<aop:after-throwing method="afterThrow" pointcut-ref="cut" throwing="ex"/>
<aop:around method="around" pointcut-ref="cut"/>
</aop:aspect> </aop:config>
public class AopIm {

    /*
* 前置通知
* 注意 可以使用通配符 public void替换成* 表示任何的权限修饰符和返回值 等等等等 。。。。。
* 参数替换成..可以表示不限参数的匹配
* */
public void beforeCheck(JoinPoint joinPoint) {
Signature sig=joinPoint.getSignature(); System.out.println("before at "+sig.getName()+"and arg[0] is "+joinPoint.getArgs()[0]); } /*
*后置通知
*无法获取返回值 。可以通过返回通知获取返回值
*且后置通知无论方法是不是异常都会执行
* */
public void afterCheck(JoinPoint joinPoint) {
Signature sig=joinPoint.getSignature(); System.out.println("After at "+sig.getName()+"and arg[0] is "+joinPoint.getArgs()[0]); } /*
* 返回通知
* */
public void afterReturn(JoinPoint joinPoint,Object res) {
Signature sig=joinPoint.getSignature();
System.out.println("After at "+sig.getName()+"return. res= "+res); } /*
* 异常通知
* 注意如果这个方法的参数:假设异常类型为数学除零的异常
* afterThrow(JoinPoint joinPoint,空指针异常类 ex) 然后我这里写了空指针异常
* 那afterThrow这个方法就执行不了 因为类型不对
* */
public void afterThrow(JoinPoint joinPoint,Exception ex) { Signature sig=joinPoint.getSignature();
System.out.println("After at "+sig.getName()+"Throw. message= ");
System.out.println(ex.getMessage());
} /*
* 环绕通知
* 环绕通知就等于整个代理过程交给你自己处理 连被代理对象的要执行的目标方法要不要执行也取决你
* 上面几个通知比较像 处理目标方法调用的某个时刻的 处理过程
* */
public Object around(ProceedingJoinPoint pJoinPoint) { Object res=null;
String methodName=pJoinPoint.getSignature().getName(); System.out.println(methodName+" 执行前(前置通知)");
try { res=pJoinPoint.proceed();
System.out.println(methodName+" 执行后有结果(返回通知)");
} catch (Throwable e) { System.out.println("异常通知 "+e.getMessage());
}
System.out.println(methodName+" 执行后(后置通知)");
return res;
} }

三、基于注解配置的通知

xml配置:

<!-- 配置自动扫描包 -->
<context:component-scan base-package="spring.aop.helloworld"/> <!-- 让aspectj注解起作用: 自动为匹配的类生成代理对象 -->
<aop:aspectj-autoproxy/>
/*
**
** @Aspect:声明式一个切面
** @Component:加入到ioc容器中
** @Order(2):如果在不同的切面中定义多个通知响应同一个切点,为了控制执行顺序,可以用该注解解决。参数(int 类型)越小优先级越大,越先执行。
** 也可以实现org.springframework.core.Ordered 接口,重写getOrder方法。
**
*/ @Order(2)
@Aspect
@Component
public class LoggingAspect {
/**
* 定义一个方法,用于声明切入点表达式,一般的,该方法中不需要再添加其他的代码。 使用@Pointcut来声明切入点表达式
* 后面的其他通知直接使用方法名来引用当前的切入点表达式
*/
@Pointcut("execution(public int spring.aop.helloworld.ArithmeticCalculator.*(int, int))")
public void declareJoinPointExpression() {
} // 声明该方法是一个前置通知:在目标方法开始之前执行
@Before("declareJoinPointExpression()")
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The " + methodName + " begins " + " args is: " + args);
} // 后置通知:在目标方法发生之后执行(不论这个方法是否发生了异常)
// 在后置通知中海不能访问目标方法执行的结果
@After("declareJoinPointExpression()")
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The " + methodName + " ends " + methodName + " args is: " + args);
} // 返回通知
@AfterReturning(value = "declareJoinPointExpression()", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The " + methodName + " ends " + methodName + " return is: " + result);
} // 在目标方法出现异常时,会执行的代码,可以访问到异常的对象,且可以指定在发生特定异常时才会打印
@AfterThrowing(value = "declareJoinPointExpression()", throwing = "ex")
public void afterThrowing(JoinPoint joinPoint, Exception ex) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The " + methodName + " occurs exception " + ex);
} /**
* 环绕通知需要携带ProceedingJoinPoint类型的参数 环绕通知类似于动态代理的全过程: ProceedingJoinPoint
* 类型的参数可以决定是否执行目标方法 且环绕通知必须有返回值,返回值即为目标方法的返回值
*
* @param pjd
*/
@Around("execution(public int spring.aop.helloworld.ArithmeticCalculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pjd) {
Object result = null;
String methodName = pjd.getSignature().getName();
try {
// 前置通知
System.out
.println("@Around before ----> The " + methodName + " begin with " + Arrays.asList(pjd.getArgs()));
result = pjd.proceed();
// 返回通知
System.out.println("@Around return ----> The " + methodName + " return with " + result);
} catch (Throwable e) {
System.out.println("@Around exception ----> The " + methodName + " occurs exception " + e);
throw new RuntimeException(e);
}
// 后置通知
System.out.println("@Around after ----> The " + methodName + " ends");
return result;
}
}

【Spring AOP】通知(五)的更多相关文章

  1. spring aop的五种通知类型

    昨天在腾讯课堂看springboot的视频,老师随口提问,尼玛竟然回答错了.特此记录! 问题: Spring web项目如果程序启动时出现异常,调用的是aop中哪类通知? 正确答案是: 异常返回通知. ...

  2. spring aop 的五种通知类型

    本文转自:http://blog.csdn.net/cqabl/article/details/46965197 spring aop通知(advice)分成五类: 前置通知[Before advic ...

  3. Spring学习(十五)----- Spring AOP通知实例 – Advice

    Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...

  4. Spring AOP通知实例 – Advice

    Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...

  5. Spring AOP 学习(五)

    1. 使用动态代理实现AOP package com.atguigu.spring.aop; import java.lang.reflect.InvocationHandler; import ja ...

  6. Spring系列26:Spring AOP 通知与顺序详解

    本文内容 如何声明通知 如何传递参数到通知方法中 多种通知多个切面的通知顺序 多个切面通知的顺序源码分析与图解 声明通知 Spring中有5种通知,通过对应的注解来声明: @BeforeBefore ...

  7. Spring Aop(五)——给Advice传参

    转发:https://www.iteye.com/blog/elim-2395337 5 给Advice传递参数 Advice除了可以接收JoinPoint(非Around Advice)或Proce ...

  8. Spring基础——AOP通知

    spring(AOP通知) 切面 切面是封装通用业务逻辑的组件,可以作用到其他组件上.是spring组件中的某个方法.无返回类型.参数类型与通知类型有关.一个切面 开启数据库 关闭数据库 开启事务 检 ...

  9. 尚硅谷spring aop详解

    spring的aop实现我们采用AspectJ的方式来实现,不采用spring框架自带的aop aspect实现有基于注解的方式,有基于xml的方式,首先我们先讲基于注解的方式,再将基于xml的方式 ...

  10. Spring学习(十六)----- Spring AOP实例(Pointcut(切点),Advisor)

    在上一个Spring AOP通知的例子,一个类的整个方法被自动拦截.但在大多数情况下,可能只需要一种方式来拦截一个或两个方法,这就是为什么引入'切入点'的原因.它允许你通过它的方法名来拦截方法.另外, ...

随机推荐

  1. Python socket & socket server

    socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket(套接字). 建立网络通信连接至少要一对socket.socket是对TCP/IP的封装 使用方法 ...

  2. appium--python启动appium服务

    前戏 前面我们都是在cmd下通过输入appium加端口号来启动服务的,在我们做自动化的时候,我们当然不希望我们手动启动appium服务,而是希望通过脚本自动启动appium服务. 我们可以使用subp ...

  3. SDOI2019退役记

    \(update:2019-8-15\) 这个坑占了四个月,不填上终归不太好,也不甘心.日子久了,记不太清了,您就当瞧个乐吧. \(Day \ 0\) 下午来到山师大领准考证,晚上一点前就睡了觉. \ ...

  4. P1验证性内容

    #include<stdio.h> int main() { printf("201983270555"); return 0; } #include<stdio ...

  5. JavaScript 数据结构与算法之美 - 非线性表中的树、堆是干嘛用的 ?其数据结构是怎样的 ?

    1. 前言 想学好前端,先练好内功,内功不行,就算招式练的再花哨,终究成不了高手. 非线性表(树.堆),可以说是前端程序员的内功,要知其然,知其所以然. 笔者写的 JavaScript 数据结构与算法 ...

  6. AOP软件设计

    什么是面向方面的编程? 为什么面向方面的软件设计? 术语 关注 视口 关注点分离 人工制品 横切 方面 编织 零件 形式主义 第二节 案例研究 关注 人工制品 横切 方面 AspectJ 加入点 切入 ...

  7. Git仓库迁移命令

    1. 从原git上clone bare下到本地 git clone --bare https://***.git 2. push mirror到目标仓库 git push --mirror https ...

  8. ubuntu 16.04 上编译和安装C++机器学习工具包mlpack并编写mlpack-config.cmake | tutorial to compile and install mplack on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/1cd6a04d/,欢迎阅读最新内容! tutorial to compile and install mplack on ubun ...

  9. VMware workstation 12虚拟机安装CentOS7详细安装教程

    虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统. 虚拟系统通过生成现有操作系统的全新虚拟镜像,它具有真实windows系统完全 ...

  10. Spring Cloud Sleuth+ZipKin+ELK服务链路追踪(七)

    序言 sleuth是spring cloud的分布式跟踪工具,主要记录链路调用数据,本身只支持内存存储,在业务量大的场景下,为拉提升系统性能也可通过http传输数据,也可换做rabbit或者kafka ...