博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring基本使用(元素replaced-method使用)
阅读量:4109 次
发布时间:2019-05-25

本文共 2700 字,大约阅读时间需要 9 分钟。

0.0
  • 当元素 lookup-method 和 replaced-method 一起使用时,元素 lookup-method 的优先级更高,会覆盖元素 replaced-method。所以应当避免同时使用这两个元素来配置同一个 method。
  • 元素 replaced-method 可以实现元素 lookup-method 的功能,但是它更强大,接口 org.springframework.beans.factory.support.MethodReplacer 和该元素配合使用。
  • 元素 replaced-method 可以使用其子元素 arg-type 来指定参数类型(字符串,例如类的 FQN),如果没有指定任何 arg-type 元素,则表示该方法无参。
  • 元素 lookup-method 可以悄无声息的覆盖掉某个 bean 的某个 method。而元素 replaced-method 则可以提供更精细的控制,对于应该选取指定名称的哪个方法(即 多个重载方法的场景)。
1. 使用元素 replaced-method 的一个示例
  1. 实现 MethodReplacer 接口
package com.willhonor.test.useApplicationContext3LookupOrReplaceMethods;import java.lang.reflect.Method;import java.util.UUID;import org.springframework.beans.factory.support.MethodReplacer;public class MMethodReplacer implements MethodReplacer{
public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
return "String-Obj:from method-replacer-" + UUID.randomUUID(); }}
  1. 定义一个具体类,该类的某个重载方法想要被容器覆盖,该类包含 3 个重载方法:
package com.willhonor.test.useApplicationContext3LookupOrReplaceMethods;import java.util.List;public class PersonA_use_lookup_method {
public Object getAFFruit() {
System.out.println("invoke 0"); return null; } public Object getAFFruit(String str) {
System.out.println("invoke string"); return null; } public Object getAFFruit(List
list) {
System.out.println("invoke list"); return null; }}
  1. spring 配置文件如下:
  1. 测试代码如下:
package com.willhonor.test.useApplicationContext3LookupOrReplaceMethods;import java.util.List;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * bean 元素的 lookup-method/replaced-method 属性的使用 * @author jokee * */public class Test_Test_1 {
@Test public void test_use_repalced_method() throws Exception {
String pathA = "com/willhonor/test/configs/application.d2.xml"; String[] path = new String[] {
pathA}; ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(path); // PersonA_use_lookup_method persona = (PersonA_use_lookup_method) context.getBean("persona"); System.out.println("++++++++"); System.out.println(persona.getAFFruit()); System.out.println("++++++++"); System.out.println(persona.getAFFruit("")); System.out.println("++++++++"); System.out.println(persona.getAFFruit((List
)null)); System.out.println("++++++++"); }}
  1. 测试执行结果如下,可见重载方法:getAFFruit(String str) 和 getAFFruit(List list) 都被 IOC 容器 override 覆盖了:
```shell...#此处省略 spring 日志打印...++++++++invoke 0null++++++++String-Obj:from method-replacer-0fc177c9-f8d5-4e01-8fbb-a50554eff2cf++++++++String-Obj:from method-replacer-d6894628-f17d-4f8e-847a-c5c9b314d992++++++++

转载地址:http://ojlsi.baihongyu.com/

你可能感兴趣的文章
XML生成(一):DOM生成XML
查看>>
XML生成(三):JDOM生成
查看>>
Ubuntu Could not open lock file /var/lib/dpkg/lock - open (13:Permission denied)
查看>>
collect2: ld returned 1 exit status
查看>>
C#入门
查看>>
查找最大值最小值
查看>>
C#中ColorDialog需点两次确定才会退出的问题
查看>>
数据库
查看>>
nginx反代 499 502 bad gateway 和timeout
查看>>
linux虚拟机安装tar.gz版jdk步骤详解
查看>>
python猜拳游戏
查看>>
python实现100以内自然数之和,偶数之和
查看>>
python数字逆序输出及多个print输出在同一行
查看>>
ESP8266 WIFI数传 Pixhaw折腾笔记
查看>>
苏宁产品经理面经
查看>>
百度产品经理群面
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
element ui 弹窗在IE11中关闭时闪现问题修复
查看>>
vue 遍历对象并动态绑定在下拉列表中
查看>>
Vue动态生成el-checkbox点击无法选中的解决方法
查看>>