【Spring实战】—— 10 AOP针对参数的通知
时间:2022-04-22
本文章向大家介绍【Spring实战】—— 10 AOP针对参数的通知,主要内容包括源码参考、讲解说明、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
通过前面的学习,可以了解到 Spring的AOP可以很方便的监控到方法级别的执行 ,针对于某个方法实现通知响应。 那么对于方法的参数如何呢? 比如我们有一个方法,每次传入了一个字符串,我想要知道每次传入的这个字符串是神马?这又如何办到呢!
举个Action上面的例子,一个思考者(thinker),每次在思考时,都会传入一个字符串作为思考的内容。
我们想要每次获取到这个思考的内容,实现一个通知。因此读心者可以通过AOP直接监控到每次传入的内容。
源码参考
首先看一下思考者的接口和实现类:
package com.spring.test.aopmind;
public interface Thinker {
void thinkOfSomething(String thoughts);
}
package com.spring.test.aopmind;
public class Volunteer implements Thinker{
private String thoughts;
public void thinkOfSomething(String thoughts) {
this.thoughts = thoughts;
}
public String getThoughts(){
return thoughts;
}
}
下面是读心者的接口和实现类:
package com.spring.test.aopmind;
public interface MindReader {
void interceptThoughts(String thougths);
String getThoughts();
}
package com.spring.test.aopmind;
public class Magician implements MindReader{
private String thoughts;
public void interceptThoughts(String thougths) {
System.out.println("Intercepting volunteer's thoughts");
this.thoughts = thougths;
}
public String getThoughts() {
return thoughts;
}
}
接着配置好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: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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="magician" class="com.spring.test.aopmind.Magician"/>
<bean id="xingoo" class="com.spring.test.aopmind.Volunteer" />
<aop:config proxy-target-class="true">
<aop:aspect ref="magician">
<aop:pointcut id="thinking"
expression="execution(* com.spring.test.aopmind.Thinker.thinkOfSomething(String)) and args(thoughts)"/>
<aop:before pointcut-ref="thinking" method="interceptThoughts" arg-names="thoughts"/>
</aop:aspect>
</aop:config>
</beans>
测试类如下
package com.spring.test.aopmind;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
Thinker thinker = (Thinker)ctx.getBean("xingoo");
thinker.thinkOfSomething("吃点啥呢!");
}
}
执行结果:
Intercepting volunteer's thoughts
讲解说明
在配置文件中:
在<aop:before>中指明了要传入的参数thoughts
在<aop:pointcut>切点中通过AspectJ表达式锁定到特定的方法和参数thoughts
这样,当执行到方法thinkOfSomething()之前,就会触发aop,得到参数thoughts,并传递给通知类的拦截方法中。
- 分布式计算,WCF+JSON+实体对象与WebService+DataSet效率大比拼
- 【自然框架】 页面里的父类—— 改进和想法、解释
- 线性神经网络
- 【数据可视化】深度解析大数据可视化设计案例分析
- 使用IE6看老赵的博客——比较完美版(可以在线查看、回复)
- 【Python环境】R vs Python:硬碰硬的数据分析
- 使用IE6看老赵的博客——jQuery初探
- matlab GUI基础8
- 见到了“公司”定义一个Company类,那么见到了“字段”是不是也可定义一个Column类?
- 再论桥接模式(上)纸上谈兵
- 反馈型神经网络
- 竞争型神经网络续1
- 径向基神经网络续1
- "开门待客"还是“送货上门”?
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释