spring文档把方法替换归为一种注入形式,不过它和你之前见过的注入有很大的不同,借助方法注入,我们可以任意替换bean方法的实现,同时无需改变你正在修改的bean之源代码
这是一个简单bean的实现
package
ch5.MethodReplace;

public
class
ReplacementTarget
...
{
public String fromMessage(String msg)...{
return msg;
}
public int fromMessage(int msg )...{
return 1;
}
}
借助spring的方法替换,你可以替换ReplacementTarget类的任意方法,本例替换了String参数的fromMessage
首先,创造一个实现MethodReplacer接口的实现
package
ch5.MethodReplace;
import
java.lang.reflect.Method;
import
org.springframework.beans.factory.support.MethodReplacer;

public
class
FromMessageReplacement
implements
MethodReplacer
...
{
public Object reimplement(Object object, Method method, Object[] arg)
throws Throwable ...{
if(isFromMessageMethod(method))...{
String msg=(String)arg[0];
return "gaoxiang@replacement";
}
else...{
return "error appear";
}
}

public boolean isFromMessageMethod(Method method)...{
//检查参数个数
if(method.getParameterTypes().length!=1)...{
return false;
}
//检查方法名
if(!method.getName().equals("fromMessage"))...{
return false;
}
//检查返回值类型
if(method.getReturnType()!=String.class)...{
return false;
}
//检查参数类型
if(method.getParameterTypes()[0]!=String.class)...{
return false;
}
return true;
}
}
Object为原有方法被调用的那个bean
Method位实例代表要覆写的方法
Objects[]代表传入方法的参数
配置文件:
<?
xml version="1.0" encoding="UTF-8"
?>
<
beans
xmlns
="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
>



<
bean
id
="methodReplacer"
class
="ch5.MethodReplace.FromMessageReplacement"
/>

<
bean
id
="standardTarget"
class
="ch5.MethodReplace.ReplacementTarget"
/>

<
bean
id
="replacementTarget"
class
="ch5.MethodReplace.ReplacementTarget"
>
<
replaced-method
name
="fromMessage"
replacer
="methodReplacer"
>
<
arg-type
>
string
</
arg-type
>
</
replaced-method
>
</
bean
>

</
beans
>
我们把fromMessage(String msg)的返回值用一个返回gaoxiang@replacement的方法替换
测试代码:
package
ch5.MethodReplace;
import
java.io.File;
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import
org.springframework.beans.factory.xml.XmlBeanFactory;
import
org.springframework.core.io.FileSystemResource;
import
org.springframework.util.StopWatch;

public
class
TestSpring
...
{
public static void main(String args[]) throws Exception...{
//获取bean factory
ConfigurableListableBeanFactory factory=(ConfigurableListableBeanFactory)getBeanFactory(); //使用子beanFactory
ReplacementTarget standardTarget=(ReplacementTarget)factory.getBean("standardTarget");
ReplacementTarget replacementTarget=(ReplacementTarget)factory.getBean("replacementTarget");
displayInfo(standardTarget);
displayInfo(replacementTarget);
}
public static BeanFactory getBeanFactory()...{
//获取bean factory
String realpath="";
//加载配置项
realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/MethodReplace"+File.separator+"applicationContext.xml";
ConfigurableListableBeanFactory factory=new XmlBeanFactory(new FileSystemResource(realpath));
return factory;
}

public static void displayInfo(ReplacementTarget target)...{
System.out.println(target.fromMessage("hello world"));
StopWatch stopWatch=new StopWatch();
stopWatch.start("TestSpring");
for (int i = 0; i < 1000000; i++) ...{
String out=target.fromMessage("foo");
}
stopWatch.stop();
System.out.println(stopWatch.getTotalTimeMillis());
}
}
运行结果:
hello world
16
gaoxiang@replacement
3015
如果,不进行类型检查,效率会有一点点提升,如下:
hello world
16
gaoxiang@replacement
2188
使用建议:
1.针对每个方法或每组重载方法使用一个MethodPlacer,避免针对大量无关方法使用一个MethodReplacer
2.不一定非要检查类型检查,如果注重性能,可以给MethodPlacer注入一个boolean开关,支持检查的open/close
本文介绍了Spring框架中的Method Replacement特性,这是一种特殊的注入方式,允许在不修改原始bean源代码的情况下替换bean方法的实现。通过创建MethodReplacer接口的实现并配置,可以灵活替换目标方法的行为。在示例中,演示了如何替换一个接收String参数的方法,并给出了测试结果。建议在使用时,针对每个方法单独创建MethodReplacer,以优化性能。
2197

被折叠的 条评论
为什么被折叠?



