题目:有一个懂得读心术的人需要完成两件事情:截听志愿者的内心感应和显示他们在想什么

 <?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的更多相关文章

  1. spring 切面 前置后置通知 环绕通知demo

    环绕通知: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  2. 阶段3 2.Spring_08.面向切面编程 AOP_8 spring中的环绕通知

    环绕通知.method属性需要新加一个方法 在logger内中新加aroundPringLog方法 异常代码先注释掉 对比现在的环绕通知和之前写代理类做的环绕通知.右侧的方法内有明确的业务层方法(切入 ...

  3. [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. Spring AOP--返回通知,异常通知和环绕通知

    在上篇文章中学习了Spring AOP,并学习了前置通知和后置通知.地址为:http://www.cnblogs.com/dreamfree/p/4095858.html 在本文中,将继续上篇的学习, ...

  5. spring框架应用系列四:切面编程(环绕通知与前后置通知区别)

    切面编程(环绕通知与前后置通知区别) 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7867034.html 解决问 ...

  6. 19Spring返回通知&异常通知&环绕通知

    在前置通知和后置通知的基础上加上返回通知&异常通知&环绕通知 代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interfa ...

  7. 返回通知&异常通知&环绕通知

    [返回通知] LoggingAspect.java: @Aspect @Component public class LoggingAspect { /* * 在方法正常执行后执行的通知叫返回通知 * ...

  8. 环绕通知(xml)

    1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  9. java中AOP的环绕通知

    pom.xml <dependencies> <dependency> <groupId>org.springframework</groupId> & ...

随机推荐

  1. 使用crs_setperm修改RAC资源的所有者及权限

    Oracle RAC 集群中,对于各种资源的管理,也存在所有者与权限的问题.crs_getperm与crs_setperm则是这样的一对命令,主要用于查看与修改集群中resource的owner,gr ...

  2. android中的所谓观察者模式

    生活中我们常认定某些人很有才,但什么是有才呢?明朝的王守仁曾这样解释:才,是所谓天理,应用到物上,便成了才.凡事凡物,只要掌握了所谓科学的方法,并能灵活运用,那么你也可以成为一个有才的人. 观察者模式 ...

  3. JS面向对象组件(六) -- 拖拽功能以及组件的延展

    HTML部分 <div id="div1"></div> <div id="div2"></div> CSS部分 ...

  4. Bootstrap初级用户谈谈网页在手机上的显示效果优化

    本人之前已经使用Bootstrap有一段时间了,但是之前做出的网站都只是在电脑端使用,没有注意过手机端的显示效果.这两天自己使用Bootstrap做了一个简单的Web个人日志系统,想在手机端也使用,桌 ...

  5. XSS 前端防火墙(4):天衣无缝的防护

    例如,我们的属性钩子只考虑了 setAttribute,却忽视还有类似的 setAttributeNode.尽管从来不用这方法,但并不意味人家不能使用. 例如,创建元素通常都是 createEleme ...

  6. Redis 主从 Replication 的配置,主从复制原理

    概述 Redis的replication机制允许slave从master那里通过网络传输拷贝到完整的数据备份.具有以下特点: 异步复制.从2.8版本开始,slave能不时地从master那里获取到数据 ...

  7. A.xml

    pre{ line-height:1; color:#1e1e1e; background-color:#f0f0f0; font-size:16px;}.sysFunc{color:#627cf6; ...

  8. MSDN 中 对vector::erase()的解释.xml

    pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...

  9. LRU Cache的实现

      代码如下:     来自为知笔记(Wiz)

  10. Python:映像、集合

    一.字典 字典(dictionary)是Python中唯一的“映射”类型,映射这个概念在高中就学过:一个函数f将键(key, 定义域)映射到值(value, 值域).这样的函数在字典中可以称为哈希(H ...