Search in sources :

Example 76 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-integration by spring-projects.

the class MessageHistoryRegistrar method registerBeanDefinitions.

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(EnableMessageHistory.class.getName());
    Object componentNamePatterns = annotationAttributes.get("value");
    if (componentNamePatterns instanceof String[]) {
        StringBuilder componentNamePatternsString = new StringBuilder();
        for (String s : (String[]) componentNamePatterns) {
            componentNamePatternsString.append(s).append(",");
        }
        componentNamePatterns = componentNamePatternsString.substring(0, componentNamePatternsString.length() - 1);
    }
    if (!registry.containsBeanDefinition(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME)) {
        Set<Object> componentNamePatternsSet = new ManagedSet<Object>();
        componentNamePatternsSet.add(componentNamePatterns);
        AbstractBeanDefinition messageHistoryConfigurer = BeanDefinitionBuilder.genericBeanDefinition(MessageHistoryConfigurer.class).addPropertyValue("componentNamePatternsSet", componentNamePatternsSet).getBeanDefinition();
        registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME, messageHistoryConfigurer);
    } else {
        BeanDefinition beanDefinition = registry.getBeanDefinition(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME);
        PropertyValue propertyValue = beanDefinition.getPropertyValues().getPropertyValue("componentNamePatternsSet");
        if (propertyValue != null) {
            @SuppressWarnings("unchecked") Set<Object> currentComponentNamePatternsSet = (Set<Object>) propertyValue.getValue();
            currentComponentNamePatternsSet.add(componentNamePatterns);
        } else {
            Set<Object> componentNamePatternsSet = new ManagedSet<Object>();
            componentNamePatternsSet.add(componentNamePatterns);
            beanDefinition.getPropertyValues().addPropertyValue("componentNamePatternsSet", componentNamePatternsSet);
        }
    }
}
Also used : ManagedSet(org.springframework.beans.factory.support.ManagedSet) Set(java.util.Set) ManagedSet(org.springframework.beans.factory.support.ManagedSet) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) PropertyValue(org.springframework.beans.PropertyValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition)

Example 77 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition 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 78 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-integration by spring-projects.

the class IntegrationNamespaceUtils method configurePollerMetadata.

/**
 * Parse a "poller" element to provide a reference for the target BeanDefinitionBuilder. If the poller element does
 * not contain a "ref" attribute, this will create and register a PollerMetadata instance and then add it as a
 * property reference of the target builder.
 *
 * @param pollerElement the "poller" element to parse
 * @param targetBuilder the builder that expects the "trigger" property
 * @param parserContext the parserContext for the target builder
 */
