spring replaced method 注入
2024-09-08 05:23:50
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修饰的。
最新文章
- js 父窗体
- 【解决方案】VS2013外部工具中添加ildasm.exe
- JavaScript TDD with Mocha
- Eclipse is running in a JRE, but a JDK is required 解决方法
- linux命令——rmdir
- CTE-递归[2]
- 转载:CURL常用命令
- elasticsearch 性能调优
- CSS使用心得小结
- 配置 CSV Data Set Config 来参数化新增客户信息操作
- coreData的ManagedObject后,报错
- Introducing Apache Spark Datasets(中英双语)
- Android性能优化-减小图片下载大小
- PHP更改自动加载的顺序
- C语言--第二周作业评分和总结(5班)
- 【bzoj2754】 SCOI2012—喵星球上的点名
- win32下编译glog
- CUDA ---- Memory Access
- java的break,另一种用法(多层循环嵌套)
- UIImagePickerController本地图片视频,相机录像机使用