Skip to content

Commit 983e2ce

Browse files
tempredirectphilwebb
authored andcommitted
Allow 'arg-type' matches against element body
Allow the body of 'arg-type' XML elements to be used as an alternative to 'match' attribute when defining a 'replace-method' in XML configuration. This change has been introduced primarily to support the samples printed in the Apress 'Pro Spring' book. Issue: SPR-9812 Backport-Issue: SPR-9929 Backport-Commit: 0709c03
1 parent ea49c8b commit 983e2ce

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -826,7 +826,11 @@ public void parseReplacedMethodSubElements(Element beanEle, MethodOverrides over
826826
// Look for arg-type match elements.
827827
List<Element> argTypeEles = DomUtils.getChildElementsByTagName(replacedMethodEle, ARG_TYPE_ELEMENT);
828828
for (Element argTypeEle : argTypeEles) {
829-
replaceOverride.addTypeIdentifier(argTypeEle.getAttribute(ARG_TYPE_MATCH_ATTRIBUTE));
829+
String match = argTypeEle.getAttribute(ARG_TYPE_MATCH_ATTRIBUTE);
830+
match = (StringUtils.hasText(match) ? match : DomUtils.getTextValue(argTypeEle));
831+
if (StringUtils.hasText(match)) {
832+
replaceOverride.addTypeIdentifier(match);
833+
}
830834
}
831835
replaceOverride.setSource(extractSource(replacedMethodEle));
832836
overrides.addOverride(replaceOverride);

org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTestTypes.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
3-
*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
@@ -540,6 +540,9 @@ public String replaceMe(int someParam) {
540540
return "replaceMe:" + someParam;
541541
}
542542

543+
public String replaceMe(String someParam) {
544+
return "replaceMe:" + someParam;
545+
}
543546
}
544547

545548

org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-delegationOverrides.xml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
35

4-
<beans>
5-
6-
<!--
6+
<!--
77
Not yet in use: illustration of possible approach
88
-->
99
<bean id="overrideOneMethod" class="org.springframework.beans.factory.xml.OverrideOneMethod">
@@ -80,4 +80,16 @@
8080
<property name="age"><value>27</value></property>
8181
</bean>
8282

83+
<bean id="overrideOneMethodByAttribute" class="org.springframework.beans.factory.xml.OverrideOneMethod">
84+
<replaced-method name="replaceMe" replacer="reverseReplacer">
85+
<arg-type match="String"/>
86+
</replaced-method>
87+
</bean>
88+
89+
<bean id="overrideOneMethodByElement" class="org.springframework.beans.factory.xml.OverrideOneMethod">
90+
<replaced-method name="replaceMe" replacer="reverseReplacer">
91+
<arg-type>String</arg-type>
92+
</replaced-method>
93+
</bean>
94+
8395
</beans>

org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1489,6 +1489,24 @@ public void BUGtestSerializableMethodReplacerAndSuperclass() throws IOException,
14891489
}
14901490
}
14911491

1492+
public @Test void testOverrideMethodByArgTypeAttribute() {
1493+
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
1494+
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
1495+
reader.loadBeanDefinitions(DELEGATION_OVERRIDES_CONTEXT);
1496+
OverrideOneMethod oom = (OverrideOneMethod) xbf.getBean("overrideOneMethodByAttribute");
1497+
assertEquals("should not replace", "replaceMe:1", oom.replaceMe(1));
1498+
assertEquals("should replace", "cba", oom.replaceMe("abc"));
1499+
}
1500+
1501+
public @Test void testOverrideMethodByArgTypeElement() {
1502+
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
1503+
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
1504+
reader.loadBeanDefinitions(DELEGATION_OVERRIDES_CONTEXT);
1505+
OverrideOneMethod oom = (OverrideOneMethod) xbf.getBean("overrideOneMethodByElement");
1506+
assertEquals("should not replace", "replaceMe:1", oom.replaceMe(1));
1507+
assertEquals("should replace", "cba", oom.replaceMe("abc"));
1508+
}
1509+
14921510
public static class DoSomethingReplacer implements MethodReplacer {
14931511

14941512
public Object lastArg;

0 commit comments

Comments
 (0)