public static void configurePollerMetadata(Element pollerElement, BeanDefinitionBuilder targetBuilder, ParserContext parserContext) {
    if (pollerElement.hasAttribute("ref")) {
        int numberOfAttributes = pollerElement.getAttributes().getLength();
        if (numberOfAttributes != 1) {
            /*
				 * When importing the core namespace, e.g. into jdbc, we get a 'default="false"' attribute,
				 * even if not explicitly declared.
				 */
            if (!(numberOfAttributes == 2 && pollerElement.hasAttribute("default") && pollerElement.getAttribute("default").equals("false"))) {
                parserContext.getReaderContext().error("A 'poller' element that provides a 'ref' must have no other attributes.", pollerElement);
            }
        }
        if (pollerElement.getChildNodes().getLength() != 0) {
            parserContext.getReaderContext().error("A 'poller' element that provides a 'ref' must have no child elements.", pollerElement);
        }
        targetBuilder.addPropertyReference("pollerMetadata", pollerElement.getAttribute("ref"));
    } else {
        BeanDefinition beanDefinition = parserContext.getDelegate().parseCustomElement(pollerElement, targetBuilder.getBeanDefinition());
        if (beanDefinition == null) {
            parserContext.getReaderContext().error("BeanDefinition must not be null", pollerElement);
        }
        targetBuilder.addPropertyValue("pollerMetadata", beanDefinition);
    }
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AbstractPollingEndpoint(org.springframework.integration.endpoint.AbstractPollingEndpoint)

Example 79 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-integration by spring-projects.

the class IntegrationNamespaceUtils method configureAdviceChain.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static ManagedList configureAdviceChain(Element adviceChainElement, Element txElement, boolean handleMessageAdvice, BeanDefinition parentBeanDefinition, ParserContext parserContext) {
    ManagedList adviceChain = new ManagedList();
    if (txElement != null) {
        adviceChain.add(configureTransactionAttributes(txElement, handleMessageAdvice));
    }
    if (adviceChainElement != null) {
        NodeList childNodes = adviceChainElement.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                Element childElement = (Element) child;
                String localName = child.getLocalName();
                if ("bean".equals(localName)) {
                    BeanDefinitionHolder holder = parserContext.getDelegate().parseBeanDefinitionElement(childElement, parentBeanDefinition);
                    parserContext.registerBeanComponent(new BeanComponentDefinition(holder));
                    adviceChain.add(new RuntimeBeanReference(holder.getBeanName()));
                } else if ("ref".equals(localName)) {
                    String ref = childElement.getAttribute("bean");
                    adviceChain.add(new RuntimeBeanReference(ref));
                } else {
                    BeanDefinition customBeanDefinition = parserContext.getDelegate().parseCustomElement(childElement, parentBeanDefinition);
                    if (customBeanDefinition == null) {
                        parserContext.getReaderContext().error("failed to parse custom element '" + localName + "'", childElement);
                    }
                    adviceChain.add(customBeanDefinition);
                }
            }
        }
    }
    return adviceChain;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) ManagedList(org.springframework.beans.factory.support.ManagedList) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AbstractPollingEndpoint(org.springframework.integration.endpoint.AbstractPollingEndpoint)

Example 80 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-integration by spring-projects.

the class IntegrationNamespaceUtils method createExpressionDefinitionFromValueOrExpression.

public static BeanDefinition createExpressionDefinitionFromValueOrExpression(String valueElementName, String expressionElementName, ParserContext parserContext, Element element, boolean oneRequired) {
    Assert.hasText(valueElementName, "'valueElementName' must not be empty");
    Assert.hasText(expressionElementName, "'expressionElementName' must not be empty");
    String valueElementValue = element.getAttribute(valueElementName);
    String expressionElementValue = element.getAttribute(expressionElementName);
    boolean hasAttributeValue = StringUtils.hasText(valueElementValue);
    boolean hasAttributeExpression = StringUtils.hasText(expressionElementValue);
    if (hasAttributeValue && hasAttributeExpression) {
        parserContext.getReaderContext().error("Only one of '" + valueElementName + "' or '" + expressionElementName + "' is allowed", element);
    }
    if (oneRequired && (!hasAttributeValue && !hasAttributeExpression)) {
        parserContext.getReaderContext().error("One of '" + valueElementName + "' or '" + expressionElementName + "' is required", element);
    }
    BeanDefinition expressionDef;
    if (hasAttributeValue) {
        expressionDef = new RootBeanDefinition(LiteralExpression.class);
        expressionDef.getConstructorArgumentValues().addGenericArgumentValue(valueElementValue);
    } else {
        expressionDef = createExpressionDefIfAttributeDefined(expressionElementName, element);
    }
    return expressionDef;
}
Also used : LiteralExpression(org.springframework.expression.common.LiteralExpression) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)601 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)188 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)116 Test (org.junit.jupiter.api.Test)112 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)89 Element (org.w3c.dom.Element)72 Test (org.junit.Test)66 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)53 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)41 ManagedList (org.springframework.beans.factory.support.ManagedList)39 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)36 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)35 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)34 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)34 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)31 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)30 Method (java.lang.reflect.Method)28 ArrayList (java.util.ArrayList)28 Map (java.util.Map)27