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

定义切面

  1. package com.zln.aop;
  2. import org.aspectj.lang.annotation.*;
  3. /**
  4. * 定义切面
  5. * Created by sherry on 17/3/9.
  6. */
  7. @Aspect
  8. public class Audience {
  9. /**
  10. * 切点
  11. */
  12. @Pointcut("execution(* com.zln.aop.Performance.*(..))")
  13. public void performance(){}
  14. /**
  15. * 前置通知
  16. */
  17. @Before("performance()")
  18. public void silenceCellPhones(){
  19. System.out.println("前置通知:表演前手机静音");
  20. }
  21. @Before("performance()")
  22. public void takeSeats(){
  23. System.out.println("前置通知:就坐");
  24. }
  25. @AfterReturning("performance()")
  26. public void applause(){
  27. System.out.println("返回通知:表演结束,鼓掌");
  28. }
  29. @AfterThrowing("performance()")
  30. public void demandRefund(){
  31. System.out.println("异常通知:表演失败");
  32. }
  33. }
  1. package com;
  2. import com.zln.aop.Audience;
  3. import org.springframework.context.annotation.*;
  4. /**
  5. * Created by sherry on 17/3/9.
  6. */
  7. @Configuration
  8. @EnableAspectJAutoProxy
  9. @ComponentScan
  10. public class AppBeans {
  11. @Bean
  12. public Audience audience(){
  13. return new Audience();
  14. }
  15. }

运行测试

  1. package com.zln.aop;
  2. import org.springframework.stereotype.Component;
  3. /**
  4. * Created by sherry on 17/3/9.
  5. */
  6. @Component
  7. public class Performance{
  8. public void perform() {
  9. System.out.println("正在表演");
  10. }
  11. }

环绕通知

  1. package com.zln.aop;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.*;
  4. /**
  5. * 定义切面
  6. * Created by sherry on 17/3/9.
  7. */
  8. @Aspect
  9. public class Audience {
  10. /**
  11. * 切点
  12. */
  13. @Pointcut("execution(* com.zln.aop.Performance.*(..))")
  14. public void performance(){}
  15. /**
  16. * 前置通知
  17. */
  18. @Before("performance()")
  19. public void silenceCellPhones(){
  20. System.out.println("前置通知:表演前手机静音");
  21. }
  22. @Before("performance()")
  23. public void takeSeats(){
  24. System.out.println("前置通知:就坐");
  25. }
  26. @AfterReturning("performance()")
  27. public void applause(){
  28. System.out.println("返回通知:表演结束,鼓掌");
  29. }
  30. @AfterThrowing("performance()")
  31. public void demandRefund(){
  32. System.out.println("异常通知:表演失败");
  33. }
  34. @Around("performance()")
  35. public void watch(ProceedingJoinPoint proceedingJoinPoint){
  36. System.out.println("环绕通知1");
  37. try {
  38. proceedingJoinPoint.proceed();
  39. System.out.println("环绕通知2");
  40. } catch (Throwable throwable) {
  41. throwable.printStackTrace();
  42. }
  43. }
  44. }

环绕通知的proceedingJoinPoint.proceed();比较神奇,如果没有这句,相当于代码阻塞,如果调用多次,相当于执行多次目标方法

通知的方法参数

  1. package com.zln.aop;
  2. import org.springframework.stereotype.Component;
  3. /**
  4. * Created by sherry on 17/3/9.
  5. */
  6. @Component
  7. public class Performance{
  8. public void perform(int i) {
  9. System.out.println("正在表演");
  10. }
  11. }
  1. //如果不是int等基本类型,要使用类的全限定名
  2. @Pointcut("execution(* com.zln.aop.Performance.*(int))")
  3. public void performance(){}
  4. /**
  5. * 前置通知
  6. */
  7. @Before("performance()&&args(i)")
  8. public void silenceCellPhones(int i){
  9. System.out.println("前置通知:表演前手机静音"+i);
  10. }

调用perform方法的时候,参数i的值就会被获取到

