spring含参数 环绕通知demo
题目:有一个懂得读心术的人需要完成两件事情:截听志愿者的内心感应和显示他们在想什么
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="magician" class="imple.Magician"/> <aop:config>
<aop:aspect ref="magician">
<aop:pointcut expression="execution(* inter.Thinker.thinkOfSomething(String)) and args(thoughts)" id="thingking" />
<aop:before method="interceptThoughts" pointcut-ref="thingking" arg-names="thoughts"/>
</aop:aspect>
</aop:config>
</beans>
读心术:
package inter;
public interface MindReader {
void interceptThoughts(String thoughts);
String getThoughts();
}
package imple;
import inter.MindReader;
public class Magician implements MindReader{
private String thoughts;
public void interceptThoughts(String thoughts) {
System.out.println("前置Intercepting volunter's thoughts");
this.thoughts = thoughts;
System.out.println(thoughts);
System.out.println("后置");
}
public String getThoughts() {
return thoughts;
}
}
志愿者:
package inter;
public interface Thinker {
void thinkOfSomething(String thoughts);
}
package imple;
import inter.Thinker;
public class Volunteer implements Thinker{
private String thoughts;
public void thinkOfSomething(String thoughts) {
this.thoughts = thoughts;
}
public String getThoughts(){
return thoughts;
}
}
测试代码:
@Test
public void test7(){
Magician magician = (Magician) ctx.getBean("magician");
magician.interceptThoughts("ssss");
}
运行结果:
前置Intercepting volunter's thoughts
ssss
后置
----------------------------------------------------------基于注解前置通知---------------------------------------------------------------------------------
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <aop:aspectj-autoproxy></aop:aspectj-autoproxy><!-- 自定义配置文件 --> <bean id="magicianAspectJ" class="imple.MagicianAspectJ"/> </beans>
package imple; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; import inter.MindReader;
/**
* 读心者读取志愿者信息
*/
@Aspect
public class MagicianAspectJ implements MindReader{ private String thoughts; @Pointcut("execution(* inter.Thinker.thinkOfSomething(String)) && args(thoughts)")
public void thinking(String thoughts){ } @Before("thinking(thoughts)")
public void interceptThoughts(String thoughts) {
System.out.println("Intercepting volunteer's thoughts: " + thoughts);
this.thoughts = thoughts;
} public String getThoughts() {
return thoughts;
} }
@Test
public void test11(){
MagicianAspectJ magician = (MagicianAspectJ) ctx.getBean("magicianAspectJ");
magician.interceptThoughts("sdfsdf");
}
结果
Intercepting volunteer's thoughts: sdfsdf
spring含参数 环绕通知demo的更多相关文章
- spring 切面 前置后置通知 环绕通知demo
环绕通知: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...
- 阶段3 2.Spring_08.面向切面编程 AOP_8 spring中的环绕通知
环绕通知.method属性需要新加一个方法 在logger内中新加aroundPringLog方法 异常代码先注释掉 对比现在的环绕通知和之前写代理类做的环绕通知.右侧的方法内有明确的业务层方法(切入 ...
- [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring AOP--返回通知,异常通知和环绕通知
在上篇文章中学习了Spring AOP,并学习了前置通知和后置通知.地址为:http://www.cnblogs.com/dreamfree/p/4095858.html 在本文中,将继续上篇的学习, ...
- spring框架应用系列四:切面编程(环绕通知与前后置通知区别)
切面编程(环绕通知与前后置通知区别) 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7867034.html 解决问 ...
- 19Spring返回通知&异常通知&环绕通知
在前置通知和后置通知的基础上加上返回通知&异常通知&环绕通知 代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interfa ...
- 返回通知&异常通知&环绕通知
[返回通知] LoggingAspect.java: @Aspect @Component public class LoggingAspect { /* * 在方法正常执行后执行的通知叫返回通知 * ...
- 环绕通知(xml)
1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- java中AOP的环绕通知
pom.xml <dependencies> <dependency> <groupId>org.springframework</groupId> & ...
随机推荐
- 六:分布式事务一致性协议paxos的分析
最近研究paxos算法,看了许多相关的文章,概念还是很模糊,觉得还是没有掌握paxos算法的精髓,所以花了3天时间分析了libpaxos3的所有代码,此代码可以从https://bitbucket.o ...
- js记录用户在网站的浏览记录和停留时间
by weber开发者 from http://weber.pub/ 本文地址: http://weber.pub/js记录用户行为浏览记录和停留时间/163.html 问题 公司想统计一个用户从进入 ...
- 一段实现页面上的图片延时加载的js
大家如果使用firebug去查看的话就会发现,当你滚动到相应的行时,当前行的图片才即时加载的,这样子的话页面在打开只加可视区域的图片,而其它隐藏的图片则不加载,一定程序上加快了页面加载的速度,对于比较 ...
- hdu 5444 Elven Postman(根据先序遍历和中序遍历求后序遍历)2015 ACM/ICPC Asia Regional Changchun Online
很坑的一道题,读了半天才读懂题,手忙脚乱的写完(套上模板+修改模板),然后RE到死…… 题意: 题面上告诉了我们这是一棵二叉树,然后告诉了我们它的先序遍历,然后,没了……没了! 反复读题,终于在偶然间 ...
- Collectl基础
Collectl比nmon更专业和深入的linux性能测试工具 首先是安装 $ sudo apt-get install collectl 直接输入collectl就可以看到数据 默认是CPU,硬盘, ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 unselect_from_list_by_value(self, locator, *values)
def unselect_from_list_by_value(self, locator, *values): """Unselects `*values` from ...
- Android百度地图开发(二)地图覆盖物
上一篇文章写道如何在一个mapview中简单的显示地图:本次学习一下如何在地图上添加一些覆盖物. 1.设置显示交通地图: // 显示交通地图 mapView.setTraffic(true); 2.设 ...
- ifstream 流 判断文件是否结尾的函数eof(.xml
pre{ line-height:1; color:#800080; font-size:16px;}.sysFunc{color:#627cf6;font-style:italic;font-wei ...
- Ubuntu 16.04 TensorFlow CPU 版本安装
1.下载Anaconda,官方网站.我下载的时Python 2.7 64bit版本: 2.安装执行命令 bash Anaconda2-4.2.0-Linux-x86_64.sh 设置好目录后等 ...
- 时间日期Date类型
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...