是一种建议类型,可确保方法执行前后的通知可以运行。 以下是通知的语法:

语法

@Pointcut("execution(* com.yiibai.Student.getAge(..))") private void selectGetName(){} @Around("selectGetAge()") public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint){ System.out.println("[aroundAdvice] Around advice"); Object[] args=jp.getArgs(); if(args.length>0){ System.out.print("[aroundAdvice] Arguments passed: " ); for (int i = 0; i < args.length; i++) { System.out.print("[aroundAdvice] arg "+(i+1)+": "+args[i]); } } Object result=jp.proceed(args); System.out.println("[aroundAdvice] Returning " + result); return result.toString(); }

在上面的语法示例中 –

– 将函数标记为切入点

execution( expression ) – 涵盖应用通知的方法的表达式。

– 将函数标记为在切入点覆盖的方法之前执行的通知。

要了解上面提到的通知相关的概念,我们写一个Spring AOP基于注解的应用例子,它将使用基于注解配置实现通知。打开并使用Eclipse IDE,并按照以下步骤创建一个Spring应用程序:

更新在中创建过的Student项目。

更新bean配置并运行应用程序,如下所述。

整个项目的目录结构如下所示 –

下面是Logging.java文件的内容。 这实际上是一个Aspect模块的样本,它定义了在各个点被调用的方法。

package com.yiibai; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.ProceedingJoinPoint; @Aspect public class Logging { /** Following is the definition for a pointcut to select * all the methods available. So advice will be called * for all the methods. */ @Pointcut("execution(* com.yiibai.Student.getAge(..))") private void selectGetAge(){} /** * This is the method which I would like to execute * around a selected method execution. */ @Around("selectGetAge()") public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ System.out.println("Around advice"); Object[] args=proceedingJoinPoint.getArgs(); if(args.length>0){ System.out.print("Arguments passed: " ); for (int i = 0; i < args.length; i++) { System.out.print("arg "+(i+1)+": "+args[i]); } } Object result=proceedingJoinPoint.proceed(args); System.out.println("Returning " + result); } }

以下是Student.java文件的内容:

package com.yiibai; public class Student { private Integer age; private String name; public void setAge(Integer age) { this.age = age; } public Integer getAge() { System.out.println("Age : " + age ); return age; } public void setName(String name) { this.name = name; } public String getName() { System.out.println("Name : " + name ); return name; } public void printThrowException(){ System.out.println("Exception raised"); throw new IllegalArgumentException(); } }

以下是MainApp.java文件的内容:

package com.yiibai; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Student student = (Student) context.getBean("student"); student.getAge(); } }

以下是配置文件Beans.xml文件的内容:

运行项目

完成创建源代码和配置文件后,运行应用程序。右键单击应用程序中的MainApp.java,并使用运行方式作为Java应用程序命令。 如果您的应用程序一切正常,这将打印以下消息:

[aroundAdvice]Around advice Age : 24 [aroundAdvice] Returning 24

¥ 我要打赏   纠错/补充 收藏

java @around,Spring AOP基于注解的Around通知相关推荐

  1. Java之——Spring AOP自定义注解实现日志管理

    转载请注明出处:https://blog.csdn.net/l1028386804/article/details/80295737 1.定义日志类SystemLog package io.mykit ...

  2. Spring Aop 常见注解和执行顺序

    欢迎关注方志朋的博客,回复"666"获面试宝典 来源:juejin.cn/post/7062506923194581029 Spring 一开始最强大的就是 IOC / AOP 两 ...

  3. spring AOP自定义注解方式实现日志管理

    转:spring AOP自定义注解方式实现日志管理 今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接 ...

  4. spring aop 自定义注解配合swagger注解保存操作日志到mysql数据库含(源码)

    spring aop 自定义注解保存操作日志到mysql数据库 一.思路 二.自定义注解 三.编写操作日志 四.编写操作日志切面\增强 五.使用 六.`注意` 一.思路 利用spring aop 对方 ...

  5. Spring 事务基于注解和xml方式

    文章目录 基于注解方式的Spring事务配置 1 创建表结构 2 创建实体类 3 创建Dao 4 创建DaoImpl 5 创建Service 6 创建SrviceImpl 7 创建TxConfig 8 ...

  6. AOP基于注解环绕通知

    核心代码 (前提配置好文件) @Aspect // 1. 让当前类变成切面类 @Component // 2. 让spring接管切面类的创建 public class LogAspect {/*** ...

  7. spring aop 配置注解 切入点说明

    转载链接:https://www.cnblogs.com/junzi2099/p/8274813.html 基于注解的Spring AOP开发 简单案例快速入门 定义目标类接口和实现类 /*** Cr ...

  8. 【Spring】基于注解实现事务控制(银行转账)

    结构 domain类 package com.itheima.domain;import java.io.Serializable;/*** 账户的实体类*/ public class Account ...

  9. Spring Boot 基于注解驱动源码分析--自动配置

    Spring作为Java开发最常用的容器管理框架,使用注解为我们提供很多便捷,下面通过源码分析Spring基于注解驱动自动配置的原理 首先介绍两个关键类: ConfigurationClassPost ...

最新文章

  1. C++多线程编程以及epoll处理socket通信时多端口问题
  2. caffe模型文件解析_Caffe ImageData神经网络基本示例无法解析模型文件
  3. 从未在一起更让人遗憾_二十不惑强行悲伤结尾,我们本可以在一起,才最让人遗憾...
  4. 第一部分 Calendar介绍
  5. 【Git】GitHub主页从Dark调回Light的方法
  6. java集成开发工具项目_Java项目开发(一)-不借助集成工具创建Java项目并编写编译执行脚本...
  7. 关于K-Means算法
  8. 华为获首张 5G 终端进网许可证;Linux 之父来华;Eclipse 4.12 发布 | 极客头条
  9. js中WINDOW对象中的location成员对象
  10. 前端学习之--CSS
  11. C# string 判断字符串是否是中文
  12. 【Go语言入门教程】Go语言简介
  13. Linux工具参考篇(网摘)
  14. 傅里叶分析之掐死教程
  15. Elastisearch 简介 使用 Query DSL 映射 分词 Elasticsearch-Rest-Client
  16. 关于android studio menu键的问题
  17. 接口测试用例怎么写?一文1600字教你写一个优秀的接口测试的测试用例
  18. python——获取矩形四个角点的坐标
  19. 《鱿鱼游戏》开场了|谁才是影评人的御用写作工具
  20. Android开发入门与实战之Android应用安装卸载

热门文章

  1. VS2005如何加载Word组件(详细步骤)
  2. SDNU-ACM第一次月赛地大武汉选拔赛
  3. FAQ:Nacos报错:server is DOWN now, please try again later!
  4. 微软认证系统管理员MCSA(院校IT课程)
  5. [新人向]MySQL和Navicat下载、安装及使用详细教程
  6. Alibaba大牛常读的10本Java实战书籍,(Java开发进阶必备书单),可以白嫖了
  7. css 优惠券样式大全
  8. SAP 字段仍作为视图字段在视图中使用 | 更改表结构重新生成 CDS View「实例」
  9. 【论文笔记】《SketchMate: Deep Hashing for Million-Scale Human Sketch Retrieval》
  10. winForm在多显示器(主显示器 + 扩展显示器)上显示最大化和还原操作。