ITeye博客

替换任意的方法

prodream · 2008-08-24 18:06 · Spring · Markdown

在spring中不但提供了Lookup方法来注射方法,还提供了另一种方法注射,它在运行时,用另一个方法来替换Bean的任意一个方法。这有点类似于C++的重载,但不如lookup常用

形如以下:

希望重写MyValueCalculator类的computeValue()方法

...

public class MyValueCalculator{

public String computeValue(){

计算功能实现

}

....

}

使用方法替换需要实现org,springframework.beans.factory.suppor.MethodReplacer接口

public class ReplacementComputeValue implments MethodReplacer{

public Object reimplement(Object o,Method m,Object[] args)

throws Throwable{

String input=(String) agrs[0];

...

return ...;

}

配置如下:

<bean id="myValueCalculator" class="prodream.com.MyValueCalculator">

<replaced-method name="computerValue"

replacer="replacementComputeValue">

</replaced-method>

</bean>

<bean id="replacementComputeValue" class="prodream.com.ReplacementComputeValue">

</bean>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。