SpringAOP的通知分为以下五种:

1前置通知(@before)
在连接点执行之前执行的代码

2后置通知(@after)
在连接点执行之后执行的代码,不管连接点执行后是否出现异常,后置通知都会执行,但是不能访问连接点返回值

3返回通知
返回通知:就是可以获取连接点的返回值,
      当连接点执行之后,若没有异常,则执行返回通知,返回通知在后置通知执行后才会执行

4异常通知
在连接点执行的时候,若出现异常,则会执行异常通知,可以根据异常类型来定义执行对应的异常通知

5环绕通知
相当于一个动态代理,也就是说其动能包括了前面四个通知的功能,

示例代码:

package com.jeremy.aop.example;

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;
/*
* 使用AOP的步骤
* 1声明一个切面,切面必须是在IOC容器中,所以必须添加@component注解,而且还说明是一个切面,所以还要添加@Aspect注解
* 2定义通知:通知有五种,分别是前置通知、后置通知、返回通知、异常通知、环绕通知,根据业务需求定义所需的通知
* 3在通知里说明通知时由那个类的那个方法执行
* 4在Spring的配置文件自动扫描<context:component-scan>和配置AspectJ
* 配置AspectJ是当我们调用一个目标方法时,而这个目标方法跟我注解声明的方法相匹配的时候,那么AOP框架应该自动的为那个方法所在的那个类生成一个代理对象,
* 在我目标方法执行之前先来执行切面的对应的方法
*/ @Aspect
@Component
public class LoggingAspect {
@Before("execution(* com.jeremy.aop.example.ArithmeticCalculator.*(..))")
private void beforeMethod() {
System.out.println("the Method begins........"); } @After("execution(* com.jeremy.aop.example.ArithmeticCalculator.*(..))")
private void afterMethod() {
// TODO Auto-generated method stub
System.out.println("The Method after ................");
} @AfterReturning(value="execution(* com.jeremy.aop.example.ArithmeticCalculator.*(..))",returning="result") public void AfterReturning(JoinPoint joinPoint,Object result){ System.out.println("The Method after ................"+result);
} @AfterThrowing(value="execution(* com.jeremy.aop.example.ArithmeticCalculator.*(..))",throwing="e")
public void AfterThrowing(JoinPoint joinPoint,Exception e){
System.out.println("The Method afterThrowing ................"+e.getMessage().toString());
} @Around(value="execution(* com.jeremy.aop.example.ArithmeticCalculator.*(..))")
public Object aroundTest(ProceedingJoinPoint pjp){ String methodName=pjp.getSignature().getName();
Object object=null;
try { // 前置通知 System.out.println("The Method "+methodName+"before ................");
object=pjp.proceed();
//返回通知
System.out.println(object);
} catch (Throwable e) {
// TODO Auto-generated catch block
//异常通知
System.out.println(e.getMessage().toString());
}
System.out.println("The Method "+methodName+" after ................");
return object;
}
}

  

切面优先级:也就是切面运行的先后

      比如当我们一个系统涉及权限,日志,验证。。等等各种功能我们一般是需要先验证有没有这个权限,然后再打印日志,但是系统并不知道怎么去识别使用哪个切面

      所以我们就要硬性的规定一下优先使用哪个切面(使用@order注解既可以了)
示例:

    
     

声明切面表达式:在实际的开发中切面表达式可能被多个通知 重复利用,所以如果同意在一处声明就可能不用那么麻烦的去书写了
示例:

/**
* 定义一个方法, 用于声明切入点表达式. 一般地, 该方法中再不需要添入其他的代码.
* 使用 @Pointcut 来声明切入点表达式.
* 后面的其他通知直接使用方法名来引用当前的切入点表达式.
*/
@Pointcut("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")
public void declareJointPointExpression(){} /**
* 在 com.atguigu.spring.aop.ArithmeticCalculator 接口的每一个实现类的每一个方法开始之前执行一段代码
*/
@Before("declareJointPointExpression()")
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));
}

  

Spring-AOP的五种通知和切面的优先级、通知变量声明的更多相关文章

  1. spring aop 的五种通知类型

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

  2. spring aop的五种通知类型

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

  3. spring AOP的两种代理

    本篇记录下spring AOP的两种代理,为下一篇AOP实现做下铺垫. 1.JDK动态代理  2.cglib代理 1.如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP2.如果目标对象 ...

  4. Spring AOP 学习(五)

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

  5. (一)spring aop的两种配置方式。

    sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释. 一.基于注解的springAop配置 ...

  6. Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解

    引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...

  7. spring AOP的两种配置方式

    连接点(JoinPoint) ,就是spring允许你是通知(Advice)的地方,那可就真多了,基本每个方法的前.后(两者都有也行),或抛出异常是时都可以是连接点,spring只支持方法连接点.其他 ...

  8. spring ----> aop的两种实现方式

    实现1:基于xml package com.rr.spring3.interf; //接口 public interface SayHello { public void sayHello(); } ...

  9. 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

随机推荐

  1. UIScrollView 滚动视图—IOS开发

    转自:http://blog.csdn.net/iukey/article/details/7319314 UIScrollView 类负责所有基于 UIKit 的滚动操作. 一.创建 CGRect  ...

  2. Mac下Selenium无法最大化Chrome解决方案

    在用Selenium做自动化操作时,一般最大化浏览器的代码都是:driver.manage().window().maximize(), 但是在Mac下这样是无法最大化Chrome浏览器的,解决方法: ...

  3. python操作word

    python教程(百度经验) Python 操作Word(Excel.PPT等通用)   import win32comfrom win32com.client import Dispatch, co ...

  4. 上传绕过WAF几种常见的姿势

    1:WTS-WAF 绕过上传原内容:Content-Disposition: form-data; name="up_picture"; filename="xss.ph ...

  5. [转]PHP 5.3.0以上推荐使用mysqlnd驱动

    我们一般的使用场景,比较少关注PHP版本的问题,而且市面上提供的PHP运行环境都还是5.2系列的. 原文:http://zhangxugg-163-com.iteye.com/blog/1894990 ...

  6. [systemd]Linux系统启动之systemd

    参照:https://wiki.debian.org/systemd 最近在添加板子应用程序自启动的时候,发现在rcN.d中的符号链接并没有用,文件系统为Debian Jessie 8, 后来从同事那 ...

  7. sama5d3 环境检测 gpio--yk测试

    说明: gpio的MAP关系 yk0--pioA7  yk1--pioA5   yk2--pioA9   yk3--pioA3   yk4--pioA1  yk5--pioA8    (端子从左--& ...

  8. http协议之报文详解

    一. 概述 用于HTTP协议交互的信息被称为HTTP报文.请求端(客户端)的http报文叫做请求报文,响应端的叫做响应报文. 报文,是网络中交换和传输的数据单元,即站点一次性要发送的数据块.报文包含了 ...

  9. Spider Studio 新版本 (20131201) - BrowserManager / 节点选择器 / JQueryContext.Focus

    2013-12-1版本更新, 包含如下改动: 1. 修复BrowserManager重复初始化的bug; 2. 大幅提高节点选择器性能: 以网页 http://data.sports.sohu.com ...

  10. C++ 引用本质的详解

    //引用本质的理解① #include<iostream> using namespace std; int GetA(){ ; return a; } int & GetB(){ ...