Search in sources :

Example 66 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project spring-integration by spring-projects.

the class GlobalChannelInterceptorParser method parseInternal.

@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder globalChannelInterceptorBuilder = BeanDefinitionBuilder.genericBeanDefinition(GlobalChannelInterceptorWrapper.class);
    Object childBeanDefinition = getBeanDefinitionBuilderConstructorValue(element, parserContext);
    globalChannelInterceptorBuilder.addConstructorArgValue(childBeanDefinition);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(globalChannelInterceptorBuilder, element, "order");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(globalChannelInterceptorBuilder, element, CHANNEL_NAME_PATTERN_ATTRIBUTE, "patterns");
    return globalChannelInterceptorBuilder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder)

Example 67 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project spring-integration by spring-projects.

the class HeaderEnricherParserSupport method addHeader.

private void addHeader(Element element, ManagedMap<String, Object> headers, ParserContext parserContext, String headerName, Element headerElement, Class<?> headerType, String expression, String overwrite) {
    String value = headerElement.getAttribute("value");
    String ref = headerElement.getAttribute(REF_ATTRIBUTE);
    String method = headerElement.getAttribute(METHOD_ATTRIBUTE);
    if (expression == null) {
        expression = headerElement.getAttribute(EXPRESSION_ATTRIBUTE);
    }
    Element beanElement = null;
    Element scriptElement = null;
    Element expressionElement = null;
    List<Element> subElements = DomUtils.getChildElements(headerElement);
    if (!subElements.isEmpty()) {
        Element subElement = subElements.get(0);
        String subElementLocalName = subElement.getLocalName();
        if ("bean".equals(subElementLocalName)) {
            beanElement = subElement;
        } else if ("script".equals(subElementLocalName)) {
            scriptElement = subElement;
        } else if ("expression".equals(subElementLocalName)) {
            expressionElement = subElement;
        }
        if (beanElement == null && scriptElement == null && expressionElement == null) {
            parserContext.getReaderContext().error("Only 'bean', 'script' or 'expression' can be defined as a sub-element", element);
        }
    }
    if (StringUtils.hasText(expression) && expressionElement != null) {
        parserContext.getReaderContext().error("The 'expression' attribute and sub-element are mutually exclusive", element);
    }
    boolean isValue = StringUtils.hasText(value);
    boolean isRef = StringUtils.hasText(ref);
    boolean hasMethod = StringUtils.hasText(method);
    boolean isExpression = StringUtils.hasText(expression) || expressionElement != null;
    boolean isScript = scriptElement != null;
    BeanDefinition innerComponentDefinition = null;
    if (beanElement != null) {
        innerComponentDefinition = parserContext.getDelegate().parseBeanDefinitionElement(beanElement).getBeanDefinition();
    } else if (isScript) {
        innerComponentDefinition = parserContext.getDelegate().parseCustomElement(scriptElement);
    }
    boolean isCustomBean = innerComponentDefinition != null;
    if (hasMethod && isScript) {
        parserContext.getReaderContext().error("The 'method' attribute cannot be used when a 'script' sub-element is defined", element);
    }
    if (isValue == (isRef ^ (isExpression ^ isCustomBean))) {
        parserContext.getReaderContext().error("Exactly one of the 'ref', 'value', 'expression' or inner bean is required.", element);
    }
    BeanDefinitionBuilder valueProcessorBuilder = null;
    if (isValue) {
        if (hasMethod) {
            parserContext.getReaderContext().error("The 'method' attribute cannot be used with the 'value' attribute.", element);
        }
        if (IntegrationMessageHeaderAccessor.ROUTING_SLIP.equals(headerName)) {
            List<String> routingSlipPath = new ManagedList<String>();
            routingSlipPath.addAll(Arrays.asList(StringUtils.tokenizeToStringArray(value, ";")));
            valueProcessorBuilder = BeanDefinitionBuilder.genericBeanDefinition(RoutingSlipHeaderValueMessageProcessor.class).addConstructorArgValue(routingSlipPath);
        } else {
            Object headerValue = (headerType != null) ? new TypedStringValue(value, headerType) : value;
            valueProcessorBuilder = BeanDefinitionBuilder.genericBeanDefinition(StaticHeaderValueMessageProcessor.class).addConstructorArgValue(headerValue);
        }
    } else if (isExpression) {
        if (hasMethod) {
            parserContext.getReaderContext().error("The 'method' attribute cannot be used with the 'expression' attribute.", element);
        }
        valueProcessorBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionEvaluatingHeaderValueMessageProcessor.class);
        if (expressionElement != null) {
            BeanDefinitionBuilder dynamicExpressionBuilder = BeanDefinitionBuilder.genericBeanDefinition(DynamicExpression.class);
            dynamicExpressionBuilder.addConstructorArgValue(expressionElement.getAttribute("key"));
            dynamicExpressionBuilder.addConstructorArgReference(expressionElement.getAttribute("source"));
            valueProcessorBuilder.addConstructorArgValue(dynamicExpressionBuilder.getBeanDefinition());
        } else {
            valueProcessorBuilder.addConstructorArgValue(expression);
        }
        valueProcessorBuilder.addConstructorArgValue(headerType);
    } else if (isCustomBean) {
        if (StringUtils.hasText(headerElement.getAttribute("type"))) {
            parserContext.getReaderContext().error("The 'type' attribute cannot be used with an inner bean.", element);
        }
        if (hasMethod || isScript) {
            valueProcessorBuilder = BeanDefinitionBuilder.genericBeanDefinition(MessageProcessingHeaderValueMessageProcessor.class).addConstructorArgValue(innerComponentDefinition);
            if (hasMethod) {
                valueProcessorBuilder.addConstructorArgValue(method);
            }
        } else {
            valueProcessorBuilder = BeanDefinitionBuilder.genericBeanDefinition(StaticHeaderValueMessageProcessor.class);
            valueProcessorBuilder.addConstructorArgValue(innerComponentDefinition);
        }
    } else {
        if (StringUtils.hasText(headerElement.getAttribute("type"))) {
            parserContext.getReaderContext().error("The 'type' attribute cannot be used with the 'ref' attribute.", element);
        }
        if (hasMethod) {
            valueProcessorBuilder = BeanDefinitionBuilder.genericBeanDefinition(MessageProcessingHeaderValueMessageProcessor.class).addConstructorArgReference(ref).addConstructorArgValue(method);
        } else {
            valueProcessorBuilder = BeanDefinitionBuilder.genericBeanDefinition(StaticHeaderValueMessageProcessor.class).addConstructorArgReference(ref);
        }
    }
    if (StringUtils.hasText(overwrite)) {
        valueProcessorBuilder.addPropertyValue("overwrite", overwrite);
    }
    headers.put(headerName, valueProcessorBuilder.getBeanDefinition());
}
Also used : StaticHeaderValueMessageProcessor(org.springframework.integration.transformer.support.StaticHeaderValueMessageProcessor) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) DynamicExpression(org.springframework.integration.expression.DynamicExpression) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) TypedStringValue(org.springframework.beans.factory.config.TypedStringValue) MessageProcessingHeaderValueMessageProcessor(org.springframework.integration.transformer.support.MessageProcessingHeaderValueMessageProcessor)

