`

在Spring中使用replaced-method来进行方法替换

 
阅读更多
<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog01.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
  我们知道,通过使用Spring的AOP,可以非常容易的增强类中一些方法的功能,或者是替换掉一个方法。这里简单介绍一种不使用Spring AOP,而是Spring IOC中内置的一种方法替换功能。即<bean>标签中<replaced-method>元素的应用。</replaced-method></bean>
  在Spring的配置文件中,在配置一个Bean的时候,可以使用该元素(标签)用来设置方法替换。<replaced-method>标签的name属性用来指定要替换的方法名称,replacer属性用来指定用来替换的Bean,这个Bean要求实现Spring的MethodReplacer接口。该标签下面的arg-type元素用来指定0个或多个方法参数。下面我们看一个简单的例子:<br>public class LookupMethodBean {<br>public void test()<br>{<br> System.out.println("原始方法!");<br>}<br>}<br><br><strong>MethodReplace.java<br></strong>import java.lang.reflect.Method;<br>import org.springframework.beans.factory.support.MethodReplacer;<br>public class MethodReplace implements MethodReplacer {<br>public Object reimplement(Object obj, Method method, Object[] args)<br>throws Throwable {<br> System.out.println("方法已经被替换!");<br>return null;<br>}<br>}</replaced-method>

Spring配置文件部分内容
<bean name="replacer" class="springroad.deomo.chap4.MethodReplace"><br></bean>
<bean name="testBean" class="springroad.deomo.chap4.LookupMethodBean"><br><replaced-method name="test" replacer="replacer"></replaced-method><br></bean>
 
  这样,testBean的test方法被替换,在调用testBean的test方法时,将执行replcacer这个Bean中的reimplement方法。


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1344361


分享到:
评论

相关推荐

    JAVA spring 系列案例50个和学习资料

    Spring系列第12篇:lazy-init:bean延迟初始化Spring系列第13篇:使用继承简化bean配置(abstract & parent)Spring系列第14篇:lookup-method和replaced-method比较陌生,怎么玩的?Spring系列第15篇:代理详解(Java...

    servlet2.4doc

    The default behavior of this method is to call addCookie(Cookie cookie) on the wrapped response object. addCookie(Cookie) - Method in interface javax.servlet.http.HttpServletResponse Adds the ...

    gulp-html-replace:替换HTML中的构建块。 像useref一样,但是做对了

    gulp-html-replace 替换HTML中的构建块。 像useref一样,但是做对了。目录用法安装: npm install --save-dev gulp-html-replace 在您HTML文件中放置一些块: &lt;!-- build:&lt;name&gt; --&gt;Everything here will be ...

    unigui0.83.5.820

    0.83.5.820 +---------------------------------------------------------------------------------------- - 0000796: DBGrid: Render bug when Column color is clWindow and project is created with 0.82 ...

    基于PaddleOCR文字识别实现视频中关键字替换或遮盖项目源码+使用说明.zip

    基于PaddleOCR文字识别实现视频中关键字替换或遮盖项目源码+使用说明.zip ```commandline python3 tools/infer_keyword/infer_end_to_end.py \ --keyword="aws" \ --image_dir=/home/jackdance/Desktop/aws_video/...

    gobble-replace:用 gobble 替换文本中的 &lt;@variables@>

    将文本中的 &lt;@variables@&gt; 替换为 gobble。 安装 首先,您需要安装 gobble - 有关详细信息,请参阅。 然后, npm i -D gobble-replace 用法 gobblefile.js var gobble = require ( 'gobble' ) ; module . ...

    replace-string-action:使用正则表达式替换字符串的 GitHub 操作

    replace-string GitHub...产出replaced 被替换的字符串。示例用法 uses : frabert/replace-string-action@v1.2with : pattern : ' Hello, (\w+)! ' string : ' Hello, world! ' replace-with : ' I greet you, $1! '

    替换文本文件中Ant表达式的Ant扩展任务

    该程序为Ant的扩展任务,在拷贝文本格式的文件的同时替换文件中的Ant表达式,类似于 ${property.name},属性值来自于Ant脚本加载的属性文件,如果没有属性文件中没有定义相关属性,那么保留原来的表达式;...

    Agilent-Openlab-ECM服务器操作规程.doc

    "Effective " " " " "Date " " "替换文件 "N/A "复审日期 " " "Replaced For " "Review Date " " "分发部门 " " "Distributed " " "to " " 1. 目的 建立Agilent Openlab ECM服务器的使用操作规程,确保仪器操作人员...

    remark-emoji:在文本中替换的markmarkdown转换器

    这是一个插件,用于将:emoji:替换为文本中的实际UTF-8表情符号。 演示版 您可以在下面的找到一个演示。 用法 remark().use(emoji [, options]); const remark = require ( 'remark' ) ; const emoji = require ( '...

    frs-replace:替换任何textfilepiped流内容的最快方法。 开箱即用随附NodeJS API和CLI!

    @ frsource / frs-replace 最快的()CLI和围绕Node包装器,可以进行即时替换(无论是否更改输入文件), ,等等!...5.在给定的内容字符串abcd中用b替换所有a ,并将结果保存到foo_replaced.js 6.从管道流中用b替换

    import-html-entry:导入html并接管脚本的导出

    import-html-entry 将索引html视为清单,并加载资产(css,js),从入口脚本获取导出。 &lt;!-- subApp/index.html --&gt;&lt;!DOCTYPE html &gt;&lt;...&lt; head &gt;... meta charset =" UTF-8 " &gt;...

    最新预训练模型ELECTRA.rar

    最近基于maskd langage modeling(MLM)的预训练模型,比如BERT,主要是使用[MASK]令牌替换输入序列中的部分令牌,然后训练一个模型来修复原来的令牌。虽然它们在下游的NLP任务中有良好的结果,但是模型往往需要大量的...

    acpi控制笔记本风扇转速

    The overhead of a semaphore per-method is eliminated. Fixed a regression where an error was no longer emitted if a control method attempts to create 2 objects of the same name. This once again ...

    ITUG729源代码

    replaced in Version 1.3 by lines 147 to 149 : ---------------------------------------------------------------------------- if(bfi == 1) { if(past_ftyp == 1) ftyp = 1; else ftyp = 0; *parm = ftyp; ...

    2011年移动互联网大趋势

    2011年移动互联网大趋势 ...- Players who cannot evolve at software speeds will eventually be replaced by alternatives (e.g. software has superseded carrier efforts in location, billing and distribution)

    mnit-replaced.github.io

    mnit-replaced.github.io

    Hi3520D_V100R001C01SPC022

    1.osdrv commands description: The design idea of this catalog is to fulfil the requirement that compiling the same set of source code with two different compilation tool chains. So an extra parameter ...

    大文件 文本 替换VC6源码

    把源文件的某一个字符替换成另外一个字符,并且生成目标文件.-put a source of a character replaced another character, and generates the target file. VC源码

Global site tag (gtag.js) - Google Analytics