Search in sources :

Example 86 with BeanDefinition

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

the class DefaultInboundChannelAdapterParser method parseExpression.

private BeanMetadataElement parseExpression(String expressionString, Element expressionElement, Element element, ParserContext parserContext) {
    BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionEvaluatingMessageSource.class);
    BeanDefinition expressionDef = null;
    if (StringUtils.hasText(expressionString)) {
        expressionDef = new RootBeanDefinition(ExpressionFactoryBean.class);
        expressionDef.getConstructorArgumentValues().addGenericArgumentValue(expressionString);
    } else {
        BeanDefinitionBuilder dynamicExpressionBuilder = BeanDefinitionBuilder.genericBeanDefinition(DynamicExpression.class);
        String key = expressionElement.getAttribute("key");
        String expressionSourceReference = expressionElement.getAttribute("source");
        dynamicExpressionBuilder.addConstructorArgValue(key);
        dynamicExpressionBuilder.addConstructorArgReference(expressionSourceReference);
        expressionDef = dynamicExpressionBuilder.getBeanDefinition();
    }
    sourceBuilder.addConstructorArgValue(expressionDef);
    sourceBuilder.addConstructorArgValue(null);
    this.parseHeaderExpressions(sourceBuilder, element, parserContext);
    return sourceBuilder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ExpressionFactoryBean(org.springframework.integration.config.ExpressionFactoryBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 87 with BeanDefinition

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

the class GatewayParser method parse.

@Override
@SuppressWarnings("rawtypes")
public BeanDefinition parse(final Element element, ParserContext parserContext) {
    boolean isNested = parserContext.isNested();
    final Map<String, Object> gatewayAttributes = new HashMap<String, Object>();
    gatewayAttributes.put("name", element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE));
    gatewayAttributes.put("defaultPayloadExpression", element.getAttribute("default-payload-expression"));
    gatewayAttributes.put("defaultRequestChannel", element.getAttribute(isNested ? "request-channel" : "default-request-channel"));
    gatewayAttributes.put("defaultReplyChannel", element.getAttribute(isNested ? "reply-channel" : "default-reply-channel"));
    gatewayAttributes.put("errorChannel", element.getAttribute("error-channel"));
    String asyncExecutor = element.getAttribute("async-executor");
    if (!element.hasAttribute("async-executor") || StringUtils.hasLength(asyncExecutor)) {
        gatewayAttributes.put("asyncExecutor", asyncExecutor);
    } else {
        gatewayAttributes.put("asyncExecutor", null);
    }
    gatewayAttributes.put("mapper", element.getAttribute("mapper"));
    gatewayAttributes.put("defaultReplyTimeout", element.getAttribute(isNested ? "reply-timeout" : "default-reply-timeout"));
    gatewayAttributes.put("defaultRequestTimeout", element.getAttribute(isNested ? "request-timeout" : "default-request-timeout"));
    List<Element> headerElements = DomUtils.getChildElementsByTagName(element, "default-header");
    if (!CollectionUtils.isEmpty(headerElements)) {
        List<Map<String, Object>> headers = new ArrayList<Map<String, Object>>(headerElements.size());
        for (Element e : headerElements) {
            Map<String, Object> header = new HashMap<String, Object>();
            header.put("name", e.getAttribute("name"));
            header.put("value", e.getAttribute("value"));
            header.put("expression", e.getAttribute("expression"));
            headers.add(header);
        }
        gatewayAttributes.put("defaultHeaders", headers.toArray(new Map[headers.size()]));
    }
    List<Element> methodElements = DomUtils.getChildElementsByTagName(element, "method");
    if (!CollectionUtils.isEmpty(methodElements)) {
        Map<String, BeanDefinition> methodMetadataMap = new ManagedMap<String, BeanDefinition>();
        for (Element methodElement : methodElements) {
            String methodName = methodElement.getAttribute("name");
            BeanDefinitionBuilder methodMetadataBuilder = BeanDefinitionBuilder.genericBeanDefinition(GatewayMethodMetadata.class);
            methodMetadataBuilder.addPropertyValue("requestChannelName", methodElement.getAttribute("request-channel"));
            methodMetadataBuilder.addPropertyValue("replyChannelName", methodElement.getAttribute("reply-channel"));
            methodMetadataBuilder.addPropertyValue("requestTimeout", methodElement.getAttribute("request-timeout"));
            methodMetadataBuilder.addPropertyValue("replyTimeout", methodElement.getAttribute("reply-timeout"));
            boolean hasMapper = StringUtils.hasText(element.getAttribute("mapper"));
            Assert.state(!hasMapper || !StringUtils.hasText(element.getAttribute("payload-expression")), "'payload-expression' is not allowed when a 'mapper' is provided");
            IntegrationNamespaceUtils.setValueIfAttributeDefined(methodMetadataBuilder, methodElement, "payload-expression");
            List<Element> invocationHeaders = DomUtils.getChildElementsByTagName(methodElement, "header");
            if (!CollectionUtils.isEmpty(invocationHeaders)) {
                Assert.state(!hasMapper, "header elements are not allowed when a 'mapper' is provided");
                Map<String, Object> headerExpressions = new ManagedMap<String, Object>();
                for (Element headerElement : invocationHeaders) {
                    BeanDefinition expressionDef = IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression("value", "expression", parserContext, headerElement, true);
                    headerExpressions.put(headerElement.getAttribute("name"), expressionDef);
                }
                methodMetadataBuilder.addPropertyValue("headerExpressions", headerExpressions);
            }
            methodMetadataMap.put(methodName, methodMetadataBuilder.getBeanDefinition());
        }
        gatewayAttributes.put("methods", methodMetadataMap);
    }
    gatewayAttributes.put("serviceInterface", element.getAttribute("service-interface"));
    BeanDefinitionHolder gatewayHolder = this.registrar.parse(gatewayAttributes);
    if (isNested) {
        return gatewayHolder.getBeanDefinition();
    } else {
        BeanDefinitionReaderUtils.registerBeanDefinition(gatewayHolder, parserContext.getRegistry());
        return null;
    }
}
Also used : HashMap(java.util.HashMap) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) ManagedMap(org.springframework.beans.factory.support.ManagedMap) HashMap(java.util.HashMap) Map(java.util.Map) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Example 88 with BeanDefinition

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

