Search in sources :

Example 6 with ConstructorArgumentValues

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

the class QualifierAnnotationAutowireContextTests method autowiredMethodParameterWithStaticallyQualifiedCandidate.

@Test
public void autowiredMethodParameterWithStaticallyQualifiedCandidate() {
    GenericApplicationContext context = new GenericApplicationContext();
    ConstructorArgumentValues cavs = new ConstructorArgumentValues();
    cavs.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person = new RootBeanDefinition(QualifiedPerson.class, cavs, null);
    context.registerBeanDefinition(JUERGEN, ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(person, JUERGEN), context, true).getBeanDefinition());
    context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    context.refresh();
    QualifiedMethodParameterTestBean bean = (QualifiedMethodParameterTestBean) context.getBean("autowired");
    assertEquals(JUERGEN, bean.getPerson().getName());
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 7 with ConstructorArgumentValues

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

the class QualifierAnnotationAutowireContextTests method autowiredFieldDoesNotResolveWithMultipleQualifierValuesAndConflictingDefaultValue.

@Test
public void autowiredFieldDoesNotResolveWithMultipleQualifierValuesAndConflictingDefaultValue() {
    GenericApplicationContext context = new GenericApplicationContext();
    ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
    cavs1.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
    AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class);
    qualifier.setAttribute("number", 456);
    person1.addQualifier(qualifier);
    ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
    cavs2.addGenericArgumentValue(MARK);
    RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
    AutowireCandidateQualifier qualifier2 = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class);
    qualifier2.setAttribute("number", 123);
    qualifier2.setAttribute("value", "not the default");
    person2.addQualifier(qualifier2);
    context.registerBeanDefinition(JUERGEN, person1);
    context.registerBeanDefinition(MARK, person2);
    context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithMultipleAttributesTestBean.class));
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    try {
        context.refresh();
        fail("expected BeanCreationException");
    } catch (BeanCreationException e) {
        assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException);
        assertEquals("autowired", e.getBeanName());
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 8 with ConstructorArgumentValues

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

the class QualifierAnnotationAutowireContextTests method autowiredFieldResolvesQualifiedCandidateWithDefaultValueAndNoValueOnBeanDefinition.

@Test
public void autowiredFieldResolvesQualifiedCandidateWithDefaultValueAndNoValueOnBeanDefinition() {
    GenericApplicationContext context = new GenericApplicationContext();
    ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
    cavs1.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
    // qualifier added, but includes no value
    person1.addQualifier(new AutowireCandidateQualifier(TestQualifierWithDefaultValue.class));
    ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
    cavs2.addGenericArgumentValue(MARK);
    RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
    context.registerBeanDefinition(JUERGEN, person1);
    context.registerBeanDefinition(MARK, person2);
    context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithDefaultValueTestBean.class));
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    context.refresh();
    QualifiedFieldWithDefaultValueTestBean bean = (QualifiedFieldWithDefaultValueTestBean) context.getBean("autowired");
    assertEquals(JUERGEN, bean.getPerson().getName());
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 9 with ConstructorArgumentValues

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

the class QualifierAnnotationAutowireContextTests method autowiredConstructorArgumentWithSingleNonQualifiedCandidate.

@Test
public void autowiredConstructorArgumentWithSingleNonQualifiedCandidate() {
    GenericApplicationContext context = new GenericApplicationContext();
    ConstructorArgumentValues cavs = new ConstructorArgumentValues();
    cavs.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
    context.registerBeanDefinition(JUERGEN, person);
    context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    try {
        context.refresh();
        fail("expected BeanCreationException");
    } catch (BeanCreationException e) {
        assertTrue(e instanceof UnsatisfiedDependencyException);
        assertEquals("autowired", e.getBeanName());
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 10 with ConstructorArgumentValues

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

the class QualifierAnnotationAutowireContextTests method autowiredFieldDoesNotResolveWithMultipleQualifierValuesAndMultipleMatchingCandidates.

@Test
public void autowiredFieldDoesNotResolveWithMultipleQualifierValuesAndMultipleMatchingCandidates() {
    GenericApplicationContext context = new GenericApplicationContext();
    ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
    cavs1.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
    AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class);
    qualifier.setAttribute("number", 123);
    person1.addQualifier(qualifier);
    ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
    cavs2.addGenericArgumentValue(MARK);
    RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
    AutowireCandidateQualifier qualifier2 = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class);
    qualifier2.setAttribute("number", 123);
    qualifier2.setAttribute("value", "default");
    person2.addQualifier(qualifier2);
    context.registerBeanDefinition(JUERGEN, person1);
    context.registerBeanDefinition(MARK, person2);
    context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithMultipleAttributesTestBean.class));
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    try {
        context.refresh();
        fail("expected BeanCreationException");
    } catch (BeanCreationException e) {
        assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException);
        assertEquals("autowired", e.getBeanName());
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Aggregations

ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)96 Test (org.junit.Test)63 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)50 BeanCreationException (org.springframework.beans.factory.BeanCreationException)23 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)20 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)15 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)14 UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)9 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)9 ValueHolder (org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder)8 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 LinkedHashSet (java.util.LinkedHashSet)4 InjectionPoint (org.springframework.beans.factory.InjectionPoint)4 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)4 ManagedList (org.springframework.beans.factory.support.ManagedList)4 MethodParameter (org.springframework.core.MethodParameter)4