环绕通知




pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.dym</groupId><artifactId>day03_eesy_04adviceType</artifactId><version>1.0-SNAPSHOT</version><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.6</source><target>1.6</target></configuration></plugin></plugins></build><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.7</version></dependency></dependencies></project>

IAccountService.java

package com.itheima.service;/*** 账户的业务层接口*/
public interface IAccountService {/*** 模拟保存账户*/void saveAccount();/*** 模拟更新账户* @param i*/void updateAccount(int i);/*** 删除账户* @return*/int  deleteAccount();
}

AccountServiceImpl.java

package com.itheima.service.impl;import com.itheima.service.IAccountService;/*** 账户的业务层实现类*/
public class AccountServiceImpl implements IAccountService{@Overridepublic void saveAccount() {System.out.println("执行了保存");
//        int i=1/0;}@Overridepublic void updateAccount(int i) {System.out.println("执行了更新"+i);}@Overridepublic int deleteAccount() {System.out.println("执行了删除");return 0;}
}

Logger.java

package com.itheima.utils;import org.aspectj.lang.ProceedingJoinPoint;/*** 用于记录日志的工具类,它里面提供了公共的代码*/
public class Logger {/*** 前置通知*/public  void beforePrintLog(){System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");}/*** 后置通知*/public  void afterReturningPrintLog(){System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");}/*** 异常通知*/public  void afterThrowingPrintLog(){System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");}/*** 最终通知*/public  void afterPrintLog(){System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");}public Object aroundPringLog(ProceedingJoinPoint pjp){Object rtValue = null;try{Object[] args = pjp.getArgs();//得到方法执行所需的参数System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置");rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置");return rtValue;}catch (Throwable t){System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常");throw new RuntimeException(t);}finally {System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终");}}
}

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置srping的Ioc,把service对象配置进来--><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean><!-- 配置Logger类 --><bean id="logger" class="com.itheima.utils.Logger"></bean><!--配置AOP--><aop:config><aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut><!--配置切面 --><aop:aspect id="logAdvice" ref="logger"><!--配置前置通知:在切入点方法执行之前执行--><!--<aop:before method="beforePrintLog" pointcut-ref="pt1" ></aop:before>--><!--&lt;!&ndash; 配置后置通知:在切入点方法正常执行之后值。它和异常通知永远只能执行一个--><!--<aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>--><!--配置异常通知:在切入点方法执行产生异常之后执行。它和后置通知永远只能执行一个--><!--<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>--><!--配置最终通知:无论切入点方法是否正常执行它都会在其后面执行--><!--<aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>--><!-- 配置环绕通知 详细的注释请看Logger类中--><aop:around method="aroundPringLog" pointcut-ref="pt1"></aop:around></aop:aspect></aop:config></beans>

配置切入点表达式|| 前置通知、后置通知、异常通知、最终通知、环绕通知相关推荐

  1. Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)

    AspectJ支持5种类型的通知注解: @Before:前置通知,在方法执行之前执行: @After:后置通知,在方法执行之后执行: @AfterRunning:返回通知,在方法返回结果之后执行(因此 ...

  2. PHP通过__call实现简单的AOP(主事务后的其他操作)比如前置通知,后置通知

    /*** person class*/ class Person {/*** person class -> function say*/public static function say($ ...

  3. JavaScript运算符:递增递减运算符前置和后置的区别

    从两段代码说起 var num1 = 2; var num2 = 20; var num3 = --num1 + num2; var num4 = num1 + num2; console.log(n ...

  4. (转)前置++和后置++的区别

    今天在阅读<google c++ 编程风格>的文档的时候,5.10. 前置自增和自减:有一句话引起了我的注意: 对于迭代器和其他模板对象使用前缀形式 (++i) 的自增, 自减运算符.,理 ...

  5. 前置++与后置++之一道简单的题目引发的思考

    引言 昨晚一时兴起,我脑子就问自己下面的代码会输出什么,也不知道我脑子为什么有这个代码模型,只是模糊的有些印象: #include <stdio.h> #include <stdli ...

  6. C++之运算符重载(前置++和后置++)

    今天在阅读<google c++ 编程风格>的文档的时候,5.10. 前置自增和自减:有一句话引起了我的注意: 对于迭代器和其他模板对象使用前缀形式 (++i) 的自增, 自减运算符.,理 ...

  7. 前置++与后置++的要点分析

    前置++和后置++,都是自增1,但是表达式返回值不同.执行效率上前置++更快. 前置++返回是自增之后的值. 等价于num+=1; rerurn num; 后置++返回的是自增之前的值. 等价于  i ...

  8. C语言的前置++和后置++

    文章目录 一.逗号表达式 二.后置++ 三.前置++ 四.几个表达式 1. a+++++b 2. ++a+++b 3. (++a)+++b 4. (++i) + (++i) 一.逗号表达式 (exp1 ...

  9. 前置++与后置++、前置--与后置--

    前置++与后置++.前置–与后置– ++与–的知识点相同,在此只记录前置++与后置++的知识点. 1 基本区别 前置++是先自加再使用,而后置++是先使用再自加! 此处自加自减指的是变量的值,使用指的 ...

最新文章

  1. [CLPR] 定位算法探幽 - 边缘和形态学
  2. 二进制、字节、int范围、编码
  3. php 二维数组去重
  4. matlab字符串数组里里固定格式的内容,字符串数组和字符数组中的文本
  5. 何为父组件子组件、props作用、slot插槽
  6. python画简便的图-python的pyecharts绘制各种图表详细(附代码)
  7. L2-001 紧急救援(最短路)
  8. (7)数据结构-基本队列实现
  9. Java中的正则表达式
  10. react customize-cra 配置less modules
  11. 中国智能POS终端行业市场供需与战略研究报告
  12. 启动计算机实现5秒开机,电脑3秒开机,怎么做到的?
  13. 高德SDK 小车轨迹回放,速度、进度控制
  14. 学习python表情包_我用Python一键保存了半佛老师所有的骚气表情包
  15. R语言基础图形元素——多边形
  16. 使用UltraISO软碟通离线安装Centos8.3.2011过程中遇到的问题
  17. acdsee14.0许可密钥使用方法:
  18. 第一次作业:幻想科技
  19. 【Lintcode】444. Graph Valid Tree II
  20. 验证码数字0-9代码分析

热门文章

  1. Codeforces1063D Candies for Children 【分类讨论】【暴力】
  2. 客户端网页编程,第二章思维导图
  3. PHP合并数组array_merge函数运算符加号与的区别
  4. Android平台 Psensor传感器调试方法
  5. TS2003基于触摸屏的应用
  6. emiya-canvas.js 解决ios下拍照倾斜与canvas高清屏下绘图模糊问题 1
  7. JQuery中对option的添加、删除、取值
  8. 配置IIS的负载均衡
  9. bitmap文件格式分析
  10. HDUOJ---1863畅通工程