Spring AOP 标签形式及Around增强处理
上一篇文章介绍了下Spring中的AOP xml配置方式。想了解的同学可以点击这里查看。
这次写一下标签形式。
<aop:aspectj-autoproxy />
首先要在spring配置文件中加入上面的标签,类似于
<context:annotation-config />
自动扫面相关的标签。
切面类:
package com.du;import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;@Aspect
public class TimeHandler {@Before("execution(* com.du.HelloWorld.*(..))")public void sayTime(){System.out.println(""+System.currentTimeMillis());}@After("execution(* com.du.HelloWorld.*(..))")public void after(){System.out.println("after");}
}
还是上一篇中的切面类,我的bean还是在配置文件中配置的,并未用标签配置,如果你没有在spring配置文件中配置此切面类,请加上@Component标签。
接下来介绍around,即为环绕增强,可以理解为包含了before和after。
Test类来模拟事务
package com.du;public class Test {public void say(String s){System.out.println(s);}
}
TimeHandler切面类中新增around方法:
package com.du;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;@Aspect
@Component
public class TimeHandler {@Before("execution(* com.du.HelloWorld.*(..))")public void sayTime() {System.out.println("" + System.currentTimeMillis());}@After("execution(* com.du.HelloWorld.*(..))")public void after() {System.out.println("after");}@Around("execution(* com.du.Test.*(..))")public void around(ProceedingJoinPoint pjp) throws Throwable {Object[] objs = pjp.getArgs();if (objs[0] instanceof String) {String s = (String) objs[0];System.out.println("拦截到方法的第一个参数为:" + s);// 将要执行方法的第一个参数修改为"hehe"objs[0] = "hehe";}// 将修改后的参数传回方法,开始执行方法。pjp.proceed(objs);System.out.println("拦截到的方法执行完成");}}
main方法如下,此处传入的参数为"haha":
package com.du;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Show {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("appliacationcontext.xml");Test test = (Test) ac.getBean("testaop");test.say("haha");}
}
运行结果如下:
可以看到修改了say方法的参数。拦截一词用的不准确,请忽略掉。
除此之外,还可以修改方法的返回值
package com.du;public class Test {public void say(String s){System.out.println(s);}public int get(int i){return i;}}
TimeHandler 类中修改后的around方法:
@Around("execution(* com.du.Test.*(..))")public int around(ProceedingJoinPoint pjp) throws Throwable {Object[] objs = pjp.getArgs();int i = (Integer) pjp.proceed(objs);System.out.println("原方法运行结果为" + i);return i+1;}
main方法:
package com.du;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Show {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("appliacationcontext.xml");Test test = (Test) ac.getBean("testaop");int i = test.get(0);System.out.println("" + i);}
}
运行结果:
Spring AOP 标签形式及Around增强处理相关推荐
- android aop静态方法,spring aop 不能对静态方法进行增强解决
想要通过aop的方式记录HttpUtils发出的post请求日志,但是 aop 不能对静态方法进行增强.只能对实例方法进行增强.. 如果一定要增强静态方法,我们可以对目标类使用单例模式,然后通过调用实 ...
- Spring AOP 增强框架 Nepxion Matrix 详解
点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! 概述 在<深入聊一聊 Spring AOP 实现机制>一文中,介绍了 Spring A ...
- spring aop代码的增强
这篇博客,主要会分析spring aop是如何实现代码增强的. 从上一篇博客 我们大概知道,spring能在不改变代码的前提下,往一个方法的之前和之后添加代码. 想下,java中有哪种技术可以帮我们实 ...
- Spring AOP 切点 Pointcut 表达式介绍与使用
一.前言 面向切面编程 AOP 是一种常见的编程思想,是面向对象编程的一种补充,AOP 框架通过修改源代码,将处理逻辑编织到指定的业务模块中 常见的处理比如:在方执行法前进行校验,在方法执行后进行日志 ...
- Spring AOP中是如何注册Advisor的?
前置博文: Spring AOP中如何为Bean创建代理? Spring AOP中是如何注册Advisor的? Spring AOP如何为目标方法创建拦截器链? Spring AOP拦截器调用的实现 ...
- Spring AOP源码(1)—<aop:config/>AOP配置标签解析【一万字】
基于最新Spring 5.x,对Spring AOP中的<aop:config/>标签的解析源码进行了详细分析,这是Spring AOP源码的入口! 此前我们已经详细学习了Spri ...
- Spring : Spring AOP 中的增强(Advice)或者通知
1.美图 2.概述 概念参考:Spring :Spring AOP 中的一些术语 很多人将增强理解为通知,但是理解为增强会更加准确,因为它表示在连接点上执行的行为,这个行为是目标类类所没有的,是为目标 ...
- 【spring aop】连接点(Jointpoint)、切入点(Pointcut)、增强(Advice)、方面/切面(Aspect、Advisor)
文章目录 前言 1. 先来回顾一下AOP中的一些术语 2. 增强方式简介 2.1 MethodBeforeAdvice前置增强 2.2 AfterReturningAdvice后置增强 2.3 Thr ...
- Spring Aop 组件概述
Spring Aop 概述 AOP(Aspect-Oriented Programming) 面向切面编程, 这种编程模型是在 OOP(Object-Oriented Programming) 的基础 ...
最新文章
- javascript 给关键字加链接
- Css 特殊或不常用属性
- minecraft正版整合包服务器,我的世界1.7.2基佬整合包
- Codeforces Round #588 (Div. 2) F. Konrad and Company Evaluation 图论 + 建反图 好题
- C++ 定义 string
- Python 判断语句 if else
- 从java中安装webolgc_Javaweb| 文件下载
- C#3.0 语言基础扩充
- 二十套Java项目源码
- Ubuntu上Qt5+OpenCV配置
- 最小生成树的实现(C语言)
- jwplayer播放器
- 感受晋味新年俗 你的高铁票可享景区门票优惠
- 人力资源管理专业知识与实务(初级)【2】
- uniapp textarea编辑保留空格换行
- 一眼万年:AI眼底筛查为什么能够成为AI医疗中的“落地之王”
- 服务号和订阅号之间的区别,企业适合选择哪一个?
- eclipse与DW联合开发java web项目
- 【科研导向】Neural Collaborative Filtering 神经协同过滤 <论文理解代码分析>
- 极域工具包 1.1正式发布!窗口化极域,解键盘锁,适配学生机房管理助手7.4-7.5!