Spring AOP基础
AOP基本术语
Advice-通知
Before
前置通知,目标方法被调用前调用
After
后置通知,目标方法完成后调用通知,并不关心方法的输出是什么
After-returning
目标方法成功执行后调用
After-throwing
目标方法抛出异常后调用
Around
通知包裹了被通知的方法,在被通知的方法调用前和调用后执行自定义行为
Pointcut-切点
定义哪些方法是需要被通知的
Aspect-切面
Advice+Pointcut=Aspect
Join Point-连接点
应用执行过程中能够插入切面的时间点
如:抛出异常时、调用方法时......
Introduction-引入
向现有类添加新的方法或属性
Weaving-织入
把切面应用到目标对象并创建新的代理对象
切点详解
Spring的AOP中,使用AspectJ切点表达式来定义切点
Spring仅支持AspectJ切点指示器的一个子集
arg()
指定方法的参数类型
@args()
指定方法的参数由指定注解标注
execution()
指定方法,方法由切点表达式描述 (* 包名.类名.方法名(参数))
- 表示返回任意类型
参数如果是 .. ,表示任意参数
within()
指定方法类型
@within()
指定方法的注解类型
@annotation()
指定方法带有指定注解
其他
bean()
bean("beanId")-在指定bean中生效
!bean("beanId")-在指定bean中不生效
切点之间可以使用 &&、||、!连接,如果在xml中描述,使用 and、or、not
定义切面
package com.zln.aop;
import org.aspectj.lang.annotation.*;
/**
* 定义切面
* Created by sherry on 17/3/9.
*/
@Aspect
public class Audience {
/**
* 切点
*/
@Pointcut("execution(* com.zln.aop.Performance.*(..))")
public void performance(){}
/**
* 前置通知
*/
@Before("performance()")
public void silenceCellPhones(){
System.out.println("前置通知:表演前手机静音");
}
@Before("performance()")
public void takeSeats(){
System.out.println("前置通知:就坐");
}
@AfterReturning("performance()")
public void applause(){
System.out.println("返回通知:表演结束,鼓掌");
}
@AfterThrowing("performance()")
public void demandRefund(){
System.out.println("异常通知:表演失败");
}
}
package com;
import com.zln.aop.Audience;
import org.springframework.context.annotation.*;
/**
* Created by sherry on 17/3/9.
*/
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class AppBeans {
@Bean
public Audience audience(){
return new Audience();
}
}
运行测试
package com.zln.aop;
import org.springframework.stereotype.Component;
/**
* Created by sherry on 17/3/9.
*/
@Component
public class Performance{
public void perform() {
System.out.println("正在表演");
}
}
环绕通知
package com.zln.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
/**
* 定义切面
* Created by sherry on 17/3/9.
*/
@Aspect
public class Audience {
/**
* 切点
*/
@Pointcut("execution(* com.zln.aop.Performance.*(..))")
public void performance(){}
/**
* 前置通知
*/
@Before("performance()")
public void silenceCellPhones(){
System.out.println("前置通知:表演前手机静音");
}
@Before("performance()")
public void takeSeats(){
System.out.println("前置通知:就坐");
}
@AfterReturning("performance()")
public void applause(){
System.out.println("返回通知:表演结束,鼓掌");
}
@AfterThrowing("performance()")
public void demandRefund(){
System.out.println("异常通知:表演失败");
}
@Around("performance()")
public void watch(ProceedingJoinPoint proceedingJoinPoint){
System.out.println("环绕通知1");
try {
proceedingJoinPoint.proceed();
System.out.println("环绕通知2");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
环绕通知的proceedingJoinPoint.proceed();比较神奇,如果没有这句,相当于代码阻塞,如果调用多次,相当于执行多次目标方法
通知的方法参数
package com.zln.aop;
import org.springframework.stereotype.Component;
/**
* Created by sherry on 17/3/9.
*/
@Component
public class Performance{
public void perform(int i) {
System.out.println("正在表演");
}
}
//如果不是int等基本类型,要使用类的全限定名
@Pointcut("execution(* com.zln.aop.Performance.*(int))")
public void performance(){}
/**
* 前置通知
*/
@Before("performance()&&args(i)")
public void silenceCellPhones(int i){
System.out.println("前置通知:表演前手机静音"+i);
}
调用perform方法的时候,参数i的值就会被获取到
Spring AOP基础的更多相关文章
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
- [Spring框架]Spring AOP基础入门总结一.
前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...
- CgLib动态代理学习【Spring AOP基础之一】
如果不了解JDK中proxy动态代理机制的可以先查看上篇文章的内容:Java动态代理学习[Spring AOP基础之一] 由于Java动态代理Proxy.newProxyInstance()的时候会发 ...
- 【AOP】Spring AOP基础 + 实践 完整记录
Spring AOP的基础概念 ============================================================= AOP(Aspect-Oriented Pr ...
- Spring AOP基础知识
Spring AOP使用动态代理技术在运行期织入增强的代码,两种代理机制包括:一是基于JDK的动态代理,另一种是基于CGLib的动态代理.之所以需要两种代理机制,很大程度上是因为JDK本身只提供接口的 ...
- Java动态代理学习【Spring AOP基础之一】
Spring AOP使用的其中一个底层技术就是Java的动态代理技术.Java的动态代理技术主要围绕两个类进行的 java.lang.reflect.InvocationHandler java.la ...
- Spring AOP基础概念及自定义注解式AOP初体验
对AOP的理解开始是抽象的,看到切点的匹配方式其实与正则表达式性质大致一样就基本了解AOP是基本是个什么作用了.只是整个概念更抽象,需要具化理解.下图列表是AOP相关概念解释,可能也比较抽象^_^ 比 ...
- Spring Aop基础总结
什么是AOP: Aop技术是Spring核心特性之中的一个,定义一个切面.切面上包括一些附加的业务逻辑代码.在程序运行的过程中找到一个切点,把切面放置在此处,程序运行到此处时候会运行切面上的代码.这就 ...
- Spring AOP小记
一.概述 在通常的开发过程中,我们调用的顺序通常是controller->service-dao,其中,service中包含着太多的业务逻辑,并且还要不断调用dao来实现自身的业务逻辑,经常会导 ...
随机推荐
- 20155322 2017-2018-1《信息安全系统设计》第十周 课下作业-IPC
20155322 2017-2018-1<信息安全系统设计>课下作业-IPC 作业内容 研究Linux下IPC机制:原理,优缺点,每种机制至少给一个示例,提交研究博客的链接. 共享内存 管 ...
- day2 Ubuntu配置源
1.寻找国内镜像源 https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ 2.配置source list源 sources.list系统自带的,源是来U ...
- 非Contorller类使用@Service中的方法
组件扫描这种的是指bean,跟service没关系 service只能在Controller类中使用,如果别的类想使用,必须使用下面这种方法 内容来源:https://blog.csdn.net/u0 ...
- bootstrap的Alerts中 可以放置p标签 设置 align="center" 用来设置文本居中
效果
- Airflow使用入门指南
Airflow能做什么 关注公众号, 查看更多 http://mp.weixin.qq.com/s/xPjXMc_6ssHt16J07BC7jA Airflow是一个工作流分配管理系统,通过有向非循环 ...
- Redash二次开发-开发环境搭建
环境:win7+pycharm 2018.2 +redash 1.安装pycharm并如何正常使用,找度娘. 2.配置pycharm vcs,设置github用户,从github新建redash项目 ...
- 纯CSS3实现旋转木马
test.html: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- VS2013只显示会附加到进程,无法启动调试
今天在使用VS2013的时候,打开突然发现,只显示附加到进程,无法进行调试,调试位置显示灰色,到网上各处寻求答案,本以为是个大问题,没想到只是个小问题.主要原因只是后台开太多东西了,导致VS2013运 ...
- Ubuntu 安装python后,安装python-dev
1.通常情况下: sudo apt install python-dev 或者 在 sudo apt install python 命令下安装应该也附带了 python-dev 上述 pyhthon ...
- Phaser Matter Collision Plugin 碰撞插件 -- iFiero技术分享
collision-simple-demo Phaser 自带的Arcade虽然易用,但复杂的物理碰撞明显就不够用了,于是Matter等物理引擎还是不得不学的,以下是Matter物理体碰撞的一个插件, ...