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

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

Introduction

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

Prototype

public void setConstructorArgumentValues(ConstructorArgumentValues constructorArgumentValues) 

Source Link

Document

Specify constructor argument values for this bean.

Usage

From source file:com.seovic.validation.config.ValidationBeanDefinitionParser.java

private static BeanDefinition parseErrorMessageAction(Element message, ParserContext parserContext) {
    String messageId = message.getAttribute("id");
    String[] providers = message.getAttribute("providers").split(",");
    String typeName = ErrorMessageAction.class.getName();

    ConstructorArgumentValues ctorArgs = new ConstructorArgumentValues();
    ctorArgs.addGenericArgumentValue(messageId);
    ctorArgs.addGenericArgumentValue(providers);

    String when = message.getAttribute("when");
    MutablePropertyValues properties = new MutablePropertyValues();
    if (StringUtils.hasText(when)) {
        properties.addPropertyValue("when", when);
    }//from w w  w. j ava 2s .com
    AbstractBeanDefinition action;
    try {
        action = BeanDefinitionReaderUtils.createBeanDefinition(null, typeName,
                parserContext.getReaderContext().getBeanClassLoader());
    } catch (ClassNotFoundException e) {
        throw new BeanCreationException("Error occured during creation of bean definition", e);
    }
    action.setConstructorArgumentValues(ctorArgs);
    action.setPropertyValues(properties);
    return action;
}

From source file:com.griddynamics.banshun.xml.ExportBeanDefinitionParser.java

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    BeanDefinitionRegistry registry = parserContext.getRegistry();

    String exportInterface = element.getAttribute(INTERFACE_ATTR);
    if (isBlank(exportInterface)) {
        return;/*from w w  w.  j ava2s  .  co  m*/
    }

    String exportBeanRef = element.getAttribute(REF_ATTR);
    if (isBlank(exportBeanRef)) {
        return;
    }

    String rootName = element.getAttribute(ROOT_ATTR);
    if (isBlank(rootName)) {
        rootName = DEFAULT_ROOT_FACTORY_NAME;
    }

    String exportName = element.getAttribute(NAME_ATTR);
    if (isBlank(exportName)) {
        exportName = exportBeanRef;
    }

    String exportRefName = exportName + BEAN_NAME_SUFFIX;
    if (registry.containsBeanDefinition(exportRefName)) {
        throw new BeanCreationException("Registry already contains bean with name: " + exportRefName);
    }

    ConstructorArgumentValues exportBeanConstructorArgValues = new ConstructorArgumentValues();
    exportBeanConstructorArgValues.addGenericArgumentValue(exportName);
    exportBeanConstructorArgValues.addGenericArgumentValue(findClass(exportInterface, exportBeanRef,
            parserContext.getReaderContext().getResource().getDescription()));

    AbstractBeanDefinition exportBeanDef = rootBeanDefinition(ExportRef.class).getRawBeanDefinition();
    exportBeanDef.setConstructorArgumentValues(exportBeanConstructorArgValues);

    ConstructorArgumentValues voidBeanConstructorArgValues = new ConstructorArgumentValues();
    voidBeanConstructorArgValues.addGenericArgumentValue(exportBeanDef, ExportRef.class.getName());

    AbstractBeanDefinition voidBeanDef = rootBeanDefinition(Void.class).getRawBeanDefinition();
    voidBeanDef.setFactoryBeanName(rootName);
    voidBeanDef.setFactoryMethodName("export");
    voidBeanDef.setLazyInit(false);
    voidBeanDef.setScope(SCOPE_SINGLETON);
    voidBeanDef.setConstructorArgumentValues(voidBeanConstructorArgValues);
    //        voidBeanDefinition.setDependsOn(new String[] { exportBeanRef }); TODO ?

    registry.registerBeanDefinition(exportRefName, voidBeanDef);
}

From source file:com.griddynamics.banshun.xml.ImportBeanDefinitionParser.java

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {

    String exportInterface = element.getAttribute(INTERFACE_ATTR);
    if (isBlank(exportInterface)) {
        return;//from  w  w  w.  j  av  a2s.  c  om
    }

    String externalName = element.getAttribute(ID_ATTR);
    if (isBlank(externalName)) {
        return;
    }

    String rootName = element.getAttribute(ROOT_ATTR);
    if (isBlank(rootName)) {
        rootName = DEFAULT_ROOT_FACTORY_NAME;
    }

    ConstructorArgumentValues constructorArgValues = new ConstructorArgumentValues();
    constructorArgValues.addGenericArgumentValue(externalName);
    constructorArgValues.addGenericArgumentValue(findClass(exportInterface, element.getAttribute(ID_ATTR),
            parserContext.getReaderContext().getResource().getDescription()));

    AbstractBeanDefinition beanDef = builder.getRawBeanDefinition();
    beanDef.setFactoryBeanName(rootName);
    beanDef.setFactoryMethodName("lookup");
    beanDef.setConstructorArgumentValues(constructorArgValues);
    beanDef.setLazyInit(true);
    beanDef.setScope(SCOPE_SINGLETON);
}

