zoukankan      html  css  js  c++  java
  • spring replaced method 注入

     
         replaced method注入是spring动态改变bean里方法的实现。需要改变的方法,使用spring内原有其他类(需要继承接口org.springframework.beans.factory.support.MethodReplacer)的逻辑,替换这个方法。通过改变方法执行逻辑来动态改变方法。内部实现为使用cglib方法,重新生成子类,重写配置的方法和返回对象,达到动态改变的效果。
     
         实例如下:
    public class MyValueCalculator {
     
        public String computeValue(String input) {
            // some real code...
        }
     
        // some other methods...
     
    }
     
    /**
     * meant to be used to override the existing computeValue(String)
     * 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 ...;
        }
    }
     
    配置:
    <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"/>
     
    注意:由于采用cglib生成之类的方式,所以需要用来动态注入的类,不能是final修饰的;需要动态注入的方法,也不能是final修饰的。
     
     
     
     
  • 相关阅读:
    zoj 2001 Adding Reversed Numbers
    hdu 2391 Filthy Rich (简单dp)
    hdu 1128 (hash)
    华为交换机有关BGP的相关配置
    Cent os 6.8添加中文字体
    Liunx之始
    自己鼓励自己
    UML用例图中包含(include)、扩展(extend)和泛化(generalization)三种关系详解(转载)
    20091125心情感觉还不错
    DataTable 行列转换
  • 原文地址:https://www.cnblogs.com/sten/p/5745920.html
Copyright © 2011-2022 走看看