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> & ...
随机推荐
- Android Studio 小技巧合集
本文翻译自 Android Studio Tips by Philippe Breault,一共收集了62个 Android Studio 使用小技巧和快捷键. 根据这些小技巧的使用场景,本文将这62 ...
- MySQL基础之第17章 MySQL日志
17.1.日志简介 二进制日志错误日志通用查询日志慢查询日志 17.2.二进制日志 二进制日志也叫作变更日志(update log),主要用于记录数据库的变化情况.通过二进制日志可以查询MySQL数据 ...
- hdu 2476(第一道区间dp)
题意:就是给定两个字符串,第一个是初始串,第二个是目标串,问你把初始串变到目标串最少需要多少串! 分析:此题分两步,第一步是假设开始的初始串是空串,然后就进行区间dp,dp[i][j]代表把区间[i, ...
- ylb:exists(存在)的应用实例
ylbtech-SQL Server:exists(存在)的应用实例 SQL Server exists(存在)的应用实例. 1,exists(存在)的应用实例 返回顶部 -- =========== ...
- Web 通信 之 长连接、长轮询(long polling)(转)
基于HTTP的长连接,是一种通过长轮询方式实现"服务器推"的技术,它弥补了HTTP简单的请求应答模式的不足,极大地增强了程序的实时性和交互性. 一.什么是长连接.长轮询? 用通俗易 ...
- Golang几个常用记录日志对比
go语言有一个标准库,log,提供了最基本的日志功能,但是没有什么高级的功能,如果需要高级的特性,就需要使用第三方包,下面是一些候选的包: go_tmlog https://code.google.c ...
- 代理抓取RSS信息
最近工作很闲,就自己写了一个可以看RSS订阅的网站.话说,RSS阅读器到处都是,随便下一个就可以了,为什么还去做一个网站形式的呢?作为一个热(xian)爱(de)前(dan)端(teng)的程序员,我 ...
- 数组名的含义.xml
pre{ line-height:1; color:#1e1e1e; background-color:#f0f0f0; font-size:16px;}.sysFunc{color:#627cf6; ...
- 朝鲜RedStar_OS_3.0安装图解
前天exploit-db上出现了3个Local Exploit,都是来自朝鲜的RedStar 3.0的vul.网上也下到了镜像,按网上的方法测试了下,真的是 ————————————————————— ...
- while (cin>>str)退出死循环
今天在练习的时候突然发现了这个问题,百度之感觉还挺常见的,故记之! //题目描述 // //写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串. // //输入描述 : //输入一个 ...