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> & ...
随机推荐
- 关于微信扫描二维码下载apk文件的细节设计
微信使用的人数越来越多,渐渐的用户形成了一种习惯,扫描二维码的时候,也会打开微信去扫描,但是微信不支持第三方的链接下载,有些厂商已经发现了这一特点,所以在使用二维码下载自家的app时,会做一个提示,引 ...
- [转] 接触C# 反射 2
逆心 原文 反射基础,2013-4. 反射用于在程序运行过程中,获取类里面的信息或发现程序集并运行的一个过程.通过反射可以获得.dll和.exe后缀的程序集里面的信息.使用反射可以看到一个程序集内部的 ...
- [Everyday Mathematics]20150221
设 $y_n=x_n^2$ 如下归纳定义: $$\bex x_1=\sqrt{5},\quad x_{n+1}=x_n^2-2\ (n=1,2,\cdots). \eex$$ 试求 $\dps{\vl ...
- MySql通用分页存储过程
MySql通用分页存储过程 1MySql通用分页存储过程 2 3过程参数 4p_cloumns varchar(500),p_tables varchar(100),p_where varchar(4 ...
- C++模板知识小结
模板定义:模板就是实现代码重用机制的一种工具,它可以实现类型参数化,即把类型定义为参数, 从而实现了真正的代码可重用性.模版可以分为两类,一个是函数模版,另外一个是类模版. 由于类模板包含类型参数,因 ...
- 11、NFC技术:NDEF Uri格式解析
NDEF Uri格式规范 与NDEF文本格式一样,存储在NFC标签中的Uri也有一定的格式 http://www.nfc-forum.org/specs/spec_dashboard 编写可以解析Ur ...
- linux中ulimit作用
一.作用 Linux对于每个用户,系统限制其最大进程数.为提高性能,可以根据设备资源情况,设置各linux 用户的最大进程数. ulimit主要是用来限制进程对资源的使用情况的,它支持各种类型的限制, ...
- robotframework学习
下载地址: https://pypi.python.org/pypi/robotframework Installation If you already have Python with pip i ...
- Linux环境Weblogic10g服务部署
1.先安装XManager: 2.进入XShell,远程连接Linux主机后,按如下操作即可打开XManager配置WebLogic部署服务: [root@server36 bin]# cd /[ro ...
- ajax实现md5加密
一个asp.net ajax例子,使用jquery,实现md5加密..NET 4.0,Visual Studio 2010以上.效果体验:http://tool.keleyi.com/t/md5.ht ...