Spring AOP基础的更多相关文章

  1. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

  2. [Spring框架]Spring AOP基础入门总结一.

    前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...

  3. CgLib动态代理学习【Spring AOP基础之一】

    如果不了解JDK中proxy动态代理机制的可以先查看上篇文章的内容:Java动态代理学习[Spring AOP基础之一] 由于Java动态代理Proxy.newProxyInstance()的时候会发 ...

  4. 【AOP】Spring AOP基础 + 实践 完整记录

    Spring AOP的基础概念 ============================================================= AOP(Aspect-Oriented Pr ...

  5. Spring AOP基础知识

    Spring AOP使用动态代理技术在运行期织入增强的代码,两种代理机制包括:一是基于JDK的动态代理,另一种是基于CGLib的动态代理.之所以需要两种代理机制,很大程度上是因为JDK本身只提供接口的 ...

  6. Java动态代理学习【Spring AOP基础之一】

    Spring AOP使用的其中一个底层技术就是Java的动态代理技术.Java的动态代理技术主要围绕两个类进行的 java.lang.reflect.InvocationHandler java.la ...

  7. Spring AOP基础概念及自定义注解式AOP初体验

    对AOP的理解开始是抽象的,看到切点的匹配方式其实与正则表达式性质大致一样就基本了解AOP是基本是个什么作用了.只是整个概念更抽象,需要具化理解.下图列表是AOP相关概念解释,可能也比较抽象^_^ 比 ...

  8. Spring Aop基础总结

    什么是AOP: Aop技术是Spring核心特性之中的一个,定义一个切面.切面上包括一些附加的业务逻辑代码.在程序运行的过程中找到一个切点,把切面放置在此处,程序运行到此处时候会运行切面上的代码.这就 ...

  9. Spring AOP小记

    一.概述 在通常的开发过程中,我们调用的顺序通常是controller->service-dao,其中,service中包含着太多的业务逻辑,并且还要不断调用dao来实现自身的业务逻辑,经常会导 ...

随机推荐

  1. 20155310 《JAVA程序设计》实验二(JAVA面向对象程序设计)实验报告

    20155310 <JAVA程序设计>实验二(JAVA面向对象程序设计)实验报告 实验内容 •初步掌握单元测试和TDD •理解并掌握面向对象三要素:封装.继承.多态 •初步掌握UML建模 ...

  2. C语言复习20170716

    C语言复习20170716 C数据类型 图片来自:C语言基本数据类型简介 C语言程序处理的数据有常量和变量两种形式. 常量是在程序中不能改变其值的量.例如:整型常量.实型常量.字符常量.字符串常量和枚 ...

  3. Nginx入门篇(一)之Nginx介绍

    1.简介 Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 服务器. Nginx 是由 Igor Sysoe ...

  4. L018-课前练习以及知识巩固笔记

    L018-课前练习以及知识巩固笔记 OK,今天课前做了几道题,算是对以往知识的巩固. 1.请描述下列路径的内容是做什么的?/etc/sysctl.conf/etc/rc.local/etc/hosts ...

  5. 微服务介绍及Asp.net Core实战项目系列之微服务介绍

    0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 一.微服务选型 在做微服务架构的技术选型的时候,我们以“无侵入”和“社区活跃”为主要的考量点,将来升级为原子服务架构.量子服务架构 ...

  6. Cocoa Touch提供了哪几种Core Animation过渡类型?

    过渡动画通过 type 设置不同的动画效果, CATransition 有多种过渡效果, 但其实 Apple 官方的SDK只提供了四种: fade 淡出 默认 moveIn 覆盖原图 push 推出 ...

  7. PL/SQL编辑数据"这些查询结果不可更新,请包括ROWID或使用SELECT...FOR UPDATE获得可更新结果"处理

    只要有人用了: select t.* from 表名  t where 字段=xxx  for update 而不是: select t.rowid,t.* from 表名  t where 字段=x ...

  8. JS Windows.document对象

    四中选择器:class ,id , name , 标签 通过选择器获取对象: ...................................ClassName('');  -- class选择 ...

  9. 使用phpMyAdmin管理网站数据库(创建、导入、导出…)

    作为一名站长,最重视的就是网站的数据安全了.本节襄阳网站优化就来讲讲如何使用phpMyAdmin管理软件进行mysql数据库的管理,实现基本的数据库管理用户.数据库的创建.数据的导入和导出操作(网站备 ...

  10. Codeforces Round #503 (by SIS, Div. 2) D. The hat

    有图可以直观发现,如果一开始的pair(1,1+n/2)和pair(x, x+n/2)大小关系不同 那么中间必然存在一个答案 简单总结就是大小关系不同,中间就有答案 所以就可以使用二分 #includ ...