From source file:com.griddynamics.banshun.config.xml.ImportBeanDefinitionParser.java

@Override
protected void doParse(Element el, ParserContext parserContext, BeanDefinitionBuilder builder) {

    Resource resource = parserContext.getReaderContext().getResource();

    String rootName = defaultIfBlank(el.getAttribute(ROOT_ATTR), DEFAULT_ROOT_FACTORY_NAME);
    String serviceIfaceName = el.getAttribute(INTERFACE_ATTR);
    String serviceName = el.getAttribute(ID_ATTR);

    Class<?> serviceIface = ParserUtils.findClassByName(serviceIfaceName, el.getAttribute(ID_ATTR),
            parserContext);/*from www.j  a va 2 s . c o  m*/

    AbstractBeanDefinition beanDef = builder.getRawBeanDefinition();
    beanDef.setFactoryBeanName(rootName);
    beanDef.setFactoryMethodName(Registry.LOOKUP_METHOD_NAME);
    beanDef.setConstructorArgumentValues(defineLookupMethodArgs(serviceName, serviceIface));
    beanDef.setLazyInit(true);
    beanDef.setScope(SCOPE_SINGLETON);
    beanDef.setResource(resource);

    beanDef.setAttribute(IMPORT_BEAN_DEF_ATTR_NAME,
            new BeanReferenceInfo(serviceName, serviceIface, extractResourcePath(resource)));
}

From source file:atunit.spring.SpringContainer.java

protected AbstractBeanDefinition defineInstanceHolderFactoryBean(Class<?> type, Object instance)
        throws Exception {
    ConstructorArgumentValues args = new ConstructorArgumentValues();
    args.addIndexedArgumentValue(0, type);
    args.addIndexedArgumentValue(1, instance);

    AbstractBeanDefinition beandef = BeanDefinitionReaderUtils.createBeanDefinition(null,
            InstanceHolderFactoryBean.class.getName(), getClass().getClassLoader());
    beandef.setConstructorArgumentValues(args);
    beandef.setAutowireCandidate(true);//ww  w . j  av  a 2 s  .c  o m
    return beandef;
}

From source file:com.griddynamics.banshun.config.xml.ExportBeanDefinitionParser.java

/**
 * Creates a factory method bean definition that just invokes
 * {@link Registry#export(ExportRef) export()} method on the specified registry bean.
 *
 * @param registryName/*from  w  w  w. jav a  2 s .  co  m*/
 * @param exportRef The definition of the {@link ExportRef} bean.
 * @param resource The resource that this bean definition came from.
 */
private BeanDefinition defineExportFactoryBean(String registryName, BeanDefinition exportRef,
        Resource resource) {

    AbstractBeanDefinition beanDef = rootBeanDefinition(Void.class)
            .setFactoryMethod(Registry.EXPORT_METHOD_NAME).setScope(SCOPE_SINGLETON).setLazyInit(false)
            //.addDependsOn(exportBeanRef) TODO ?
            .getRawBeanDefinition();
    beanDef.setFactoryBeanName(registryName);
    beanDef.setConstructorArgumentValues(defineExportMethodArgs(exportRef));
    beanDef.setResource(resource);

    return beanDef;
}

From source file:hudson.util.spring.DefaultBeanConfiguration.java

public void setProperty(String property, Object newValue) {
    if (PARENT.equals(property)) {
        setParent(newValue);//from   w w  w  . j  av a2  s .  c o m
    } else {
        AbstractBeanDefinition bd = getBeanDefinition();
        if (AUTOWIRE.equals(property)) {
            if (BY_NAME.equals(newValue)) {
                bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
            } else if (BY_TYPE.equals(newValue)) {
                bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
            } else if (Boolean.TRUE.equals(newValue)) {
                bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
            } else if (BY_CONSTRUCTOR.equals(newValue)) {
                bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
            }
        }
        // constructorArgs
        else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
            ConstructorArgumentValues cav = new ConstructorArgumentValues();
            List args = (List) newValue;
            for (Object e : args) {
                cav.addGenericArgumentValue(e);
            }
            bd.setConstructorArgumentValues(cav);
        }
        // destroyMethod
        else if (DESTROY_METHOD.equals(property)) {
            if (newValue != null)
                bd.setDestroyMethodName(newValue.toString());
        }
        // factoryBean
        else if (FACTORY_BEAN.equals(property)) {
            if (newValue != null)
                bd.setFactoryBeanName(newValue.toString());
        }
        // factoryMethod
        else if (FACTORY_METHOD.equals(property)) {
            if (newValue != null)
                bd.setFactoryMethodName(newValue.toString());
        }
        // initMethod
        else if (INIT_METHOD.equals(property)) {
            if (newValue != null)
                bd.setInitMethodName(newValue.toString());
        } else if (wrapper.isWritableProperty(property)) {

            wrapper.setPropertyValue(property, newValue);
        }
        // autowire
        else {
            super.setProperty(property, newValue);
        }
    }
}