Spring基本使用(元素replaced-method使用)

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 配置文件如下:
<?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">
<!-- 		覆盖重载方法:getAFFruit(String str) -->
		<replaced-method name="getAFFruit" replacer="methodReplacer">
			<arg-type match="java.lang.String"></arg-type>
		</replaced-method>
<!-- 		覆盖重载方法:getAFFruit(List list) -->
		<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>
  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 0
null
++++++++
String-Obj:from method-replacer-0fc177c9-f8d5-4e01-8fbb-a50554eff2cf
++++++++
String-Obj:from method-replacer-d6894628-f17d-4f8e-847a-c5c9b314d992
++++++++

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值