0
点赞
收藏
分享

微信扫一扫

【Spring】- 任意方法替换

往复随安_5bb5 2024-01-31 阅读 9
【任意方法替换】

方法注入的一种不太有用的形式是能够用另一种方法实现替换托管 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"/>


举报

相关推荐

0 条评论