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

  • 相关阅读:
    Oracle 删表前验证表名是否存在并且删除
    Mysql的建表规范与注意事项
    MYSQL总结之sql语句大全
    主机屋云服务器(绑定域名)初探
    (十)Thymeleaf用法——Themeleaf内联
    (九)Thymeleaf用法——Themeleaf注释
    (八)Thymeleaf的 th:* 属性之—— 模板布局& th:with& 属性优先级
    (七)Thymeleaf的 th:* 属性之—— th: ->设值& 遍历迭代& 条件判断
    (六)Thymeleaf的 th:* 属性之—— th: ->text& utext& href
    (五)Thymeleaf标准表达式之——[7->8]条件表达式& 默认表达式
  • 原文地址:https://www.cnblogs.com/leodaxin/p/9383879.html
Copyright © 2011-2022 走看看