Search in sources :

Example 11 with ConstructorArgumentValues

use of org.springframework.beans.factory.config.ConstructorArgumentValues in project spring-framework by spring-projects.

the class QualifierAnnotationAutowireBeanFactoryTests method testAutowireCandidateExplicitlyFalseWithFieldDescriptor.

@Test
public void testAutowireCandidateExplicitlyFalseWithFieldDescriptor() throws Exception {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    ConstructorArgumentValues cavs = new ConstructorArgumentValues();
    cavs.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
    person.setAutowireCandidate(false);
    person.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
    lbf.registerBeanDefinition(JUERGEN, person);
    DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(QualifiedTestBean.class.getDeclaredField("qualified"), false);
    DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor(QualifiedTestBean.class.getDeclaredField("nonqualified"), false);
    assertFalse(lbf.isAutowireCandidate(JUERGEN, null));
    assertFalse(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor));
    assertFalse(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor));
}
Also used : DependencyDescriptor(org.springframework.beans.factory.config.DependencyDescriptor) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 12 with ConstructorArgumentValues

use of org.springframework.beans.factory.config.ConstructorArgumentValues in project spring-framework by spring-projects.

the class QualifierAnnotationAutowireBeanFactoryTests method testAutowireCandidateWithMultipleCandidatesDescriptor.

@Test
public void testAutowireCandidateWithMultipleCandidatesDescriptor() throws Exception {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
    cavs1.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
    person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
    lbf.registerBeanDefinition(JUERGEN, person1);
    ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
    cavs2.addGenericArgumentValue(MARK);
    RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
    person2.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
    lbf.registerBeanDefinition(MARK, person2);
    DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(new MethodParameter(QualifiedTestBean.class.getDeclaredConstructor(Person.class), 0), false);
    assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor));
    assertTrue(lbf.isAutowireCandidate(MARK, qualifiedDescriptor));
}
Also used : DependencyDescriptor(org.springframework.beans.factory.config.DependencyDescriptor) MethodParameter(org.springframework.core.MethodParameter) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 13 with ConstructorArgumentValues

use of org.springframework.beans.factory.config.ConstructorArgumentValues in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method beanDefinition.

private BeanDefinition beanDefinition(Object bean) {
    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(getClass());
    beanDefinition.setFactoryMethodName("getBean");
    ConstructorArgumentValues constructorArguments = new ConstructorArgumentValues();
    constructorArguments.addGenericArgumentValue(bean);
    beanDefinition.setConstructorArgumentValues(constructorArguments);
    return beanDefinition;
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 14 with ConstructorArgumentValues

use of org.springframework.beans.factory.config.ConstructorArgumentValues in project spring-framework by spring-projects.

the class GroovyBeanDefinitionWrapper method createBeanDefinition.

protected AbstractBeanDefinition createBeanDefinition() {
    AbstractBeanDefinition bd = new GenericBeanDefinition();
    bd.setBeanClass(this.clazz);
    if (!CollectionUtils.isEmpty(this.constructorArgs)) {
        ConstructorArgumentValues cav = new ConstructorArgumentValues();
        for (Object constructorArg : this.constructorArgs) {
            cav.addGenericArgumentValue(constructorArg);
        }
        bd.setConstructorArgumentValues(cav);
    }
    if (this.parentName != null) {
        bd.setParentName(this.parentName);
    }
    this.definitionWrapper = new BeanWrapperImpl(bd);
    return bd;
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 15 with ConstructorArgumentValues

use of org.springframework.beans.factory.config.ConstructorArgumentValues in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testCustomTypeConverter.

@Test
public void testCustomTypeConverter() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
    lbf.setTypeConverter(new CustomTypeConverter(nf));
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("myFloat", "1,1");
    ConstructorArgumentValues cav = new ConstructorArgumentValues();
    cav.addIndexedArgumentValue(0, "myName");
    cav.addIndexedArgumentValue(1, "myAge");
    lbf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class, cav, pvs));
    TestBean testBean = (TestBean) lbf.getBean("testBean");
    assertEquals("myName", testBean.getName());
    assertEquals(5, testBean.getAge());
    assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) NumberFormat(java.text.NumberFormat) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Aggregations

ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)90 Test (org.junit.Test)59 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)50 BeanCreationException (org.springframework.beans.factory.BeanCreationException)23 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)19 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)15 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)14 UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)9 DependencyDescriptor (org.springframework.beans.factory.config.DependencyDescriptor)8 Element (org.w3c.dom.Element)8 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)7 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)6 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)5 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)5 LinkedHashSet (java.util.LinkedHashSet)4 InjectionPoint (org.springframework.beans.factory.InjectionPoint)4 ValueHolder (org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder)4 ManagedList (org.springframework.beans.factory.support.ManagedList)4 MethodParameter (org.springframework.core.MethodParameter)4 Map (java.util.Map)3