Search in sources :

Example 1 with BeanDefinitionDefaults

use of cn.taketoday.beans.factory.support.BeanDefinitionDefaults in project today-framework by TAKETODAY.

the class ScriptBeanDefinitionParser method parseInternal.

/**
 * Parses the dynamic object element and returns the resulting bean definition.
 * Registers a {@link ScriptFactoryPostProcessor} if needed.
 */
@Override
@Nullable
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.isNotEmpty(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 == AbstractBeanDefinition.AUTOWIRE_AUTODETECT) {
        autowireMode = AbstractBeanDefinition.AUTOWIRE_BY_TYPE;
    } else if (autowireMode == AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR) {
        autowireMode = AbstractBeanDefinition.AUTOWIRE_NO;
    }
    bd.setAutowireMode(autowireMode);
    // Parse depends-on list of bean names.
    String dependsOn = element.getAttribute(DEPENDS_ON_ATTRIBUTE);
    if (StringUtils.isNotEmpty(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.isNotEmpty(initMethod)) {
        bd.setInitMethodNames(initMethod);
    } else if (beanDefinitionDefaults.getInitMethodName() != null) {
        bd.setInitMethodNames(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.isNotEmpty(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(cn.taketoday.beans.factory.support.GenericBeanDefinition) BeanDefinitionDefaults(cn.taketoday.beans.factory.support.BeanDefinitionDefaults) RuntimeBeanReference(cn.taketoday.beans.factory.config.RuntimeBeanReference) ConstructorArgumentValues(cn.taketoday.beans.factory.config.ConstructorArgumentValues) Nullable(cn.taketoday.lang.Nullable)

Example 2 with BeanDefinitionDefaults

use of cn.taketoday.beans.factory.support.BeanDefinitionDefaults in project today-infrastructure by TAKETODAY.

the class ScriptBeanDefinitionParser method parseInternal.

/**
 * Parses the dynamic object element and returns the resulting bean definition.
 * Registers a {@link ScriptFactoryPostProcessor} if needed.
 */
@Override
@Nullable
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.isNotEmpty(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 == AbstractBeanDefinition.AUTOWIRE_AUTODETECT) {
        autowireMode = AbstractBeanDefinition.AUTOWIRE_BY_TYPE;
    } else if (autowireMode == AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR) {
        autowireMode = AbstractBeanDefinition.AUTOWIRE_NO;
    }
    bd.setAutowireMode(autowireMode);
    // Parse depends-on list of bean names.
    String dependsOn = element.getAttribute(DEPENDS_ON_ATTRIBUTE);
    if (StringUtils.isNotEmpty(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.isNotEmpty(initMethod)) {
        bd.setInitMethodNames(initMethod);
    } else if (beanDefinitionDefaults.getInitMethodName() != null) {
        bd.setInitMethodNames(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.isNotEmpty(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(cn.taketoday.beans.factory.support.GenericBeanDefinition) BeanDefinitionDefaults(cn.taketoday.beans.factory.support.BeanDefinitionDefaults) RuntimeBeanReference(cn.taketoday.beans.factory.config.RuntimeBeanReference) ConstructorArgumentValues(cn.taketoday.beans.factory.config.ConstructorArgumentValues) Nullable(cn.taketoday.lang.Nullable)

Aggregations

ConstructorArgumentValues (cn.taketoday.beans.factory.config.ConstructorArgumentValues)2 RuntimeBeanReference (cn.taketoday.beans.factory.config.RuntimeBeanReference)2 BeanDefinitionDefaults (cn.taketoday.beans.factory.support.BeanDefinitionDefaults)2 GenericBeanDefinition (cn.taketoday.beans.factory.support.GenericBeanDefinition)2 Nullable (cn.taketoday.lang.Nullable)2