【任意方法替换】
方法注入的一种不太有用的形式是能够用另一种方法实现替换托管 bean 中的任意方法
实现org.springframework.beans.factory.support.MethodReplacer
接口的类提供了新的方法定义
public class MyValueCalculator {
public String computeValue(String input) {
// some real code...
}
// some other methods...
}
public class ReplacementComputeValue implements MethodReplacer {
public Object reimplement(Object o, Method m, Object[] args) throws Throwable {
// get the input value, work with it, and return a computed result
String input = (String) args[0];
...
return ...;
}
}
<bean id="myValueCalculator" class="x.y.z.MyValueCalculator">
<!-- arbitrary method replacement -->
<replaced-method name="computeValue" replacer="replacementComputeValue">
<arg-type>String</arg-type>
</replaced-method>
</bean>
<bean id="replacementComputeValue" class="a.b.c.ReplacementComputeValue"/>