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修饰的。
 
 
 
 

最新文章

  1. js 父窗体
  2. 【解决方案】VS2013外部工具中添加ildasm.exe
  3. JavaScript TDD with Mocha
  4. Eclipse is running in a JRE, but a JDK is required 解决方法
  5. linux命令——rmdir
  6. CTE-递归[2]
  7. 转载:CURL常用命令
  8. elasticsearch 性能调优
  9. CSS使用心得小结
  10. 配置 CSV Data Set Config 来参数化新增客户信息操作
  11. coreData的ManagedObject后,报错
  12. Introducing Apache Spark Datasets(中英双语)
  13. Android性能优化-减小图片下载大小
  14. PHP更改自动加载的顺序
  15. C语言--第二周作业评分和总结(5班)
  16. 【bzoj2754】 SCOI2012—喵星球上的点名
  17. win32下编译glog
  18. CUDA ---- Memory Access
  19. java的break,另一种用法(多层循环嵌套)
  20. UIImagePickerController本地图片视频,相机录像机使用

热门文章

  1. ubuntu desktop 登录root账户
  2. 打开app应用
  3. Node中导入模块require和import??
  4. 【转载】49个CSS知识点
  5. Cassandra的安装
  6. eclipse搭建springboot的项目
  7. pytorch中F.softmax(x1,dim = -1) dim 取值测试及验证
  8. Hadoop学习(6)-HBASE的安装和命令行操作和java操作
  9. final关键字、多态 (札记)
  10. Vue.js 父子组件相互传递数据