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

  • 相关阅读:
    goj 来自不给标题的菜鸟出题组(巴什博弈+素数判定)
    goj 城市交通线(简单并查集)
    ACM_鸡兔同笼(二元一次方程)
    ACM_魔仙岛探险(深搜)
    ACM_螺旋填数
    ACM_开心消消乐
    构建工具是如何用 node 操作 html/js/css/md 文件的
    学习使用ExpressJS 4.0中的新Router
    请求时token过期自动刷新token
    Express的基本使用
  • 原文地址:https://www.cnblogs.com/leodaxin/p/9383879.html
Copyright © 2011-2022 走看看