Skip to content

Commit b230892

Browse files
committed
Restored isTypeMatch null behavior and refined typeToMatch parameter name
Issue: SPR-12147
1 parent 44e8f7b commit b230892

File tree

8 files changed

+63
-28
lines changed

8 files changed

+63
-28
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/BeanFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,15 @@ public interface BeanFactory {
264264
* <p>Translates aliases back to the corresponding canonical bean name.
265265
* Will ask the parent factory if the bean cannot be found in this factory instance.
266266
* @param name the name of the bean to query
267-
* @param targetType the type to match against (as a {@code ResolvableType})
267+
* @param typeToMatch the type to match against (as a {@code ResolvableType})
268268
* @return {@code true} if the bean type matches,
269269
* {@code false} if it doesn't match or cannot be determined yet
270270
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
271271
* @since 4.2
272272
* @see #getBean
273273
* @see #getType
274274
*/
275-
boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException;
275+
boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;
276276

277277
/**
278278
* Check whether the bean with the given name matches the specified type.
@@ -281,15 +281,15 @@ public interface BeanFactory {
281281
* <p>Translates aliases back to the corresponding canonical bean name.
282282
* Will ask the parent factory if the bean cannot be found in this factory instance.
283283
* @param name the name of the bean to query
284-
* @param targetType the type to match against (as a {@code Class})
284+
* @param typeToMatch the type to match against (as a {@code Class})
285285
* @return {@code true} if the bean type matches,
286286
* {@code false} if it doesn't match or cannot be determined yet
287287
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
288288
* @since 2.0.1
289289
* @see #getBean
290290
* @see #getType
291291
*/
292-
boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;
292+
boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;
293293

294294
/**
295295
* Determine the type of the bean with the given name. More specifically,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,8 @@ else if (BeanFactoryUtils.isFactoryDereference(name)) {
560560
}
561561

562562
@Override
563-
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
564-
return isTypeMatch(name, ResolvableType.forClass(targetType));
563+
public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
564+
return isTypeMatch(name, ResolvableType.forClass(typeToMatch != null ? typeToMatch : Object.class));
565565
}
566566

567567
@Override

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,15 @@ public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
169169
}
170170

171171
@Override
172-
public boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException {
172+
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
173173
Class<?> type = getType(name);
174-
return (targetType == null || (type != null && targetType.isAssignableFrom(type)));
174+
return (type != null && typeToMatch.isAssignableFrom(type));
175175
}
176176

177177
@Override
178-
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
178+
public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
179179
Class<?> type = getType(name);
180-
return (targetType == null || (type != null && targetType.isAssignableFrom(type)));
180+
return (typeToMatch == null || (type != null && typeToMatch.isAssignableFrom(type)));
181181
}
182182

183183
@Override

spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,36 @@ public void testGetTypeForAbstractFactoryBean() {
16661666
assertNull(lbf.getType("factoryBean"));
16671667
}
16681668

1669+
@Test
1670+
public void testGetBeanNamesForTypeBeforeFactoryBeanCreation() {
1671+
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
1672+
lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class.getName()));
1673+
assertFalse(lbf.containsSingleton("factoryBean"));
1674+
1675+
String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false);
1676+
assertEquals(1, beanNames.length);
1677+
assertEquals("&factoryBean", beanNames[0]);
1678+
1679+
beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false);
1680+
assertEquals(1, beanNames.length);
1681+
assertEquals("&factoryBean", beanNames[0]);
1682+
}
1683+
1684+
@Test
1685+
public void testGetBeanNamesForTypeAfterFactoryBeanCreation() {
1686+
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
1687+
lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class.getName()));
1688+
lbf.getBean("&factoryBean");
1689+
1690+
String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false);
1691+
assertEquals(1, beanNames.length);
1692+
assertEquals("&factoryBean", beanNames[0]);
1693+
1694+
beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false);
1695+
assertEquals(1, beanNames.length);
1696+
assertEquals("&factoryBean", beanNames[0]);
1697+
}
1698+
16691699
/**
16701700
* Verifies that a dependency on a {@link FactoryBean} can <strong>not</strong>
16711701
* be autowired <em>by name</em>, as &amp; is an illegal character in
@@ -2862,7 +2892,7 @@ public Object createGeneric() {
28622892
}
28632893

28642894

2865-
public static class FactoryBeanThatShouldntBeCalled implements FactoryBean<Object> {
2895+
public static class FactoryBeanThatShouldntBeCalled implements FactoryBean<Object>, Runnable {
28662896

28672897
@Override
28682898
public Object getObject() {
@@ -2878,6 +2908,11 @@ public Class<?> getObjectType() {
28782908
public boolean isSingleton() {
28792909
return false;
28802910
}
2911+
2912+
@Override
2913+
public void run() {
2914+
throw new IllegalStateException();
2915+
}
28812916
}
28822917

28832918

@@ -2993,7 +3028,6 @@ private static class FactoryBeanDependentBean {
29933028

29943029
private FactoryBean<?> factoryBean;
29953030

2996-
29973031
public final FactoryBean<?> getFactoryBean() {
29983032
return this.factoryBean;
29993033
}

spring-beans/src/test/java/org/springframework/beans/factory/FactoryBeanTests.java

Lines changed: 4 additions & 4 deletions
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.
@@ -22,7 +22,6 @@
2222

2323
import org.junit.Test;
2424

25-
import org.springframework.beans.BeansException;
2625
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
2726
import org.springframework.beans.factory.config.BeanPostProcessor;
2827
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -47,6 +46,7 @@ public final class FactoryBeanTests {
4746
private static final Resource ABSTRACT_CONTEXT = qualifiedResource(CLASS, "abstract.xml");
4847
private static final Resource CIRCULAR_CONTEXT = qualifiedResource(CLASS, "circular.xml");
4948

49+
5050
@Test
5151
public void testFactoryBeanReturnsNull() throws Exception {
5252
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
@@ -238,12 +238,12 @@ public PassThroughFactoryBean(Class<T> type) {
238238
public void setInstanceName(String instanceName) {
239239
this.instanceName = instanceName;
240240
}
241+
241242
@Override
242-
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
243+
public void setBeanFactory(BeanFactory beanFactory) {
243244
this.beanFactory = beanFactory;
244245
}
245246

246-
247247
@Override
248248
public T getObject() {
249249
if (instance == null) {

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,15 +1025,15 @@ public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
10251025
}
10261026

10271027
@Override
1028-
public boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException {
1028+
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
10291029
assertBeanFactoryActive();
1030-
return getBeanFactory().isTypeMatch(name, targetType);
1030+
return getBeanFactory().isTypeMatch(name, typeToMatch);
10311031
}
10321032

10331033
@Override
1034-
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
1034+
public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
10351035
assertBeanFactoryActive();
1036-
return getBeanFactory().isTypeMatch(name, targetType);
1036+
return getBeanFactory().isTypeMatch(name, typeToMatch);
10371037
}
10381038

10391039
@Override

spring-context/src/main/java/org/springframework/jndi/support/SimpleJndiBeanFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,15 @@ public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
174174
}
175175

176176
@Override
177-
public boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException {
177+
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
178178
Class<?> type = getType(name);
179-
return (targetType == null || (type != null && targetType.isAssignableFrom(type)));
179+
return (type != null && typeToMatch.isAssignableFrom(type));
180180
}
181181

182182
@Override
183-
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
183+
public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
184184
Class<?> type = getType(name);
185-
return (targetType == null || (type != null && targetType.isAssignableFrom(type)));
185+
return (typeToMatch == null || (type != null && typeToMatch.isAssignableFrom(type)));
186186
}
187187

188188
@Override

spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public StubWebApplicationContext(ServletContext servletContext) {
8686
this.resourcePatternResolver = new ServletContextResourcePatternResolver(servletContext);
8787
}
8888

89+
8990
/**
9091
* Returns an instance that can initialize {@link ApplicationContextAware} beans.
9192
*/
@@ -194,13 +195,13 @@ public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
194195
}
195196

196197
@Override
197-
public boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException {
198-
return this.beanFactory.isTypeMatch(name, targetType);
198+
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
199+
return this.beanFactory.isTypeMatch(name, typeToMatch);
199200
}
200201

201202
@Override
202-
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
203-
return this.beanFactory.isTypeMatch(name, targetType);
203+
public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
204+
return this.beanFactory.isTypeMatch(name, typeToMatch);
204205
}
205206

206207
@Override

0 commit comments

Comments
 (0)