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

  • 相关阅读:
    JavaScript的数据类型
    php字符串操作
    PHP快速入门
    JavaScript简介与使用方法
    《技术大牛的养成指南》--读书笔记
    Java并发编程-多线程
    分布式锁的实现方式和优缺点&Java代码实现
    Java操作Zookeeper
    排序二叉树、平衡二叉树、红黑树
    HashMap&Hashtable&LinkedHashMap&ConcurrentHashMap&Collections.synchronizedMap
  • 原文地址:https://www.cnblogs.com/leodaxin/p/9383879.html
Copyright © 2011-2022 走看看