Skip to content

Commit 74e6213

Browse files
committed
Lookup method definitions can be overridden in child beans
Issue: SPR-13388
1 parent b198cad commit 74e6213

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverrides.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -18,6 +18,7 @@
1818

1919
import java.lang.reflect.Method;
2020
import java.util.HashSet;
21+
import java.util.LinkedHashSet;
2122
import java.util.Set;
2223

2324
/**
@@ -34,7 +35,7 @@
3435
*/
3536
public class MethodOverrides {
3637

37-
private final Set<MethodOverride> overrides = new HashSet<MethodOverride>(0);
38+
private final Set<MethodOverride> overrides = new LinkedHashSet<MethodOverride>(0);
3839

3940

4041
/**
@@ -89,14 +90,16 @@ public boolean isEmpty() {
8990
* @return the method override, or {@code null} if none
9091
*/
9192
public MethodOverride getOverride(Method method) {
92-
for (MethodOverride override : this.overrides) {
93-
if (override.matches(method)) {
94-
return override;
93+
MethodOverride match = null;
94+
for (MethodOverride candidate : this.overrides) {
95+
if (candidate.matches(method)) {
96+
match = candidate;
9597
}
9698
}
97-
return null;
99+
return match;
98100
}
99101

102+
100103
@Override
101104
public boolean equals(Object other) {
102105
if (this == other) {

spring-beans/src/test/java/org/springframework/beans/factory/support/LookupMethodTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -90,6 +90,16 @@ public void testWithThreeArgsShouldFail() {
9090
}
9191
}
9292

93+
@Test
94+
public void testWithOverriddenLookupMethod() {
95+
AbstractBean bean = (AbstractBean) beanFactory.getBean("extendedBean");
96+
assertNotNull(bean);
97+
TestBean expected = bean.getOneArgument("haha");
98+
assertEquals(TestBean.class, expected.getClass());
99+
assertEquals("haha", expected.getName());
100+
assertTrue(expected.isJedi());
101+
}
102+
93103

94104
public static abstract class AbstractBean {
95105

spring-beans/src/test/resources/org/springframework/beans/factory/support/lookupMethodTests.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
<lookup-method name="getTwoArguments" bean="testBean"/>
1010
</bean>
1111

12+
<bean id="extendedBean" parent="abstractBean">
13+
<lookup-method name="getOneArgument" bean="jedi"/>
14+
</bean>
15+
1216
<bean id="testBean" class="org.springframework.tests.sample.beans.TestBean" scope="prototype"/>
1317

18+
<bean id="jedi" class="org.springframework.tests.sample.beans.TestBean" scope="prototype" autowire-candidate="false">
19+
<property name="jedi" value="true"/>
20+
</bean>
21+
1422
</beans>

0 commit comments

Comments
 (0)