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来实现自身的业务逻辑,经常会导 ...
随机推荐
- 20155230 实验三《敏捷开发与XP实践》实验报告
20155230 实验三<敏捷开发与XP实践>实验报告 一.使用工具(Code->Reformate Code)把代码重新格式化 IDEA里的Code菜单有很多实用的功能可以帮助我们 ...
- 成都优步uber司机奖励政策(持续更新)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://didi-uber.com/archiv ...
- 移动端推广APP防作弊机制之依我见
本文来自网易云社区 在广告投放过程中,虚假流量常常给广告运营人员带来麻烦,影响广告投放的效果,如何预防作弊,不妨先来重现一下流量产生的场景,用户点击广告之后,一般都会落到广告主的网页,或者安装广告主的 ...
- 获取安卓app的appPackage和appActivity
1.需要配置好android的开发环境后,打开cmd命令窗口 2.在命令窗口中输入,adb logcat>D:/log.log,抓取日志 3.运行启动app 4.查看日志log 5.搜索日志的关 ...
- Mac下布置appium环境
1.下载或者更新Homebrew:homebrew官网 macOS 不可或缺的套件管理器 $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githu ...
- Hexo+gitment
Gitment是一个基于GitHub问题的评论系统,可以在没有任何服务器端实现的前端使用. 演示页面 中文简介 特征 入门 方法 定制 关于安全 特征 GitHub登录 Markdown / GFM支 ...
- TPO 02 - Desert Formation
TPO 02 - Desert Formation NOTE: 主要意思(大概就是主谓宾)用粗体标出:重要的其它用斜体: []中的是大致意思,可能与原文有关也可能无关,但不会离题 目的为训练句子/段落 ...
- 《Node.js核心技术教程》学习笔记
<Node.js核心技术教程>TOC \o "1-3" \h \z \u 1.章模块化编程 2019.2.19 13:30' PAGEREF _101 \h 1 08D ...
- 169.求众数 leetcode Javascript
给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 ...
- Python学习之web框架 Flask
一.通过PIP 安装Flask 1.1 Windows环境安装pip A.首先PIP进入官网(https://pypi.python.org/pypi/pip)下载gz包 B.对gz压缩包进行解压,解 ...