Example usage for org.springframework.beans.factory.support AbstractBeanDefinition setLenientConstructorResolution

List of usage examples for org.springframework.beans.factory.support AbstractBeanDefinition setLenientConstructorResolution

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support AbstractBeanDefinition setLenientConstructorResolution.

Prototype

public void setLenientConstructorResolution(boolean lenientConstructorResolution) 

Source Link

Document

Specify whether to resolve constructors in lenient mode ( true , which is the default) or to switch to strict resolution (throwing an exception in case of ambiguous constructors that all match when converting the arguments, whereas lenient mode would use the one with the 'closest' type matches).

Usage

From source file:org.springframework.beans.factory.xml.XmlBeanFactoryTests.java

@Test
public void testNonLenientDependencyMatching() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("lenientDependencyTestBean");
    bd.setLenientConstructorResolution(false);
    try {//from w  ww  . j a v a2s.co  m
        xbf.getBean("lenientDependencyTestBean");
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected
        ex.printStackTrace();
        assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
    }
}

From source file:org.springframework.beans.factory.xml.XmlBeanFactoryTests.java

@Test
public void testNonLenientDependencyMatchingFactoryMethod() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf
            .getBeanDefinition("lenientDependencyTestBeanFactoryMethod");
    bd.setLenientConstructorResolution(false);
    try {/*from  w  ww  .ja  v a  2  s .c o m*/
        xbf.getBean("lenientDependencyTestBeanFactoryMethod");
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected
        ex.printStackTrace();
        assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
    }
}

From source file:org.springframework.beans.factory.xml.XmlBeanFactoryTests.java

@Test
public void testJavaLangStringConstructor() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("string");
    bd.setLenientConstructorResolution(false);
    String str = (String) xbf.getBean("string");
    assertEquals("test", str);
}

From source file:org.springframework.beans.factory.xml.XmlBeanFactoryTests.java

@Test
public void testCustomStringConstructor() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("stringConstructor");
    bd.setLenientConstructorResolution(false);
    StringConstructorTestBean tb = (StringConstructorTestBean) xbf.getBean("stringConstructor");
    assertEquals("test", tb.name);
}

From source file:org.springframework.beans.factory.xml.XmlBeanFactoryTests.java

@Test
public void testStringConstructorArrayNoTypeNonLenient() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("constructorArrayNoType");
    bd.setLenientConstructorResolution(false);
    ConstructorArrayTestBean bean = (ConstructorArrayTestBean) xbf.getBean("constructorArrayNoType");
    assertTrue(bean.array instanceof String[]);
    assertEquals(0, ((String[]) bean.array).length);
}