Search in sources :

Example 41 with BeanDefinitionBuilder

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

the class IntegrationRegistrar method registerImplicitChannelCreator.

/**
 * This method will auto-register a ChannelInitializer which could also be overridden by the user
 * by simply registering a ChannelInitializer {@code <bean>} with its {@code autoCreate} property
 * set to false to suppress channel creation.
 * It will also register a ChannelInitializer$AutoCreateCandidatesCollector
 * which simply collects candidate channel names.
 * @param registry The {@link BeanDefinitionRegistry} to register additional {@link BeanDefinition}s.
 */
private void registerImplicitChannelCreator(BeanDefinitionRegistry registry) {
    if (!registry.containsBeanDefinition(IntegrationContextUtils.CHANNEL_INITIALIZER_BEAN_NAME)) {
        String channelsAutoCreateExpression = IntegrationProperties.getExpressionFor(IntegrationProperties.CHANNELS_AUTOCREATE);
        BeanDefinitionBuilder channelDef = BeanDefinitionBuilder.genericBeanDefinition(ChannelInitializer.class).addPropertyValue("autoCreate", channelsAutoCreateExpression);
        BeanDefinitionHolder channelCreatorHolder = new BeanDefinitionHolder(channelDef.getBeanDefinition(), IntegrationContextUtils.CHANNEL_INITIALIZER_BEAN_NAME);
        BeanDefinitionReaderUtils.registerBeanDefinition(channelCreatorHolder, registry);
    }
    if (!registry.containsBeanDefinition(IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME)) {
        BeanDefinitionBuilder channelRegistryBuilder = BeanDefinitionBuilder.genericBeanDefinition(ChannelInitializer.AutoCreateCandidatesCollector.class);
        channelRegistryBuilder.addConstructorArgValue(new ManagedSet<String>());
        // SPR-12761
        channelRegistryBuilder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        BeanDefinitionHolder channelRegistryHolder = new BeanDefinitionHolder(channelRegistryBuilder.getBeanDefinition(), IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME);
        BeanDefinitionReaderUtils.registerBeanDefinition(channelRegistryHolder, registry);
    }
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder)

Example 42 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 43 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 44 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 45 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)

Aggregations

BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)539 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)115 Element (org.w3c.dom.Element)99 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)60 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)59 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)29 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)11 Test (org.junit.Test)10 TypedStringValue (org.springframework.beans.factory.config.TypedStringValue)10 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)10