`
BradyZhu
  • 浏览: 247728 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring In Action读书笔记之五------------------AOP的参数传递

 
阅读更多

在AOP方法拦截的时候,总会有一些参数的传递,我们可能希望在调用某个方法的时候,将这些方法悄悄的传递给

暗中的拦截方法,这样的功能如何实现呢,下面我们就这个参数传递的例子来进行一个案例。

首先书写一个接口,这个是暗中的拦截方法执行的接口

package com.bird.springidol;

public interface MindReader {
	public void interceptThoughts(String thoughts);
	public String getThought();
}

然后是他的实现类

package com.bird.springidol;

public class Magician implements MindReader {
	private String thoughts;

	@Override
	public void interceptThoughts(String thoughts) {
		System.out.println("Intercepting volunteer's thoughts");
		this.thoughts = thoughts;
		getThought();
	}

	@Override
	public String getThought() {
		// TODO Auto-generated method stub
		System.out.println("daw"+thoughts);
		return thoughts;
	}

}

然后下面是需要拦截的方法的接口

package com.bird.springidol;

public interface Thinker {
	public void thinkOfSomething(String thoughts);
	public String getThoughts();
}

然后是实现类

package com.bird.springidol;

public class Volunteer implements Thinker {
	private String thoughts;

	@Override
	public void thinkOfSomething(String thoughts) {
		this.thoughts=thoughts;
	}
	
	public String getThoughts(){
		return thoughts;
	}

}

接着就是配置了

<aop:config>
    	<aop:aspect ref="magician">
    		<aop:pointcut id="thinking"
    		expression="execution(* com.bird.springidol.Thinker.thinkOfSomething(String)) and args(thoughts)"/>
    		
    	<aop:before method="interceptThoughts" pointcut-ref="thinking" arg-names="thoughts"/>
    	
    	</aop:aspect>
    </aop:config>

这个配置的意思就是,拦截这个Thinker接口的实现方法,其中的参数命名为thoughts,下面的before方法里面的

interceptThoughts方法接受一个参数,这个参数映射到thoughts这个地方,然后暗中的拦截方法就能接受到参数了。

其实感觉挺扰人的,得好好想想做做例子就会了。

我的测试例子

@Test
	public void test10(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		Thinker ju = (Thinker) ctx.getBean("volunteer");
		ju.thinkOfSomething("Queen of Hearts");
		
	}

运行结果为

2012-5-22 12:25:05 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@167d940: startup date [Tue May 22 12:25:05 CST 2012]; root of context hierarchy
2012-5-22 12:25:05 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2012-5-22 12:25:05 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@ef137d: defining beans [org.springframework.aop.config.internalAutoProxyCreator,duke,duke1,sonnet29,poeticDuke,theStage,saxophone,guitar,kenny,hank,hank1,car1,audience,perform,magician,volunteer,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,performance,org.springframework.aop.aspectj.AspectJPointcutAdvisor#4,thinking]; root of factory hierarchy
Intercepting volunteer's thoughts
dawQueen of Hearts


分享到:
评论

相关推荐

    J2EE框架_笔记_c

    48Spring_AOP笔记 49-Spring_JDBC模板笔记 50-Spring_Struts、Hibernate支持笔记 51-52使用Struts + Spring + Hibernate完成用户登陆笔记 53使用Struts + Spring + Hibernate完成分页笔记 54留言管理程序_Struts + ...

    J2EE框架_笔记_b

    48Spring_AOP笔记 49-Spring_JDBC模板笔记 50-Spring_Struts、Hibernate支持笔记 51-52使用Struts + Spring + Hibernate完成用户登陆笔记 53使用Struts + Spring + Hibernate完成分页笔记 54留言管理程序_Struts + ...

    J2EE三大框架_笔记_a

    48Spring_AOP笔记 49-Spring_JDBC模板笔记 50-Spring_Struts、Hibernate支持笔记 51-52使用Struts + Spring + Hibernate完成用户登陆笔记 53使用Struts + Spring + Hibernate完成分页笔记 54留言管理程序_Struts + ...

    javaEE框架笔记,识货人下

    48Spring_AOP笔记.pdf 49-Spring_JDBC模板笔记.pdf 50-Spring_Struts、Hibernate支持笔记.pdf 51-52使用Struts + Spring + Hibernate完成用户登陆笔记.pdf 53使用Struts + Spring + Hibernate完成分页笔记.pdf 54...

    xmljava系统源码-SpringInAction4:《SpringInAction4th》学习笔记

    SpringInAction4 《Spring In Action 4th》学习笔记 第一部分 Spring的核心 1. Spring之旅 依赖注入 AOP bean的初始化过程 spring容器 2. 装配Bean “initialization on demand holder”创建单例模式的理解,参考 ...

    spring项目开发学习笔记

    Spring是一个轻量级的DI/IoC和AOP容器框架。存在的目的是用于构建轻量级的J2EE应用。 轻量级:应用大小和应用开支,包括应用方式 依赖注入DI/IoC控制反转:提供松耦合的一种实现技术 AOP面向切面编程:(可以在不...

    springmybatis

    mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...

    Java学习笔记-个人整理的

    {5.2.1}将浮点数四舍五入到指定精度}{98}{subsection.5.2.1} {6}Exception}{99}{chapter.6} {6.1}\ttfamily try-catch}{99}{section.6.1} {6.2}\ttfamily finally}{100}{section.6.2} {6.3}\ttfamily throws}{...

    JBoss ESB 学习笔记

    自己辛苦整理的网上的...13——第十二个ESB代码Spring AOP 113 14——第十三个ESB代码Transform CSV to XML 122 15——第十四个ESB代码Transform XML to POJO 128 16——第十五个ESB代码Web Service Consumer 1 151

    spring学习笔记

    Spring的Ioc Spring的AOP , AspectJ Spring的事务管理 , 三大框架的整合 目录 1.1 Spring 框架学习路线:..........................................................................................................

    Java/JavaEE 学习笔记

    第三章 Spring AOP(面向切面编程)..........351 第四章 Spring中的数据访问..........353 CVS学习笔记.................355 PL/SQL学习笔记............358 第一章 PL/SQL概述........................358 第二章 ...

    J2EE学习笔记(J2ee初学者必备手册)

    第三章 Spring AOP(面向切面编程)..........351 第四章 Spring中的数据访问..........353 CVS学习笔记.................355 PL/SQL学习笔记............358 第一章 PL/SQL概述........................358 第二章 PL...

Global site tag (gtag.js) - Google Analytics