From 1eabe2b4416ee7619bd863fcdc1e6f6ada766400 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 27 Jul 2009 14:09:42 +0000 Subject: [PATCH] lenientConstructorResolution flag applies to factory methods as well --- .../factory/support/ConstructorResolver.java | 6 +++++ .../XmlBeanFactoryTests-constructorArg.xml | 7 +++++- .../factory/xml/XmlBeanFactoryTests.java | 23 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java index a9dc14bf19..a33ed6589e 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java @@ -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)"); + } } } diff --git a/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-constructorArg.xml b/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-constructorArg.xml index 39e6dfaa8e..1c05a0e6b0 100644 --- a/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-constructorArg.xml +++ b/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-constructorArg.xml @@ -162,7 +162,7 @@ - + true @@ -175,6 +175,11 @@ true + + false + true + + test diff --git a/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java b/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java index 7151096ff6..5a33d8c8e2 100644 --- a/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java +++ b/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java @@ -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); + } } -- GitLab