zoukankan      html  css  js  c++  java
  • Spring之强制修改某个方法的行为(Arbitrary method replacement)

     

     

    A less commonly useful form of method injection than Lookup Method Injection is the ability to replace arbitrary methods in a managed bean with another method implementation. Users may safely skip the rest of this section (which describes this somewhat advanced feature), until this functionality is actually needed.

    In an XmlBeanFactory, the replaced-method element may be used to replace an existing method implementation with another, for a deployed bean. Consider the following class, with a method computeValue, which we want to override:

    ...
    public class MyValueCalculator {
      public String computeValue(String input) {
        ... some real code
      }
    
      ... some other methods
    }
    

      

    A class implementing the org.springframework.beans.factory.support.MethodReplacer interface is needed to provide the new method definition.

    /** meant to be used to override the existing computeValue
        implementation in MyValueCalculator */
    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 ...;
    }
    

      

    The BeanFactory deployment definition to deploy the original class and specify the method override would look like:

    <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">
    </bean>
    

      

    One or more contained arg-type elements within the replaced-method element may be used to indicate the method signature of the method being overridden. Note that the signature for the arguments is actually only needed in the case that the method is actually overloaded and there are multiple variants within the class. For convenience, the type string for an argument may be a substring of the fully qualified type name. For example, all the following would match java.lang.String.

        java.lang.String
        String
        Str

    Since the number of arguments is often enough to distinguish between each possible choice, this shortcut can save a lot of typing, by just using the shortest string which will match an argument.

    3.3.5. Using depends-on

  • 相关阅读:
    js + html 实现视频截图
    检测浏览器版本是否支持webp
    【安装系统】win8装win7遇到的一些坑
    《说文解字》与程序设计
    朝花夕拾——更新两个开源项目
    js发送和接收二进制字节流数据
    字符编码--丛起原到代码
    JavaScript进行WebSocket字节流通讯示例
    JavaScript进行UTF-8编码与解码
    JS字符串与二进制的相互转化
  • 原文地址:https://www.cnblogs.com/leodaxin/p/9383879.html
Copyright © 2011-2022 走看看