Spring AOP 的使用过程理解

首先,aop的使用场景介绍:

1、处理一些通用的非功能性的需求,不影响业务流程,比如说打印日志、性能统计、推送消息等;

2、aop无法拦截static、final方法、private方法

3、无法拦截内部方法调用

如果只要访问目标方法的参数,Spring还提供了一种更简单的方法:我们可以在程序中使用args来绑定目标方法的参数。如果在一个args表达式中指定了一个或多个参数,则该切入点将只匹配具有对应形参的方法,且目标方法的参数值将被传入增强处理方法。

Person.java :

public interface Person {
public String sayHello(String name);
public void eat(String food,Date time);
}

Chinese.java :

@Component
public class Chinese implements Person { @Override
public void eat(String food, Date time) {
System.out.println("我正在吃"+food+",现在时间是:"+time);
} @Override
public String sayHello(String name) {
return name+" Hello,Spring AOP";
} }

AccessArgAspect.java :

@Aspect
public class AccessArgAspect { @AfterReturning(returning="retVal",
pointcut="execution(* com.bean.*.*(..)) && args(food,time)")
public void access(String food,Date time,Object retVal){
System.out.println("目标方法中Strig参数为:"+food);
System.out.println("目标方法中Date参数为:"+time);
System.out.println("模拟记录日志...");
}
}

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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:component-scan base-package="com.bean">
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>

<aop:aspectj-autoproxy/>
</beans>

Test.java

public class Test {
public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
Person p=(Person) ctx.getBean("chinese");
System.out.println(p.sayHello("张三"));
p.eat("西瓜",new Date());
}
}

运行程序,控制台输出:

切入点表达式部分增加了&&args(food,time)部分,意味着可以在增强处理方法中定义food和time两个形参------定义这两个形参时,形参类型可以随意指定,但是一旦指定,譬如这里分别是String类型和Date类型,这两个形参类型将用于限制该切入点只匹配第一个参数类型为String,第二个参数类型为Date的方法。

我们如果修改access方法的形参列表为:

public void access(int food,Date time,Object retVal)

那么该切入点表达式只能匹配第一个形参类型为int,第二个形参类型为Date的方法,此例子中没有这样的方法,也就匹配不上。此时运行程序,看控制台输出:

可以看到没有方法匹配上,程序里的增强处理没起作用。

至此可以看出,使用 args表达式 有如下两个作用:

① 提供了一种简单的方式来访问目标方法的参数。

② 可用于对切入表达式增加额外的限制。

除此之外,使用args表达式时还可使用如下形式:args(name,age,..),这表明增强处理方法中可以通过name,age来访问目标方法的参数。注意上面args表达式括号中的2点,它表示可以匹配更多参数。

Spring AOP中使用args表达式访问目标方法的参数的更多相关文章

  1. Spring AOP中pointcut expression表达式

    Pointcut 是指那些方法需要被执行"AOP",是由"Pointcut Expression"来描述的. Pointcut可以有下列方式来定义或者通过&am ...

  2. 转载《Spring AOP中pointcut expression表达式解析 及匹配多个条件》

    原文地址:https://www.cnblogs.com/rainy-shurun/p/5195439.html 原文 Pointcut 是指那些方法需要被执行"AOP",是由&q ...

  3. Spring AOP中pointcut expression表达式解析 及匹配多个条件

    Spring中事务控制相关配置: <bean id="txManager" class="org.springframework.jdbc.datasource.D ...

  4. Spring AOP 中pointcut expression表达式

    原文地址——http://blog.csdn.net/qq525099302/article/details/53996344 Pointcut是指那些方法需要被执行”AOP”,是由”Pointcut ...

  5. Spring AOP 中pointcut expression表达式解析及配置

    Pointcut是指那些方法需要被执行”AOP”,是由”Pointcut Expression”来描述的. Pointcut可以有下列方式来定义或者通过&& || 和!的方式进行组合. ...

  6. Spring AOP中pointcut expression表达式解析

    Pointcut 是指那些方法需要被执行"AOP",是由"Pointcut Expression"来描述的. Pointcut可以有下列方式来定义或者通过&am ...

  7. Spring AOP中 pointcut expression表达式解析

    任意公共方法的执行: execution(public * *(..)) 任何一个以“set”开始的方法的执行: execution(* set*(..)) AccountService 接口的任意方 ...

  8. Spring AOP中args()、arg-names、argNames

    先小结一下: args()是用来匹配并且接收目标方法的参数的. argNames(用在注解中)与arg-names(用在XML中),他们是同一个东西. argNames用来接收AspectJ表达式中的 ...

  9. spring aop中pointcut表达式完整版

    spring aop中pointcut表达式完整版 本文主要介绍spring aop中9种切入点表达式的写法 execute within this target args @target @with ...

随机推荐

  1. Ubuntu18安装LAMP环境详细步骤

    Ubuntu18安装Lamp环境 1.su root  切换root账号(root账户权限高不用总输入sudo) 更新源 阿里源网址:https://opsx.alibaba.com/mirror 更 ...

  2. python matplotlib给图中的点加标签

    在写论文用到matplotlib画散点图,想着如果能把每个点对应的ID打在点的旁边就好了,经过一番搜索,最后找到了方法. 首先是打点,先把所有的点画好,举例如下: p1 = ax.scatter(X[ ...

  3. Egret Engine 2D - 显示容器

      DisplayObjectContainer 所有容器的父类 1 添加 删除 子对象 2 访问子对象 3 检测子对象 4 设置叠放次序 Sprite 继承自DisplayObjectContain ...

  4. PAT Advanced 1151 LCA in a Binary Tree (30) [树的遍历,LCA算法]

    题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...

  5. 可重入排他锁ReentrantLock源码浅析

    1.引子 "ReentrantLock"单词中的“Reentrant”就是“重入”的意思,正如其名,ReentrantLock是一个支持重入的排他锁,即同一个线程中可以多次获得同步 ...

  6. Bootstrap-按钮篇btn

    参考网址:http://v3.bootcss.com/(能抄不写) 1.按钮颜色样式: 对应代码:(主要体现在class内容:btn-default,btn-primary...) <butto ...

  7. 求1+2+3+…..+n

    [问题]求1+2+3+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). [思路]由于题目好多运算符不能用,我们只有想到使用递 ...

  8. 洛谷 P1018乘积最大

    题目描述 今年是国际数学联盟确定的“20002000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰9090周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友 ...

  9. 文献阅读报告 - Pedestrian Trajectory Prediction With Learning-based Approaches A Comparative Study

    概述 本文献是一篇文献综述,以自动驾驶载具对外围物体行动轨迹的预测为切入点,介绍了基于运动学(kinematics-based)和基于机器学习(learning-based)的两大类预测方法. 并选择 ...

  10. 【每日Scrum】第七天冲刺

    一.计划会议内容 界面ui制作,主界面进度 二.任务看板 三.scrum讨论照片 四.产品的状态 无 五.任务燃尽图