Spring-AOP的五种通知和切面的优先级、通知变量声明
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的五种通知和切面的优先级、通知变量声明的更多相关文章
- spring aop 的五种通知类型
本文转自:http://blog.csdn.net/cqabl/article/details/46965197 spring aop通知(advice)分成五类: 前置通知[Before advic ...
- spring aop的五种通知类型
昨天在腾讯课堂看springboot的视频,老师随口提问,尼玛竟然回答错了.特此记录! 问题: Spring web项目如果程序启动时出现异常,调用的是aop中哪类通知? 正确答案是: 异常返回通知. ...
- spring AOP的两种代理
本篇记录下spring AOP的两种代理,为下一篇AOP实现做下铺垫. 1.JDK动态代理 2.cglib代理 1.如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP2.如果目标对象 ...
- Spring AOP 学习(五)
1. 使用动态代理实现AOP package com.atguigu.spring.aop; import java.lang.reflect.InvocationHandler; import ja ...
- (一)spring aop的两种配置方式。
sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释. 一.基于注解的springAop配置 ...
- Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解
引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...
- spring AOP的两种配置方式
连接点(JoinPoint) ,就是spring允许你是通知(Advice)的地方,那可就真多了,基本每个方法的前.后(两者都有也行),或抛出异常是时都可以是连接点,spring只支持方法连接点.其他 ...
- spring ----> aop的两种实现方式
实现1:基于xml package com.rr.spring3.interf; //接口 public interface SayHello { public void sayHello(); } ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
随机推荐
- python 获取当前时间的用法
1.先导入库:import datetime 2.获取当前日期和时间:now_time = datetime.datetime.now() 3.格式化成我们想要的日期:strftime() 比如:“2 ...
- 【转】用python实现简单的文本情感分析
import jieba import numpy as np # 打开词典文件,返回列表 def open_dict(Dict='hahah',path = r'/Users/zhangzhengh ...
- DOM方法 getElementsByName()方法
http://www.imooc.com/code/1583 getElementsByName()方法 返回带有指定名称的节点对象的集合. 语法: document.getElementsByNam ...
- IIS短文件名泄露漏洞危害及防范方法(转)
攻击方法(转自http://blog.sina.com.cn/s/blog_64a3795a01017xqt.html) 一直在寻找一种方法,如果我可以使用通配符"*" 和 &qu ...
- jfinal中Interceptor的使用
一.拦截器是用于对action请求的拦截处理,发生在进入action方法体之前的拦截操作,这样方便了对请求实例做一些文章. 二.自定义.系统已有拦截器都需要实现Interceptor接口,这样才能 ...
- a标签去掉下划线
转载自:http://jingyan.baidu.com/article/a17d52853095838099c8f24e.html <a>是默认有下划线的.所以有时候为了美观,我们需要去 ...
- openvpn 移植之buildroot添加相关选项
openvpn 移植第一步,在buildroot 内添加 openssl ,openvpn , 另外还有一个 RSA 的支持,我不确定这个需要程度如何,但是也添加进去了. buildroot 添加相关 ...
- Android——Android Bundle类(转)
今天发现自己连Bundle类都没有搞清楚,于是花时间研究了一下. 根据google官方的文档(http://developer.android.com/reference/android/os/Bun ...
- jquery -- 同时监听多个事件
多个事件触发同一反应 $("#redrow").on("click touchend",function(){});//每个监听事件之间用 “空格” 隔开 多个 ...
- 【SPOJ】8222. Substrings(后缀自动机)
http://www.spoj.com/problems/NSUBSTR/ 题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.求F(1)..F(Length(S)) 这题 ...