Browse Source

Restored isTypeMatch null behavior and refined typeToMatch parameter name

Issue: SPR-12147
pull/759/head
Juergen Hoeller 10 years ago
parent
commit
b2308926bc
  1. 8
      spring-beans/src/main/java/org/springframework/beans/factory/BeanFactory.java
  2. 4
      spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java
  3. 8
      spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java
  4. 38
      spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java
  5. 8
      spring-beans/src/test/java/org/springframework/beans/factory/FactoryBeanTests.java
  6. 8
      spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java
  7. 8
      spring-context/src/main/java/org/springframework/jndi/support/SimpleJndiBeanFactory.java
  8. 9
      spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java

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

@ -264,7 +264,7 @@ public interface BeanFactory {
* <p>Translates aliases back to the corresponding canonical bean name. * <p>Translates aliases back to the corresponding canonical bean name.
* Will ask the parent factory if the bean cannot be found in this factory instance. * Will ask the parent factory if the bean cannot be found in this factory instance.
* @param name the name of the bean to query * @param name the name of the bean to query
* @param targetType the type to match against (as a {@code ResolvableType}) * @param typeToMatch the type to match against (as a {@code ResolvableType})
* @return {@code true} if the bean type matches, * @return {@code true} if the bean type matches,
* {@code false} if it doesn't match or cannot be determined yet * {@code false} if it doesn't match or cannot be determined yet
* @throws NoSuchBeanDefinitionException if there is no bean with the given name * @throws NoSuchBeanDefinitionException if there is no bean with the given name
@ -272,7 +272,7 @@ public interface BeanFactory {
* @see #getBean * @see #getBean
* @see #getType * @see #getType
*/ */
boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException; boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;
/** /**
* Check whether the bean with the given name matches the specified type. * Check whether the bean with the given name matches the specified type.
@ -281,7 +281,7 @@ public interface BeanFactory {
* <p>Translates aliases back to the corresponding canonical bean name. * <p>Translates aliases back to the corresponding canonical bean name.
* Will ask the parent factory if the bean cannot be found in this factory instance. * Will ask the parent factory if the bean cannot be found in this factory instance.
* @param name the name of the bean to query * @param name the name of the bean to query
* @param targetType the type to match against (as a {@code Class}) * @param typeToMatch the type to match against (as a {@code Class})
* @return {@code true} if the bean type matches, * @return {@code true} if the bean type matches,
* {@code false} if it doesn't match or cannot be determined yet * {@code false} if it doesn't match or cannot be determined yet
* @throws NoSuchBeanDefinitionException if there is no bean with the given name * @throws NoSuchBeanDefinitionException if there is no bean with the given name
@ -289,7 +289,7 @@ public interface BeanFactory {
* @see #getBean * @see #getBean
* @see #getType * @see #getType
*/ */
boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException; boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;
/** /**
* Determine the type of the bean with the given name. More specifically, * Determine the type of the bean with the given name. More specifically,

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

@ -560,8 +560,8 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
} }
@Override @Override
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
return isTypeMatch(name, ResolvableType.forClass(targetType)); return isTypeMatch(name, ResolvableType.forClass(typeToMatch != null ? typeToMatch : Object.class));
} }
@Override @Override

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

@ -169,15 +169,15 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
} }
@Override @Override
public boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
Class<?> type = getType(name); Class<?> type = getType(name);
return (targetType == null || (type != null && targetType.isAssignableFrom(type))); return (type != null && typeToMatch.isAssignableFrom(type));
} }
@Override @Override
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
Class<?> type = getType(name); Class<?> type = getType(name);
return (targetType == null || (type != null && targetType.isAssignableFrom(type))); return (typeToMatch == null || (type != null && typeToMatch.isAssignableFrom(type)));
} }
@Override @Override

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

@ -1666,6 +1666,36 @@ public class DefaultListableBeanFactoryTests {
assertNull(lbf.getType("factoryBean")); assertNull(lbf.getType("factoryBean"));
} }
@Test
public void testGetBeanNamesForTypeBeforeFactoryBeanCreation() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class.getName()));
assertFalse(lbf.containsSingleton("factoryBean"));
String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false);
assertEquals(1, beanNames.length);
assertEquals("&factoryBean", beanNames[0]);
beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false);
assertEquals(1, beanNames.length);
assertEquals("&factoryBean", beanNames[0]);
}
@Test
public void testGetBeanNamesForTypeAfterFactoryBeanCreation() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class.getName()));
lbf.getBean("&factoryBean");
String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false);
assertEquals(1, beanNames.length);
assertEquals("&factoryBean", beanNames[0]);
beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false);
assertEquals(1, beanNames.length);
assertEquals("&factoryBean", beanNames[0]);
}
/** /**
* Verifies that a dependency on a {@link FactoryBean} can <strong>not</strong> * Verifies that a dependency on a {@link FactoryBean} can <strong>not</strong>
* be autowired <em>by name</em>, as &amp; is an illegal character in * be autowired <em>by name</em>, as &amp; is an illegal character in
@ -2862,7 +2892,7 @@ public class DefaultListableBeanFactoryTests {
} }
public static class FactoryBeanThatShouldntBeCalled implements FactoryBean<Object> { public static class FactoryBeanThatShouldntBeCalled implements FactoryBean<Object>, Runnable {
@Override @Override
public Object getObject() { public Object getObject() {
@ -2878,6 +2908,11 @@ public class DefaultListableBeanFactoryTests {
public boolean isSingleton() { public boolean isSingleton() {
return false; return false;
} }
@Override
public void run() {
throw new IllegalStateException();
}
} }
@ -2993,7 +3028,6 @@ public class DefaultListableBeanFactoryTests {
private FactoryBean<?> factoryBean; private FactoryBean<?> factoryBean;
public final FactoryBean<?> getFactoryBean() { public final FactoryBean<?> getFactoryBean() {
return this.factoryBean; return this.factoryBean;
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,7 +22,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@ -47,6 +46,7 @@ public final class FactoryBeanTests {
private static final Resource ABSTRACT_CONTEXT = qualifiedResource(CLASS, "abstract.xml"); private static final Resource ABSTRACT_CONTEXT = qualifiedResource(CLASS, "abstract.xml");
private static final Resource CIRCULAR_CONTEXT = qualifiedResource(CLASS, "circular.xml"); private static final Resource CIRCULAR_CONTEXT = qualifiedResource(CLASS, "circular.xml");
@Test @Test
public void testFactoryBeanReturnsNull() throws Exception { public void testFactoryBeanReturnsNull() throws Exception {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
@ -238,12 +238,12 @@ public final class FactoryBeanTests {
public void setInstanceName(String instanceName) { public void setInstanceName(String instanceName) {
this.instanceName = instanceName; this.instanceName = instanceName;
} }
@Override @Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory; this.beanFactory = beanFactory;
} }
@Override @Override
public T getObject() { public T getObject() {
if (instance == null) { if (instance == null) {

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

@ -1025,15 +1025,15 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
} }
@Override @Override
public boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
assertBeanFactoryActive(); assertBeanFactoryActive();
return getBeanFactory().isTypeMatch(name, targetType); return getBeanFactory().isTypeMatch(name, typeToMatch);
} }
@Override @Override
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
assertBeanFactoryActive(); assertBeanFactoryActive();
return getBeanFactory().isTypeMatch(name, targetType); return getBeanFactory().isTypeMatch(name, typeToMatch);
} }
@Override @Override

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

@ -174,15 +174,15 @@ public class SimpleJndiBeanFactory extends JndiLocatorSupport implements BeanFac
} }
@Override @Override
public boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
Class<?> type = getType(name); Class<?> type = getType(name);
return (targetType == null || (type != null && targetType.isAssignableFrom(type))); return (type != null && typeToMatch.isAssignableFrom(type));
} }
@Override @Override
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
Class<?> type = getType(name); Class<?> type = getType(name);
return (targetType == null || (type != null && targetType.isAssignableFrom(type))); return (typeToMatch == null || (type != null && typeToMatch.isAssignableFrom(type)));
} }
@Override @Override

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

@ -86,6 +86,7 @@ class StubWebApplicationContext implements WebApplicationContext {
this.resourcePatternResolver = new ServletContextResourcePatternResolver(servletContext); this.resourcePatternResolver = new ServletContextResourcePatternResolver(servletContext);
} }
/** /**
* Returns an instance that can initialize {@link ApplicationContextAware} beans. * Returns an instance that can initialize {@link ApplicationContextAware} beans.
*/ */
@ -194,13 +195,13 @@ class StubWebApplicationContext implements WebApplicationContext {
} }
@Override @Override
public boolean isTypeMatch(String name, ResolvableType targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
return this.beanFactory.isTypeMatch(name, targetType); return this.beanFactory.isTypeMatch(name, typeToMatch);
} }
@Override @Override
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException { public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
return this.beanFactory.isTypeMatch(name, targetType); return this.beanFactory.isTypeMatch(name, typeToMatch);
} }
@Override @Override

Loading…
Cancel
Save