Search in sources :

Example 76 with ConstructorArgumentValues

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

the class QualifierAnnotationAutowireContextTests method autowiredFieldResolvesWithMultipleQualifierValues.

@Test
public void autowiredFieldResolvesWithMultipleQualifierValues() {
    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);
    person2.addQualifier(qualifier2);
    context.registerBeanDefinition(JUERGEN, person1);
    context.registerBeanDefinition(MARK, person2);
    context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithMultipleAttributesTestBean.class));
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    context.refresh();
    QualifiedFieldWithMultipleAttributesTestBean bean = (QualifiedFieldWithMultipleAttributesTestBean) context.getBean("autowired");
    assertEquals(MARK, bean.getPerson().getName());
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 77 with ConstructorArgumentValues

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

the class QualifierAnnotationAutowireContextTests method autowiredFieldResolvesWithDefaultValueAndExplicitDefaultValueOnBeanDefinition.

@Test
public void autowiredFieldResolvesWithDefaultValueAndExplicitDefaultValueOnBeanDefinition() {
    GenericApplicationContext context = new GenericApplicationContext();
    ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
    cavs1.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
    // qualifier added, and value matches the default
    person1.addQualifier(new AutowireCandidateQualifier(TestQualifierWithDefaultValue.class, "default"));
    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 78 with ConstructorArgumentValues

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

the class ScriptBeanDefinitionParser method parseInternal.

/**
	 * Parses the dynamic object element and returns the resulting bean definition.
	 * Registers a {@link ScriptFactoryPostProcessor} if needed.
	 */
@Override
@SuppressWarnings("deprecation")
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    // Engine attribute only supported for <lang:std>
    String engine = element.getAttribute(ENGINE_ATTRIBUTE);
    // Resolve the script source.
    String value = resolveScriptSource(element, parserContext.getReaderContext());
    if (value == null) {
        return null;
    }
    // Set up infrastructure.
    LangNamespaceUtils.registerScriptFactoryPostProcessorIfNecessary(parserContext.getRegistry());
    // Create script factory bean definition.
    GenericBeanDefinition bd = new GenericBeanDefinition();
    bd.setBeanClassName(this.scriptFactoryClassName);
    bd.setSource(parserContext.extractSource(element));
    bd.setAttribute(ScriptFactoryPostProcessor.LANGUAGE_ATTRIBUTE, element.getLocalName());
    // Determine bean scope.
    String scope = element.getAttribute(SCOPE_ATTRIBUTE);
    if (StringUtils.hasLength(scope)) {
        bd.setScope(scope);
    }
    // Determine autowire mode.
    String autowire = element.getAttribute(AUTOWIRE_ATTRIBUTE);
    int autowireMode = parserContext.getDelegate().getAutowireMode(autowire);
    // Only "byType" and "byName" supported, but maybe other default inherited...
    if (autowireMode == GenericBeanDefinition.AUTOWIRE_AUTODETECT) {
        autowireMode = GenericBeanDefinition.AUTOWIRE_BY_TYPE;
    } else if (autowireMode == GenericBeanDefinition.AUTOWIRE_CONSTRUCTOR) {
        autowireMode = GenericBeanDefinition.AUTOWIRE_NO;
    }
    bd.setAutowireMode(autowireMode);
    // Parse depends-on list of bean names.
    String dependsOn = element.getAttribute(DEPENDS_ON_ATTRIBUTE);
    if (StringUtils.hasLength(dependsOn)) {
        bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS));
    }
    // Retrieve the defaults for bean definitions within this parser context
    BeanDefinitionDefaults beanDefinitionDefaults = parserContext.getDelegate().getBeanDefinitionDefaults();
    // Determine init method and destroy method.
    String initMethod = element.getAttribute(INIT_METHOD_ATTRIBUTE);
    if (StringUtils.hasLength(initMethod)) {
        bd.setInitMethodName(initMethod);
    } else if (beanDefinitionDefaults.getInitMethodName() != null) {
        bd.setInitMethodName(beanDefinitionDefaults.getInitMethodName());
    }
    if (element.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
        String destroyMethod = element.getAttribute(DESTROY_METHOD_ATTRIBUTE);
        bd.setDestroyMethodName(destroyMethod);
    } else if (beanDefinitionDefaults.getDestroyMethodName() != null) {
        bd.setDestroyMethodName(beanDefinitionDefaults.getDestroyMethodName());
    }
    // Attach any refresh metadata.
    String refreshCheckDelay = element.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE);
    if (StringUtils.hasText(refreshCheckDelay)) {
        bd.setAttribute(ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, Long.valueOf(refreshCheckDelay));
    }
    // Attach any proxy target class metadata.
    String proxyTargetClass = element.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE);
    if (StringUtils.hasText(proxyTargetClass)) {
        bd.setAttribute(ScriptFactoryPostProcessor.PROXY_TARGET_CLASS_ATTRIBUTE, Boolean.valueOf(proxyTargetClass));
    }
    // Add constructor arguments.
    ConstructorArgumentValues cav = bd.getConstructorArgumentValues();
    int constructorArgNum = 0;
    if (StringUtils.hasLength(engine)) {
        cav.addIndexedArgumentValue(constructorArgNum++, engine);
    }
    cav.addIndexedArgumentValue(constructorArgNum++, value);
    if (element.hasAttribute(SCRIPT_INTERFACES_ATTRIBUTE)) {
        cav.addIndexedArgumentValue(constructorArgNum++, element.getAttribute(SCRIPT_INTERFACES_ATTRIBUTE), "java.lang.Class[]");
    }
    // This is used for Groovy. It's a bean reference to a customizer bean.
    if (element.hasAttribute(CUSTOMIZER_REF_ATTRIBUTE)) {
        String customizerBeanName = element.getAttribute(CUSTOMIZER_REF_ATTRIBUTE);
        if (!StringUtils.hasText(customizerBeanName)) {
            parserContext.getReaderContext().error("Attribute 'customizer-ref' has empty value", element);
        } else {
            cav.addIndexedArgumentValue(constructorArgNum++, new RuntimeBeanReference(customizerBeanName));
        }
    }
    // Add any property definitions that need adding.
    parserContext.getDelegate().parsePropertyElements(element, bd);
    return bd;
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) BeanDefinitionDefaults(org.springframework.beans.factory.support.BeanDefinitionDefaults) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 79 with ConstructorArgumentValues

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

