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 的一个示例
- 实现 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();
}
}
- 定义一个具体类,该类的某个重载方法想要被容器覆盖,该类包含 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;
}
}
- spring 配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans SYSTEM "spring-beans.dtd">
<beans>
<bean id="persona" singleton="true"
class="com.willhonor.test.useApplicationContext3LookupOrReplaceMethods.PersonA_use_lookup_method">
<replaced-method name="getAFFruit" replacer="methodReplacer">
<arg-type match="java.lang.String"></arg-type>
</replaced-method>
<replaced-method name="getAFFruit" replacer="methodReplacer">
<arg-type match="java.util.List"></arg-type>
</replaced-method>
</bean>
<bean id="methodReplacer" singleton="true"
class="com.willhonor.test.useApplicationContext3LookupOrReplaceMethods.MMethodReplacer"></bean>
</beans>
- 测试代码如下:
package com.willhonor.test.useApplicationContext3LookupOrReplaceMethods;
import java.util.List;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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("++++++++");
}
}
- 测试执行结果如下,可见重载方法:getAFFruit(String str) 和 getAFFruit(List list) 都被 IOC 容器 override 覆盖了:
```shell
...
...
++++++++
invoke 0
null
++++++++
String-Obj:from method-replacer-0fc177c9-f8d5-4e01-8fbb-a50554eff2cf
++++++++
String-Obj:from method-replacer-d6894628-f17d-4f8e-847a-c5c9b314d992
++++++++