|
|
|
@ -23,7 +23,6 @@ import java.net.MalformedURLException; |
|
|
|
import java.text.NumberFormat; |
|
|
|
import java.text.NumberFormat; |
|
|
|
import java.text.ParseException; |
|
|
|
import java.text.ParseException; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
@ -89,6 +88,7 @@ import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatNoException; |
|
|
|
import static org.mockito.ArgumentMatchers.any; |
|
|
|
import static org.mockito.ArgumentMatchers.any; |
|
|
|
import static org.mockito.ArgumentMatchers.eq; |
|
|
|
import static org.mockito.ArgumentMatchers.eq; |
|
|
|
import static org.mockito.ArgumentMatchers.isNull; |
|
|
|
import static org.mockito.ArgumentMatchers.isNull; |
|
|
|
@ -172,10 +172,8 @@ class DefaultListableBeanFactoryTests { |
|
|
|
registerBeanDefinitions(p); |
|
|
|
registerBeanDefinitions(p); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); |
|
|
|
assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); |
|
|
|
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); |
|
|
|
assertBeanNamesForType(TestBean.class, false, false); |
|
|
|
assertThat(beanNames).hasSize(0); |
|
|
|
assertThat(lbf.getBeanNamesForAnnotation(SuppressWarnings.class)).isEmpty(); |
|
|
|
beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class); |
|
|
|
|
|
|
|
assertThat(beanNames).hasSize(0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
@ -206,10 +204,8 @@ class DefaultListableBeanFactoryTests { |
|
|
|
registerBeanDefinitions(p); |
|
|
|
registerBeanDefinitions(p); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); |
|
|
|
assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); |
|
|
|
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); |
|
|
|
assertBeanNamesForType(TestBean.class, false, false); |
|
|
|
assertThat(beanNames).hasSize(0); |
|
|
|
assertThat(lbf.getBeanNamesForAnnotation(SuppressWarnings.class)).isEmpty(); |
|
|
|
beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class); |
|
|
|
|
|
|
|
assertThat(beanNames).hasSize(0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
@ -239,10 +235,8 @@ class DefaultListableBeanFactoryTests { |
|
|
|
registerBeanDefinitions(p); |
|
|
|
registerBeanDefinitions(p); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); |
|
|
|
assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); |
|
|
|
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); |
|
|
|
assertBeanNamesForType(TestBean.class, false, false); |
|
|
|
assertThat(beanNames).hasSize(0); |
|
|
|
assertThat(lbf.getBeanNamesForAnnotation(SuppressWarnings.class)).isEmpty(); |
|
|
|
beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class); |
|
|
|
|
|
|
|
assertThat(beanNames).hasSize(0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
@ -272,10 +266,8 @@ class DefaultListableBeanFactoryTests { |
|
|
|
registerBeanDefinitions(p); |
|
|
|
registerBeanDefinitions(p); |
|
|
|
lbf.preInstantiateSingletons(); |
|
|
|
lbf.preInstantiateSingletons(); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(!DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isTrue(); |
|
|
|
assertThat(DummyFactory.wasPrototypeCreated()).as("prototype not instantiated").isFalse(); |
|
|
|
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); |
|
|
|
assertBeanNamesForType(TestBean.class, true, false, "x1"); |
|
|
|
assertThat(beanNames).hasSize(1); |
|
|
|
|
|
|
|
assertThat(beanNames[0]).isEqualTo("x1"); |
|
|
|
|
|
|
|
assertThat(lbf.containsSingleton("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsSingleton("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("&x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("&x1")).isTrue(); |
|
|
|
@ -310,14 +302,10 @@ class DefaultListableBeanFactoryTests { |
|
|
|
assertThat(lbf.isTypeMatch("&x2", Object.class)).isTrue(); |
|
|
|
assertThat(lbf.isTypeMatch("&x2", Object.class)).isTrue(); |
|
|
|
assertThat(lbf.getType("x2")).isEqualTo(TestBean.class); |
|
|
|
assertThat(lbf.getType("x2")).isEqualTo(TestBean.class); |
|
|
|
assertThat(lbf.getType("&x2")).isEqualTo(DummyFactory.class); |
|
|
|
assertThat(lbf.getType("&x2")).isEqualTo(DummyFactory.class); |
|
|
|
assertThat(lbf.getAliases("x1").length).isEqualTo(1); |
|
|
|
assertThat(lbf.getAliases("x1")).containsExactly("x2"); |
|
|
|
assertThat(lbf.getAliases("x1")[0]).isEqualTo("x2"); |
|
|
|
assertThat(lbf.getAliases("&x1")).containsExactly("&x2"); |
|
|
|
assertThat(lbf.getAliases("&x1").length).isEqualTo(1); |
|
|
|
assertThat(lbf.getAliases("x2")).containsExactly("x1"); |
|
|
|
assertThat(lbf.getAliases("&x1")[0]).isEqualTo("&x2"); |
|
|
|
assertThat(lbf.getAliases("&x2")).containsExactly("&x1"); |
|
|
|
assertThat(lbf.getAliases("x2").length).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(lbf.getAliases("x2")[0]).isEqualTo("x1"); |
|
|
|
|
|
|
|
assertThat(lbf.getAliases("&x2").length).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(lbf.getAliases("&x2")[0]).isEqualTo("&x1"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -327,9 +315,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
lbf.registerBeanDefinition("x1", rbd); |
|
|
|
lbf.registerBeanDefinition("x1", rbd); |
|
|
|
|
|
|
|
|
|
|
|
TestBeanFactory.initialized = false; |
|
|
|
TestBeanFactory.initialized = false; |
|
|
|
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); |
|
|
|
assertBeanNamesForType(TestBean.class, true, false, "x1"); |
|
|
|
assertThat(beanNames).hasSize(1); |
|
|
|
|
|
|
|
assertThat(beanNames[0]).isEqualTo("x1"); |
|
|
|
|
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("&x1")).isFalse(); |
|
|
|
assertThat(lbf.containsBean("&x1")).isFalse(); |
|
|
|
@ -352,9 +338,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
lbf.registerBeanDefinition("x1", rbd); |
|
|
|
lbf.registerBeanDefinition("x1", rbd); |
|
|
|
|
|
|
|
|
|
|
|
TestBeanFactory.initialized = false; |
|
|
|
TestBeanFactory.initialized = false; |
|
|
|
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); |
|
|
|
assertBeanNamesForType(TestBean.class, true, false, "x1"); |
|
|
|
assertThat(beanNames).hasSize(1); |
|
|
|
|
|
|
|
assertThat(beanNames[0]).isEqualTo("x1"); |
|
|
|
|
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("&x1")).isFalse(); |
|
|
|
assertThat(lbf.containsBean("&x1")).isFalse(); |
|
|
|
@ -379,9 +363,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
lbf.registerBeanDefinition("x1", rbd); |
|
|
|
lbf.registerBeanDefinition("x1", rbd); |
|
|
|
|
|
|
|
|
|
|
|
TestBeanFactory.initialized = false; |
|
|
|
TestBeanFactory.initialized = false; |
|
|
|
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); |
|
|
|
assertBeanNamesForType(TestBean.class, true, false, "x1"); |
|
|
|
assertThat(beanNames).hasSize(1); |
|
|
|
|
|
|
|
assertThat(beanNames[0]).isEqualTo("x1"); |
|
|
|
|
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("&x1")).isFalse(); |
|
|
|
assertThat(lbf.containsBean("&x1")).isFalse(); |
|
|
|
@ -407,9 +389,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
lbf.registerBeanDefinition("x1", rbd); |
|
|
|
lbf.registerBeanDefinition("x1", rbd); |
|
|
|
|
|
|
|
|
|
|
|
TestBeanFactory.initialized = false; |
|
|
|
TestBeanFactory.initialized = false; |
|
|
|
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); |
|
|
|
assertBeanNamesForType(TestBean.class, true, false, "x1"); |
|
|
|
assertThat(beanNames).hasSize(1); |
|
|
|
|
|
|
|
assertThat(beanNames[0]).isEqualTo("x1"); |
|
|
|
|
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsSingleton("x1")).isFalse(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("x1")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("&x1")).isFalse(); |
|
|
|
assertThat(lbf.containsBean("&x1")).isFalse(); |
|
|
|
@ -442,14 +422,10 @@ class DefaultListableBeanFactoryTests { |
|
|
|
assertThat(lbf.isTypeMatch("&x2", Object.class)).isFalse(); |
|
|
|
assertThat(lbf.isTypeMatch("&x2", Object.class)).isFalse(); |
|
|
|
assertThat(lbf.getType("x2")).isEqualTo(TestBean.class); |
|
|
|
assertThat(lbf.getType("x2")).isEqualTo(TestBean.class); |
|
|
|
assertThat(lbf.getType("&x2")).isNull(); |
|
|
|
assertThat(lbf.getType("&x2")).isNull(); |
|
|
|
assertThat(lbf.getAliases("x1").length).isEqualTo(1); |
|
|
|
assertThat(lbf.getAliases("x1")).containsExactly("x2"); |
|
|
|
assertThat(lbf.getAliases("x1")[0]).isEqualTo("x2"); |
|
|
|
assertThat(lbf.getAliases("&x1")).containsExactly("&x2"); |
|
|
|
assertThat(lbf.getAliases("&x1").length).isEqualTo(1); |
|
|
|
assertThat(lbf.getAliases("x2")).containsExactly("x1"); |
|
|
|
assertThat(lbf.getAliases("&x1")[0]).isEqualTo("&x2"); |
|
|
|
assertThat(lbf.getAliases("&x2")).containsExactly("&x1"); |
|
|
|
assertThat(lbf.getAliases("x2").length).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(lbf.getAliases("x2")[0]).isEqualTo("x1"); |
|
|
|
|
|
|
|
assertThat(lbf.getAliases("&x2").length).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(lbf.getAliases("&x2")[0]).isEqualTo("&x1"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -613,8 +589,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
lbf.registerSingleton("string", "A"); |
|
|
|
lbf.registerSingleton("string", "A"); |
|
|
|
|
|
|
|
|
|
|
|
TestBean self = (TestBean) lbf.getBean("self"); |
|
|
|
TestBean self = (TestBean) lbf.getBean("self"); |
|
|
|
assertThat(self.getStringArray()).hasSize(1); |
|
|
|
assertThat(self.getStringArray()).containsExactly("A"); |
|
|
|
assertThat(self.getStringArray()).contains("A"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -627,8 +602,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
lbf.registerSingleton("string", "A"); |
|
|
|
lbf.registerSingleton("string", "A"); |
|
|
|
|
|
|
|
|
|
|
|
TestBean self = (TestBean) lbf.getBean("self"); |
|
|
|
TestBean self = (TestBean) lbf.getBean("self"); |
|
|
|
assertThat(self.getStringArray()).hasSize(1); |
|
|
|
assertThat(self.getStringArray()).containsExactly("A"); |
|
|
|
assertThat(self.getStringArray()).contains("A"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -660,8 +634,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
.withCauseInstanceOf(NotWritablePropertyException.class) |
|
|
|
.withCauseInstanceOf(NotWritablePropertyException.class) |
|
|
|
.satisfies(ex -> { |
|
|
|
.satisfies(ex -> { |
|
|
|
NotWritablePropertyException cause = (NotWritablePropertyException) ex.getCause(); |
|
|
|
NotWritablePropertyException cause = (NotWritablePropertyException) ex.getCause(); |
|
|
|
assertThat(cause.getPossibleMatches()).hasSize(1); |
|
|
|
assertThat(cause.getPossibleMatches()).containsExactly("age"); |
|
|
|
assertThat(cause.getPossibleMatches()[0]).isEqualTo("age"); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -679,7 +652,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
lbf = new DefaultListableBeanFactory(); |
|
|
|
lbf = new DefaultListableBeanFactory(); |
|
|
|
p = new Properties(); |
|
|
|
p = new Properties(); |
|
|
|
p.setProperty("kerry.(class)", TestBean.class.getName()); |
|
|
|
p.setProperty("kerry.(class)", TestBean.class.getName()); |
|
|
|
p.setProperty("kerry.(scope)", "prototype"); |
|
|
|
p.setProperty("kerry.(scope)", BeanDefinition.SCOPE_PROTOTYPE); |
|
|
|
p.setProperty("kerry.age", "35"); |
|
|
|
p.setProperty("kerry.age", "35"); |
|
|
|
registerBeanDefinitions(p); |
|
|
|
registerBeanDefinitions(p); |
|
|
|
kerry1 = (TestBean) lbf.getBean("kerry"); |
|
|
|
kerry1 = (TestBean) lbf.getBean("kerry"); |
|
|
|
@ -1150,7 +1123,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
assertThat(lbf.containsBean("singletonObject")).isTrue(); |
|
|
|
assertThat(lbf.containsBean("singletonObject")).isTrue(); |
|
|
|
assertThat(lbf.isSingleton("singletonObject")).isTrue(); |
|
|
|
assertThat(lbf.isSingleton("singletonObject")).isTrue(); |
|
|
|
assertThat(lbf.getType("singletonObject")).isEqualTo(TestBean.class); |
|
|
|
assertThat(lbf.getType("singletonObject")).isEqualTo(TestBean.class); |
|
|
|
assertThat(lbf.getAliases("singletonObject").length).isEqualTo(0); |
|
|
|
assertThat(lbf.getAliases("singletonObject")).isEmpty(); |
|
|
|
DependenciesBean test = (DependenciesBean) lbf.getBean("test"); |
|
|
|
DependenciesBean test = (DependenciesBean) lbf.getBean("test"); |
|
|
|
assertThat(lbf.getBean("singletonObject")).isEqualTo(singletonObject); |
|
|
|
assertThat(lbf.getBean("singletonObject")).isEqualTo(singletonObject); |
|
|
|
assertThat(test.getSpouse()).isEqualTo(singletonObject); |
|
|
|
assertThat(test.getSpouse()).isEqualTo(singletonObject); |
|
|
|
@ -1796,12 +1769,12 @@ class DefaultListableBeanFactoryTests { |
|
|
|
assertThat(bean.beanName).isEqualTo("bd1"); |
|
|
|
assertThat(bean.beanName).isEqualTo("bd1"); |
|
|
|
assertThat(bean.spouseAge).isEqualTo(42); |
|
|
|
assertThat(bean.spouseAge).isEqualTo(42); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(lbf.getBeanNamesForType(ConstructorDependency.class).length).isEqualTo(1); |
|
|
|
assertThat(lbf.getBeanNamesForType(ConstructorDependency.class)).hasSize(1); |
|
|
|
assertThat(lbf.getBeanNamesForType(ConstructorDependencyFactoryBean.class).length).isEqualTo(1); |
|
|
|
assertThat(lbf.getBeanNamesForType(ConstructorDependencyFactoryBean.class)).hasSize(1); |
|
|
|
assertThat(lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)).length).isEqualTo(1); |
|
|
|
assertThat(lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class))).hasSize(1); |
|
|
|
assertThat(lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)).length).isEqualTo(0); |
|
|
|
assertThat(lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class))).isEmpty(); |
|
|
|
assertThat(lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class), true, true).length).isEqualTo(1); |
|
|
|
assertThat(lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class), true, true)).hasSize(1); |
|
|
|
assertThat(lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class), true, true).length).isEqualTo(0); |
|
|
|
assertThat(lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class), true, true)).isEmpty(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private RootBeanDefinition createConstructorDependencyBeanDefinition(int age) { |
|
|
|
private RootBeanDefinition createConstructorDependencyBeanDefinition(int age) { |
|
|
|
@ -1866,46 +1839,45 @@ class DefaultListableBeanFactoryTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void getBeanNamesForTypeBeforeFactoryBeanCreation() { |
|
|
|
void getBeanNamesForTypeBeforeFactoryBeanCreation() { |
|
|
|
|
|
|
|
FactoryBeanThatShouldntBeCalled.instantiated = false; |
|
|
|
lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class)); |
|
|
|
lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class)); |
|
|
|
assertThat(lbf.containsSingleton("factoryBean")).isFalse(); |
|
|
|
assertThat(lbf.containsSingleton("factoryBean")).isFalse(); |
|
|
|
|
|
|
|
assertThat(FactoryBeanThatShouldntBeCalled.instantiated).isFalse(); |
|
|
|
|
|
|
|
|
|
|
|
String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false); |
|
|
|
assertBeanNamesForType(Runnable.class, false, false, "&factoryBean"); |
|
|
|
assertThat(beanNames.length).isEqualTo(1); |
|
|
|
assertBeanNamesForType(Callable.class, false, false, "&factoryBean"); |
|
|
|
assertThat(beanNames[0]).isEqualTo("&factoryBean"); |
|
|
|
assertBeanNamesForType(RepositoryFactoryInformation.class, false, false, "&factoryBean"); |
|
|
|
|
|
|
|
assertBeanNamesForType(FactoryBean.class, false, false, "&factoryBean"); |
|
|
|
beanNames = lbf.getBeanNamesForType(Callable.class, false, false); |
|
|
|
|
|
|
|
assertThat(beanNames.length).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(beanNames[0]).isEqualTo("&factoryBean"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
beanNames = lbf.getBeanNamesForType(RepositoryFactoryInformation.class, false, false); |
|
|
|
|
|
|
|
assertThat(beanNames.length).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(beanNames[0]).isEqualTo("&factoryBean"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false); |
|
|
|
|
|
|
|
assertThat(beanNames.length).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(beanNames[0]).isEqualTo("&factoryBean"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void getBeanNamesForTypeAfterFactoryBeanCreation() { |
|
|
|
void getBeanNamesForTypeAfterFactoryBeanCreation() { |
|
|
|
|
|
|
|
FactoryBeanThatShouldntBeCalled.instantiated = false; |
|
|
|
lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class)); |
|
|
|
lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class)); |
|
|
|
lbf.getBean("&factoryBean"); |
|
|
|
lbf.getBean("&factoryBean"); |
|
|
|
|
|
|
|
assertThat(FactoryBeanThatShouldntBeCalled.instantiated).isTrue(); |
|
|
|
|
|
|
|
assertThat(lbf.containsSingleton("factoryBean")).isTrue(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertBeanNamesForType(Runnable.class, false, false, "&factoryBean"); |
|
|
|
|
|
|
|
assertBeanNamesForType(Callable.class, false, false, "&factoryBean"); |
|
|
|
|
|
|
|
assertBeanNamesForType(RepositoryFactoryInformation.class, false, false, "&factoryBean"); |
|
|
|
|
|
|
|
assertBeanNamesForType(FactoryBean.class, false, false, "&factoryBean"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test // gh-28616
|
|
|
|
|
|
|
|
void getBeanNamesForTypeWithPrototypeScopedFactoryBean() { |
|
|
|
|
|
|
|
FactoryBeanThatShouldntBeCalled.instantiated = false; |
|
|
|
|
|
|
|
RootBeanDefinition beanDefinition = new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class); |
|
|
|
|
|
|
|
beanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE); |
|
|
|
|
|
|
|
lbf.registerBeanDefinition("factoryBean", beanDefinition); |
|
|
|
|
|
|
|
assertThat(FactoryBeanThatShouldntBeCalled.instantiated).isFalse(); |
|
|
|
|
|
|
|
assertThat(lbf.containsSingleton("factoryBean")).isFalse(); |
|
|
|
|
|
|
|
|
|
|
|
String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false); |
|
|
|
// We should not find any beans of the following types if the FactoryBean itself is prototype-scoped.
|
|
|
|
assertThat(beanNames.length).isEqualTo(1); |
|
|
|
assertBeanNamesForType(Runnable.class, false, false); |
|
|
|
assertThat(beanNames[0]).isEqualTo("&factoryBean"); |
|
|
|
assertBeanNamesForType(Callable.class, false, false); |
|
|
|
|
|
|
|
assertBeanNamesForType(RepositoryFactoryInformation.class, false, false); |
|
|
|
beanNames = lbf.getBeanNamesForType(Callable.class, false, false); |
|
|
|
assertBeanNamesForType(FactoryBean.class, false, false); |
|
|
|
assertThat(beanNames.length).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(beanNames[0]).isEqualTo("&factoryBean"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
beanNames = lbf.getBeanNamesForType(RepositoryFactoryInformation.class, false, false); |
|
|
|
|
|
|
|
assertThat(beanNames.length).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(beanNames[0]).isEqualTo("&factoryBean"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false); |
|
|
|
|
|
|
|
assertThat(beanNames.length).isEqualTo(1); |
|
|
|
|
|
|
|
assertThat(beanNames[0]).isEqualTo("&factoryBean"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -2170,8 +2142,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyBean.class); |
|
|
|
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyBean.class); |
|
|
|
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); |
|
|
|
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); |
|
|
|
lbf.registerBeanDefinition("test", bd); |
|
|
|
lbf.registerBeanDefinition("test", bd); |
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy( |
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(lbf::preInstantiateSingletons); |
|
|
|
lbf::preInstantiateSingletons); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -2179,8 +2150,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class); |
|
|
|
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class); |
|
|
|
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); |
|
|
|
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); |
|
|
|
lbf.registerBeanDefinition("test", bd); |
|
|
|
lbf.registerBeanDefinition("test", bd); |
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy( |
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(lbf::preInstantiateSingletons); |
|
|
|
lbf::preInstantiateSingletons); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -2188,8 +2158,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class); |
|
|
|
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class); |
|
|
|
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); |
|
|
|
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); |
|
|
|
lbf.registerBeanDefinition("test", bd); |
|
|
|
lbf.registerBeanDefinition("test", bd); |
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> lbf.getBeansOfType(String.class)); |
|
|
|
lbf.getBeansOfType(String.class)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -2216,8 +2185,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyWithClassResolution.class); |
|
|
|
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyWithClassResolution.class); |
|
|
|
bd.getConstructorArgumentValues().addGenericArgumentValue("java.lang.Strin"); |
|
|
|
bd.getConstructorArgumentValues().addGenericArgumentValue("java.lang.Strin"); |
|
|
|
lbf.registerBeanDefinition("test", bd); |
|
|
|
lbf.registerBeanDefinition("test", bd); |
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy( |
|
|
|
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(lbf::preInstantiateSingletons); |
|
|
|
lbf::preInstantiateSingletons); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -2241,7 +2209,13 @@ class DefaultListableBeanFactoryTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void prototypeFactoryBeanNotEagerlyCalled() { |
|
|
|
void prototypeFactoryBeanNotEagerlyCalled() { |
|
|
|
lbf.registerBeanDefinition("test", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class)); |
|
|
|
lbf.registerBeanDefinition("test", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class)); |
|
|
|
lbf.preInstantiateSingletons(); |
|
|
|
assertThatNoException().isThrownBy(lbf::preInstantiateSingletons); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
void prototypeFactoryBeanNotEagerlyCalledInCaseOfBeanClassName() { |
|
|
|
|
|
|
|
lbf.registerBeanDefinition("test", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class.getName(), null, null)); |
|
|
|
|
|
|
|
assertThatNoException().isThrownBy(lbf::preInstantiateSingletons); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -2281,13 +2255,6 @@ class DefaultListableBeanFactoryTests { |
|
|
|
assertThat(factory.initialized).isTrue(); |
|
|
|
assertThat(factory.initialized).isTrue(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
void prototypeFactoryBeanNotEagerlyCalledInCaseOfBeanClassName() { |
|
|
|
|
|
|
|
lbf.registerBeanDefinition("test", |
|
|
|
|
|
|
|
new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class.getName(), null, null)); |
|
|
|
|
|
|
|
lbf.preInstantiateSingletons(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void prototypeStringCreatedRepeatedly() { |
|
|
|
void prototypeStringCreatedRepeatedly() { |
|
|
|
RootBeanDefinition stringDef = new RootBeanDefinition(String.class); |
|
|
|
RootBeanDefinition stringDef = new RootBeanDefinition(String.class); |
|
|
|
@ -2480,10 +2447,7 @@ class DefaultListableBeanFactoryTests { |
|
|
|
lbf.registerBeanDefinition("fmWithArgs", factoryMethodDefinitionWithArgs); |
|
|
|
lbf.registerBeanDefinition("fmWithArgs", factoryMethodDefinitionWithArgs); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(lbf.getBeanDefinitionCount()).isEqualTo(4); |
|
|
|
assertThat(lbf.getBeanDefinitionCount()).isEqualTo(4); |
|
|
|
List<String> tbNames = Arrays.asList(lbf.getBeanNamesForType(TestBean.class)); |
|
|
|
assertBeanNamesForType(TestBean.class, true, true, "fmWithProperties", "fmWithArgs"); |
|
|
|
assertThat(tbNames.contains("fmWithProperties")).isTrue(); |
|
|
|
|
|
|
|
assertThat(tbNames.contains("fmWithArgs")).isTrue(); |
|
|
|
|
|
|
|
assertThat(tbNames.size()).isEqualTo(2); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TestBean tb = (TestBean) lbf.getBean("fmWithProperties"); |
|
|
|
TestBean tb = (TestBean) lbf.getBean("fmWithProperties"); |
|
|
|
TestBean second = (TestBean) lbf.getBean("fmWithProperties"); |
|
|
|
TestBean second = (TestBean) lbf.getBean("fmWithProperties"); |
|
|
|
@ -2671,6 +2635,19 @@ class DefaultListableBeanFactoryTests { |
|
|
|
return (new org.springframework.beans.factory.support.PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, prefix); |
|
|
|
return (new org.springframework.beans.factory.support.PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, prefix); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void assertBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit, String... names) { |
|
|
|
|
|
|
|
if (names.length == 0) { |
|
|
|
|
|
|
|
assertThat(lbf.getBeanNamesForType(type, includeNonSingletons, allowEagerInit)) |
|
|
|
|
|
|
|
.as("bean names for type " + type.getName()) |
|
|
|
|
|
|
|
.isEmpty(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
assertThat(lbf.getBeanNamesForType(type, includeNonSingletons, allowEagerInit)) |
|
|
|
|
|
|
|
.as("bean names for type " + type.getName()) |
|
|
|
|
|
|
|
.containsExactly(names); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class NoDependencies { |
|
|
|
public static class NoDependencies { |
|
|
|
|
|
|
|
|
|
|
|
@ -2894,6 +2871,12 @@ class DefaultListableBeanFactoryTests { |
|
|
|
public static class FactoryBeanThatShouldntBeCalled<T extends Repository<S, ID>, S, ID extends Serializable> |
|
|
|
public static class FactoryBeanThatShouldntBeCalled<T extends Repository<S, ID>, S, ID extends Serializable> |
|
|
|
extends RepositoryFactoryBeanSupport<T, S, ID> implements Runnable, Callable<T> { |
|
|
|
extends RepositoryFactoryBeanSupport<T, S, ID> implements Runnable, Callable<T> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static boolean instantiated = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
instantiated = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public T getObject() { |
|
|
|
public T getObject() { |
|
|
|
throw new IllegalStateException(); |
|
|
|
throw new IllegalStateException(); |
|
|
|
|