the class ChainParser method resolveId.

@Override
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) throws BeanDefinitionStoreException {
    String id = super.resolveId(element, definition, parserContext);
    BeanDefinition containingBeanDefinition = parserContext.getContainingBeanDefinition();
    if (containingBeanDefinition != null) {
        String nestedChainIdPrefix = (String) containingBeanDefinition.getAttribute(SI_CHAIN_NESTED_ID_ATTRIBUTE);
        if (StringUtils.hasText(nestedChainIdPrefix)) {
            id = nestedChainIdPrefix + "$child." + id;
        }
    }
    definition.setAttribute(SI_CHAIN_NESTED_ID_ATTRIBUTE, id);
    return id;
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 89 with BeanDefinition

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

the class ChainParser method parseChild.

private BeanMetadataElement parseChild(String chainHandlerId, Element element, int order, ParserContext parserContext, BeanDefinition parentDefinition) {
    BeanDefinitionHolder holder = null;
    String id = element.getAttribute(ID_ATTRIBUTE);
    boolean hasId = StringUtils.hasText(id);
    String handlerComponentName = chainHandlerId + "$child" + (hasId ? "." + id : "#" + order);
    if ("bean".equals(element.getLocalName())) {
        holder = parserContext.getDelegate().parseBeanDefinitionElement(element, parentDefinition);
    } else {
        this.validateChild(element, parserContext);
        BeanDefinition beanDefinition = parserContext.getDelegate().parseCustomElement(element, parentDefinition);
        if (beanDefinition == null) {
            parserContext.getReaderContext().error("child BeanDefinition must not be null", element);
            return null;
        } else {
            holder = new BeanDefinitionHolder(beanDefinition, handlerComponentName + IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX);
        }
    }
    holder.getBeanDefinition().getPropertyValues().add("componentName", handlerComponentName);
    if (hasId) {
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
        return new RuntimeBeanReference(holder.getBeanName());
    }
    return holder;
}
Also used : BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 90 with BeanDefinition

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

the class DefaultConfiguringBeanFactoryPostProcessor method registerErrorChannel.

/**
 * Register an error channel in the given BeanDefinitionRegistry.
 */
private void registerErrorChannel(BeanDefinitionRegistry registry) {
    if (this.logger.isInfoEnabled()) {
        this.logger.info("No bean named '" + IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME + "' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.");
    }
    registry.registerBeanDefinition(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, new RootBeanDefinition(PublishSubscribeChannel.class));
    BeanDefinition loggingHandler = BeanDefinitionBuilder.genericBeanDefinition(LoggingHandler.class).addConstructorArgValue("ERROR").getBeanDefinition();
    String errorLoggerBeanName = ERROR_LOGGER_BEAN_NAME + IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX;
    registry.registerBeanDefinition(errorLoggerBeanName, loggingHandler);
    BeanDefinitionBuilder loggingEndpointBuilder = BeanDefinitionBuilder.genericBeanDefinition(ConsumerEndpointFactoryBean.class).addPropertyReference("handler", errorLoggerBeanName).addPropertyValue("inputChannelName", IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME);
    BeanComponentDefinition componentDefinition = new BeanComponentDefinition(loggingEndpointBuilder.getBeanDefinition(), ERROR_LOGGER_BEAN_NAME);
    BeanDefinitionReaderUtils.registerBeanDefinition(componentDefinition, registry);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) 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