package com.hope.utils;

import org.aspectj.lang.ProceedingJoinPoint;

/**
* @author newcityman
* @date 2019/11/22 - 23:29
* 记录日志的类
*/
public class Logger {
/**
* 前置通知
*/
public void beforePrintLog(){
System.out.println("前置通知");
}

/**
* 后置通知
*/
public void afterReturningPrintLog(){
System.out.println("后置通知");
}

/**
* 异常通知
*/
public void afterThrowingPrintLog(){
System.out.println("异常通知");
}

/**
* 最终通知
*/
public void afterPrintLog(){
System.out.println("最终通知");
}

/**
* 环绕通知
*/
public Object aroundPrintLog(ProceedingJoinPoint pjp){
Object rtValue = null;
try {
System.out.println("前置通知");
Object[] args = pjp.getArgs();
rtValue= pjp.proceed(args);
System.out.println("后置通知");
return rtValue;
} catch (Throwable t) {
System.out.println("异常通知");
throw new RuntimeException(t);
}finally {
System.out.println("最终通知");
}

}
}
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<!--配置spring的ioc,把service对象配置进来-->
<bean id="accountService" class="com.hope.service.impl.AccountService"></bean>
<!--配置logger类-->
<bean id="logger" class="com.hope.utils.Logger"></bean>
<!--配置AOP-->
<aop:config>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger">
<aop:pointcut id="pt1" expression="execution(* *..*.*(..))"/>
<!--配置通知的类型,并且建立通知方法和切入点方法的关联-->
<!--<aop:before method="printLog" pointcut="execution(public void com.hope.service.impl.AccountService.saveAccount())"></aop:before>-->
<!--<aop:before method="printLog" pointcut="execution(void com.hope.service.impl.AccountService.saveAccount())"></aop:before>-->
<!--<aop:before method="printLog" pointcut="execution(* com.hope.service.impl.AccountService.saveAccount())"></aop:before>-->
<!--<aop:before method="printLog" pointcut="execution(* *.*.*.*.AccountService.saveAccount())"></aop:before>-->
<!--<aop:before method="printLog" pointcut="execution(* *.*.*.*.AccountService.saveAccount())"></aop:before>-->
<!--<aop:before method="printLog" pointcut="execution(* *..AccountService.saveAccount())"/>-->
<!--<aop:before method="printLog" pointcut="execution(* *..*.*())"/>-->
<!--<aop:before method="beforePrintLog" pointcut-ref="pt1"/>-->
<!--<aop:after-returning method="afterReturningPrintLog" pointcut="execution(* *..*.*(..))"/>-->
<!--<aop:after-throwing method="afterThrowingPrintLog" pointcut="execution(* *..*.*(..))"/>-->
<!--<aop:after method="afterPrintLog" pointcut-ref="pt1"/>-->
<!--<aop:before method="printLog" pointcut="execution(* *..*.*(..))"/>-->
<aop:around method="aroundPrintLog" pointcut-ref="pt1"/>
</aop:aspect>
</aop:config>
</beans>


AOP中环绕通知的写法的更多相关文章

  1. AOP中环绕通知的书写和配置

    package com.hope.utils;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotatio ...

  2. java中AOP的环绕通知

    pom.xml <dependencies> <dependency> <groupId>org.springframework</groupId> & ...

  3. AOP 环绕通知 (Schema-base方式) 和 AspectJ方式在通知中获取切点的参数

    环绕通知(Schema- base方式) 1.把前置通知和后置通知都写到一个通知中,组成了环绕通知 2.实现步骤: 2.1 新建一个类实现 MethodInterceptor 接口 public cl ...

  4. Java开发学习(十九)----AOP环绕通知案例之密码数据兼容处理

    一.需求分析 需求: 对百度网盘分享链接输入密码时尾部多输入的空格做兼容处理. 问题描述: 点击链接,会提示,请输入提取码,如下图所示 当我们从别人发给我们的内容中复制提取码的时候,有时候会多复制到一 ...

  5. spring aop 环绕通知around和其他通知的区别

    前言: spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别: 1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知   是不能决定的,他们只 ...

  6. [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. Spring AOP中定义切点(PointCut)和通知(Advice)

    如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ...

  8. spring aop环绕通知

    [Spring实战]—— 9 AOP环绕通知   假如有这么一个场景,需要统计某个方法执行的时间,如何做呢? 典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间. 如果采用Sprin ...

  9. [转载] spring aop 环绕通知around和其他通知的区别

    前言: spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别: 1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知   是不能决定的,他们只 ...

随机推荐

  1. 【JAVA】笔记(1)---JVM内存图;方法重载条件;输入方法;转义字符;强制类型转换;变量分类及区别;Java命名规范;

    Java命名规范: 1.包:全部字母小写: 2.类+接口:所有单词的首字母大写: 3.变量+方法:第一个单词的首字母小写,其余单词首字母大写: 3.常量名:所有字母均大写,且用下划线" _ ...

  2. Maven 依赖调解源码解析(五):同一个文件内声明,后者覆盖前者

    本文是系列文章<Maven 源码解析:依赖调解是如何实现的?>第五篇,主要介绍同一个文件内声明,后者覆盖前者的原则.请按顺序阅读其他系列文章,系列文章总目录参见:https://www.c ...

  3. CodeBlocks调试器缺少(gdb.exe)文件

    错误如下: Building to ensure sources are up-to-date Selecting target:  Debug ERROR: You need to specify ...

  4. C++的重载操作符(operator)介绍

    原文转载至:https://blog.csdn.net/liitdar/article/details/80654324 https://blog.csdn.net/liitdar/article/d ...

  5. 跟着老猫来搞GO-内建容器Map

    前期回顾 在上面的文章中,老猫和大家分享了GO语言中比较重要的两种数据结构,一种是数组,另外一种是基于数组的slice.本篇文章想要继续和大家分享剩下的容器以及字符字符串的处理. MAP map的定义 ...

  6. WebRTC打开本地摄像头

    本文使用WebRTC的功能,打开电脑上的摄像头,并且把摄像头预览到的图像显示出来. 纯网页实现,能支持除IE外的多数浏览器.手机浏览器也可用. 引入依赖 我们需要引入adapter-latest.js ...

  7. settings.json文件语法错误先清理错误再重试

    运行官方demo,提示要微信开发者工具,导入路径出这玩意. 点击工具,设置,源码试图,把左边代码全部复制到右边, "weApp.devTools.path":"W:\\微 ...

  8. 从零开始学Kotlin第一课

    Kotlin的方法: 一个简单的计算器: fun main(args:Array<String>){ //主函数main方法 var a=8; var b=9; println(plus( ...

  9. Elasticsearch分布式搜索和数据分析引擎-ElasticStack(上)v7.14.0

    Elasticsearch概述 **本人博客网站 **IT小神 www.itxiaoshen.com Elasticsearch官网地址 https://www.elastic.co/cn/elast ...

  10. 洛谷 P5471 - [NOI2019] 弹跳(二维线段树优化建图+堆优化存边)

    题面传送门 一道非常有意思的题(大概可以这么形容?) 首先看到这类一个点想一个区域内连边的题目可以很自然地想到线段树优化建图,只不过这道题是二维的,因此需要使用二维线段树优化建图,具体来说,我们外层开 ...