Pass pre-determined merged bean definition into InstanceSupplier (for inner beans)

Replaces useless protected obtainFromSupplier method with obtainInstanceFromSupplier.
Moves InstanceSupplier handling to appropriate subclass (DefaultListableBeanFactory).
BeanInstanceSupplier throws BeanInstantiationException instead of BeanCreationException.

Closes gh-29803
This commit is contained in:
Juergen Hoeller
2023-03-08 16:49:25 +01:00
parent 6cd67412cc
commit 95710646d1
6 changed files with 151 additions and 117 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,10 +27,10 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link AbstractAutowireCapableBeanFactory} instance supplier
* support.
* Tests for {@link AbstractAutowireCapableBeanFactory} instance supplier support.
*
* @author Phillip Webb
* @author Juergen Hoeller
*/
public class BeanFactorySupplierTests {
@@ -44,13 +44,38 @@ public class BeanFactorySupplierTests {
}
@Test
void getBeanWhenUsingInstanceSupplier() {
void getBeanWithInnerBeanUsingRegularSupplier() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
RootBeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.setInstanceSupplier(InstanceSupplier
.of(registeredBean -> "I am bean " + registeredBean.getBeanName()));
beanDefinition.setInstanceSupplier(() -> "I am supplied");
RootBeanDefinition outerBean = new RootBeanDefinition(String.class);
outerBean.getConstructorArgumentValues().addGenericArgumentValue(beanDefinition);
beanFactory.registerBeanDefinition("test", outerBean);
assertThat(beanFactory.getBean("test")).asString().startsWith("I am supplied");
}
@Test
void getBeanWhenUsingInstanceSupplier() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
RootBeanDefinition beanDefinition = new RootBeanDefinition(String.class);
beanDefinition.setInstanceSupplier(InstanceSupplier.of(registeredBean ->
"I am bean " + registeredBean.getBeanName() + " of " + registeredBean.getBeanClass()));
beanFactory.registerBeanDefinition("test", beanDefinition);
assertThat(beanFactory.getBean("test")).isEqualTo("I am bean test");
assertThat(beanFactory.getBean("test")).isEqualTo("I am bean test of class java.lang.String");
}
@Test
void getBeanWithInnerBeanUsingInstanceSupplier() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
RootBeanDefinition beanDefinition = new RootBeanDefinition(String.class);
beanDefinition.setInstanceSupplier(InstanceSupplier.of(registeredBean ->
"I am bean " + registeredBean.getBeanName() + " of " + registeredBean.getBeanClass()));
RootBeanDefinition outerBean = new RootBeanDefinition(String.class);
outerBean.getConstructorArgumentValues().addGenericArgumentValue(beanDefinition);
beanFactory.registerBeanDefinition("test", outerBean);
assertThat(beanFactory.getBean("test")).asString()
.startsWith("I am bean (inner bean)")
.endsWith(" of class java.lang.String");
}
@Test
@@ -62,6 +87,17 @@ public class BeanFactorySupplierTests {
assertThat(beanFactory.getBean("test")).isEqualTo("I am supplied");
}
@Test
void getBeanWithInnerBeanUsingThrowableSupplier() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
RootBeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.setInstanceSupplier(ThrowingSupplier.of(() -> "I am supplied"));
RootBeanDefinition outerBean = new RootBeanDefinition(String.class);
outerBean.getConstructorArgumentValues().addGenericArgumentValue(beanDefinition);
beanFactory.registerBeanDefinition("test", outerBean);
assertThat(beanFactory.getBean("test")).asString().startsWith("I am supplied");
}
@Test
void getBeanWhenUsingThrowableSupplierThatThrowsCheckedException() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();