lenientConstructorResolution flag applies to factory methods as well
This commit is contained in:
parent
e85ad67fd0
commit
1eabe2b441
|
|
@ -443,6 +443,12 @@ class ConstructorResolver {
|
|||
argsToUse = args.arguments;
|
||||
minTypeDiffWeight = typeDiffWeight;
|
||||
}
|
||||
else if (typeDiffWeight < Integer.MAX_VALUE && typeDiffWeight == minTypeDiffWeight &&
|
||||
!mbd.isLenientConstructorResolution()) {
|
||||
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
|
||||
"Ambiguous factory method matches found in bean '" + beanName + "' " +
|
||||
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -162,7 +162,7 @@
|
|||
<constructor-arg value="NaN" type="double"/>
|
||||
</bean>
|
||||
|
||||
<bean id="beanWithDoubleBoolean" class="org.springframework.beans.factory.xml.XmlBeanFactoryTests$DoubleBooleanConstructorBean" autowire="constructor" scope="prototype">
|
||||
<bean id="beanWithDoubleBoolean" class="org.springframework.beans.factory.xml.XmlBeanFactoryTests$DoubleBooleanConstructorBean" autowire="constructor" scope="prototype" factory-method="create">
|
||||
<constructor-arg type="java.lang.Boolean"><value>true</value></constructor-arg>
|
||||
</bean>
|
||||
|
||||
|
|
@ -175,6 +175,11 @@
|
|||
<constructor-arg index="1"><value>true</value></constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="beanWithDoubleBooleanNoTypeFactoryMethod" class="org.springframework.beans.factory.xml.XmlBeanFactoryTests$DoubleBooleanConstructorBean" scope="prototype" factory-method="create">
|
||||
<constructor-arg index="0"><value>false</value></constructor-arg>
|
||||
<constructor-arg index="1"><value>true</value></constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="string" class="java.lang.String" autowire-candidate="false">
|
||||
<constructor-arg><value type="java.lang.String">test</value></constructor-arg>
|
||||
</bean>
|
||||
|
|
|
|||
|
|
@ -1380,6 +1380,21 @@ public final class XmlBeanFactoryTests {
|
|||
}
|
||||
}
|
||||
|
||||
public @Test void testDoubleBooleanNoTypeFactoryMethod() {
|
||||
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
|
||||
AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("beanWithDoubleBooleanNoTypeFactoryMethod");
|
||||
bd.setLenientConstructorResolution(false);
|
||||
try {
|
||||
xbf.getBean("beanWithDoubleBooleanNoTypeFactoryMethod");
|
||||
fail("Should have thrown BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
// expected
|
||||
ex.printStackTrace();
|
||||
assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
|
||||
}
|
||||
}
|
||||
|
||||
public @Test void testStringConstructor() {
|
||||
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
|
||||
AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("string");
|
||||
|
|
@ -1632,6 +1647,14 @@ public final class XmlBeanFactoryTests {
|
|||
public DoubleBooleanConstructorBean(String s1, String s2) {
|
||||
throw new IllegalStateException("Don't pick this constructor");
|
||||
}
|
||||
|
||||
public static DoubleBooleanConstructorBean create(Boolean b1, Boolean b2) {
|
||||
return new DoubleBooleanConstructorBean(b1, b2);
|
||||
}
|
||||
|
||||
public static DoubleBooleanConstructorBean create(String s1, String s2) {
|
||||
return new DoubleBooleanConstructorBean(s1, s2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue