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. RPC——看这一篇就…显然不够

    引言 RPC blablabla…… RPC 知识点 扩展 有给老婆解释的如:https://www.jianshu.com/p/2accc2840a1b

  2. js 一年中多个时间段 天数去重

    Date.prototype.format = function() { var s = ''; var mouth = (this.getMonth() + 1)>=10?(this.getM ...

  3. 小白学Python——Anaconda安装

    小白:Mr.林,快救救我,我被那些数据压得喘不过气了. Mr.林:小白,表方,怎么了? !   小白:Mr.林,我从公司数据平台上下了一堆数据,如果选择时间范围广的话,平台就卡的动不了,动不动还奔溃, ...

  4. 七、SAP中输出当前日期

    一.在Sap中输出当前日期的函数是sy-datum,代码如下: 二.输出效果如下

  5. 自定义alert

    参考:https://www.cnblogs.com/st-leslie/articles/5279864.html 把window.alert=function(){}指向新的方法,即相当于重写 w ...

  6. cf1200 D White Lines(二维差分)

    题目大意 有一个大小为n的矩阵,每个1*1的单位为黑或白,我们可以用一个(只有一个)大小为k*k的白色矩阵覆盖,问:最多的时候有几条白线(横的全为白 或竖的全为白 即为白线). 思路 要想把一条线(以 ...

  7. Elasticsearch分布式搜索

    ElasticSearch之介绍 一 Elasticsearch产生背景 1.1 大规模数据如何检索 如:当系统数据量上了10亿.100亿条的时候,我们在做系统架构的时候通常会从以下角度去考虑问题:1 ...

  8. XML--XML Schema Definition(三)

    参考 http://www.w3school.com.cn/schema/index.asp XSD 复合元素 复合元素指包含其他元素及/或属性的 XML 元素. 有四种类型的复合元素: 空元素 包含 ...

  9. ping内网服务器

    cat ping.sh#!/bin/baship="192.168.1."lastip=(200201202210211212220221222) #ip列表 可以继续添加 ps ...

  10. 抓DHCP客户端ip脚本

    cat testnew.sh #!/bin/bash catch_ip (){Ip=`sudo nmap -sP 192.168.1.0/24 |grep -i -B2 $mac|grep Nmap ...