Skip to content

Commit 999b5d4

Browse files
committed
Better error reporting when instance does not match factory method
This commit improves the handling of IllegalArgumentException in SimpleInstantiationStrategy. Previously, only arguments mismatch were handled but the exception can also be thrown if the factory instance does not match the target method. Closes gh-28897
1 parent 6299a9d commit 999b5d4

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*
3737
* @author Rod Johnson
3838
* @author Juergen Hoeller
39+
* @author Stephane Nicoll
3940
* @since 1.1
4041
*/
4142
public class SimpleInstantiationStrategy implements InstantiationStrategy {
@@ -152,6 +153,12 @@ public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, Bean
152153
}
153154
}
154155
catch (IllegalArgumentException ex) {
156+
if (factoryBean != null
157+
&& !factoryMethod.getDeclaringClass().isAssignableFrom(factoryBean.getClass())) {
158+
throw new BeanInstantiationException(factoryMethod,
159+
"Illegal factory instance for factory method '" + factoryMethod.getName() + "'; " +
160+
"instance: " + factoryBean.getClass().getName(), ex);
161+
}
155162
throw new BeanInstantiationException(factoryMethod,
156163
"Illegal arguments to factory method '" + factoryMethod.getName() + "'; " +
157164
"args: " + StringUtils.arrayToCommaDelimitedString(args), ex);

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ void instantiateWitArgs() {
5353
assertThat(simpleBean).isEqualTo("Test42");
5454
}
5555

56+
@Test
57+
void instantiateWitSubClassFactoryArgs() {
58+
RootBeanDefinition bd = new RootBeanDefinition(String.class);
59+
Object simpleBean = instantiate(bd, new ExtendedSampleFactory(),
60+
method(SampleFactory.class, "beanWithTwoArgs"), "Test", 42);
61+
assertThat(simpleBean).isEqualTo("42Test");
62+
}
63+
5664
@Test
5765
void instantiateWithNullValueReturnsNullBean() {
5866
RootBeanDefinition bd = new RootBeanDefinition(String.class);
@@ -71,6 +79,28 @@ bd, new SampleFactory(),
7179
.withMessageContaining("args: 42,Test");
7280
}
7381

82+
@Test
83+
void instantiateWithTargetTypeMismatch() {
84+
RootBeanDefinition bd = new RootBeanDefinition(String.class);
85+
assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() -> instantiate(
86+
bd, new AnotherFactory(),
87+
method(SampleFactory.class, "beanWithTwoArgs"), "Test", 42))
88+
.withMessageContaining("Illegal factory instance for factory method 'beanWithTwoArgs'")
89+
.withMessageContaining("instance: " + AnotherFactory.class.getName())
90+
.withMessageNotContaining("args: Test,42");
91+
}
92+
93+
@Test
94+
void instantiateWithTargetTypeNotAssignable() {
95+
RootBeanDefinition bd = new RootBeanDefinition(String.class);
96+
assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() -> instantiate(
97+
bd, new SampleFactory(),
98+
method(ExtendedSampleFactory.class, "beanWithTwoArgs"), "Test", 42))
99+
.withMessageContaining("Illegal factory instance for factory method 'beanWithTwoArgs'")
100+
.withMessageContaining("instance: " + SampleFactory.class.getName())
101+
.withMessageNotContaining("args: Test,42");
102+
}
103+
74104
@Test
75105
void instantiateWithException() {
76106
RootBeanDefinition bd = new RootBeanDefinition(String.class);
@@ -115,4 +145,20 @@ String errorBean(String msg) {
115145
}
116146

117147
}
148+
149+
static class ExtendedSampleFactory extends SampleFactory {
150+
151+
@Override
152+
String beanWithTwoArgs(String first, Integer second) {
153+
return second + first;
154+
}
155+
}
156+
157+
static class AnotherFactory {
158+
159+
String beanWithTwoArgs(String first, Integer second) {
160+
return second + first;
161+
}
162+
163+
}
118164
}

0 commit comments

Comments
 (0)