the class EntityScanPackages method register.

/**
	 * Register the specified entity scan packages with the system.
	 * @param registry the source registry
	 * @param packageNames the package names to register
	 */
public static void register(BeanDefinitionRegistry registry, Collection<String> packageNames) {
    Assert.notNull(registry, "Registry must not be null");
    Assert.notNull(packageNames, "PackageNames must not be null");
    if (registry.containsBeanDefinition(BEAN)) {
        BeanDefinition beanDefinition = registry.getBeanDefinition(BEAN);
        ConstructorArgumentValues constructorArguments = beanDefinition.getConstructorArgumentValues();
        constructorArguments.addIndexedArgumentValue(0, addPackageNames(constructorArguments, packageNames));
    } else {
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(EntityScanPackages.class);
        beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0, packageNames.toArray(new String[packageNames.size()]));
        beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        registry.registerBeanDefinition(BEAN, beanDefinition);
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 80 with ConstructorArgumentValues

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

the class QualifierAnnotationAutowireBeanFactoryTests method testAutowireCandidateWithFieldDescriptor.

@Ignore
@Test
public void testAutowireCandidateWithFieldDescriptor() 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);
    lbf.registerBeanDefinition(MARK, person2);
    DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(QualifiedTestBean.class.getDeclaredField("qualified"), false);
    DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor(QualifiedTestBean.class.getDeclaredField("nonqualified"), false);
    assertTrue(lbf.isAutowireCandidate(JUERGEN, null));
    assertTrue(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor));
    assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor));
    assertTrue(lbf.isAutowireCandidate(MARK, null));
    assertTrue(lbf.isAutowireCandidate(MARK, nonqualifiedDescriptor));
    assertFalse(lbf.isAutowireCandidate(MARK, qualifiedDescriptor));
}
Also used : DependencyDescriptor(org.springframework.beans.factory.config.DependencyDescriptor) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Ignore(org.junit.Ignore) 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