1.replaced-method 子元素
方法替换: 可以在运行时用新的方法替换现有的方法,与之前的 look-up不同的是replace-method 不但可以动态地替换返回的实体bean,而且可以动态的更改原有方法的逻辑,
1.1.1使用实例:
首先创建一个Bean完成某项业务
public class Person { public void show() {
System.out.println("I am Person ..");
}
}
在运营一段时间后需要改变原有的逻辑
import java.lang.reflect.Method; import org.springframework.beans.factory.support.MethodReplacer; public class ReplacedClass implements MethodReplacer { @Override
public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
System.out.println(obj);
System.out.println(method.getName());
System.out.println("I am replacer...");
return null;
} }
XML 如下
<?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.5.xsd"> <bean id="person" class="test.replaced.Person">
<replaced-method name="show" replacer="replace"></replaced-method>
</bean>
<bean id="replace" class="test.replaced.ReplacedClass">
</bean>
</beans>
测试类
public class Main { public static String XML_PATH = "test\\replaced\\applicationContxt.xml"; public static void main(String[] args) {
try {
Resource resource = new ClassPathResource(XML_PATH);
XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
Person bean = (Person) beanFactory.getBean("person");
System.out.println(bean);
bean.show();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
好了,运行测试就可以看到我们预期的结果了,控制台成功打印出
test.replaced.Person$$EnhancerBySpringCGLIB$$7f7813f2@1a69b07
test.replaced.Person$$EnhancerBySpringCGLIB$$7f7813f2@1a69b07
show
I am replacer...
知道了这个子元素的用法,我们再来看看Spring是怎么对这个元素进行提取的.
BeanDefinitionParserDelegate.java
/**
* Parse replaced-method sub-elements of the given bean element.
*/
public void parseReplacedMethodSubElements(Element beanEle, MethodOverrides overrides) {
NodeList nl = beanEle.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
//仅当在Spring默认Bean的子元素下,
//且为<lookup-method 时有效
if (isCandidateElement(node) && nodeNameEquals(node, REPLACED_METHOD_ELEMENT)) {
Element replacedMethodEle = (Element) node;
// 提取要替换的旧的方法
String name = replacedMethodEle.getAttribute(NAME_ATTRIBUTE);
// 提取对应的新的替换的方法
String callback = replacedMethodEle.getAttribute(REPLACER_ATTRIBUTE);
ReplaceOverride replaceOverride = new ReplaceOverride(name, callback);
// Look for arg-type match elements.
List<Element> argTypeEles = DomUtils.getChildElementsByTagName(
replacedMethodEle, ARG_TYPE_ELEMENT);
for (Element argTypeEle : argTypeEles) {
String match = argTypeEle.getAttribute(ARG_TYPE_MATCH_ATTRIBUTE);
// 记录参数
match = (StringUtils.hasText(match) ? match
: DomUtils.getTextValue(argTypeEle));
if (StringUtils.hasText(match)) {
replaceOverride.addTypeIdentifier(match);
}
}
replaceOverride.setSource(extractSource(replacedMethodEle));
overrides.addOverride(replaceOverride);
}
}
}
我们可以看到,无论是lookup-method 还是 replace-method 都是构造了一个methodOverride ,并且最终记录在AbstractBeanDefinition 中的 methodOerrides 属性当中.
而这个属性如何使用,我们后续再继续研究.