Example 68 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project spring-integration by spring-projects.

the class IntegrationManagementParser method parseInternal.

@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(IntegrationManagementConfigurer.class);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "default-logging-enabled");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "default-counts-enabled");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "default-stats-enabled");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "counts-enabled-patterns", "enabledCountsPatterns");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "stats-enabled-patterns", "enabledStatsPatterns");
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "metrics-factory");
    return builder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder)

Example 69 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project spring-integration by spring-projects.

the class IntegrationNamespaceUtils method createAdapter.

private static BeanMetadataElement createAdapter(BeanMetadataElement ref, String method, String unqualifiedClassName) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(IntegrationConfigUtils.BASE_PACKAGE + ".config." + unqualifiedClassName + "FactoryBean");
    builder.addPropertyValue("target", ref);
    if (StringUtils.hasText(method)) {
        builder.addPropertyValue("methodName", method);
    }
    return builder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder)

Example 70 with BeanDefinitionBuilder

use of org.springframework.beans.factory.support.BeanDefinitionBuilder in project spring-integration by spring-projects.

the class IntegrationNamespaceUtils method configureTransactionDefinition.

/**
 * Parse attributes of "transactional" element and configure a {@link DefaultTransactionAttribute}
 * with provided "transactionDefinition" properties.
 *
 * @param txElement The transactional element.
 * @return The bean definition.
 */
public static BeanDefinition configureTransactionDefinition(Element txElement) {
    BeanDefinitionBuilder txDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(DefaultTransactionAttribute.class);
    txDefinitionBuilder.addPropertyValue("propagationBehaviorName", "PROPAGATION_" + txElement.getAttribute("propagation"));
    txDefinitionBuilder.addPropertyValue("isolationLevelName", "ISOLATION_" + txElement.getAttribute("isolation"));
    txDefinitionBuilder.addPropertyValue("timeout", txElement.getAttribute("timeout"));
    txDefinitionBuilder.addPropertyValue("readOnly", txElement.getAttribute("read-only"));
    return txDefinitionBuilder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder)

Aggregations

BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)524 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)116 Element (org.w3c.dom.Element)96 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)61 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)58 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)53 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)47 ManagedList (org.springframework.beans.factory.support.ManagedList)33 ManagedMap (org.springframework.beans.factory.support.ManagedMap)26 HashMap (java.util.HashMap)19 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)19 Test (org.junit.jupiter.api.Test)16 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)16 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)12 Node (org.w3c.dom.Node)12 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 TypedStringValue (org.springframework.beans.factory.config.TypedStringValue)10 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)10