Search in sources :

Example 41 with BeanDefinitionStoreException

use of org.springframework.beans.factory.BeanDefinitionStoreException in project spring-cloud-stream by spring-cloud.

the class BindingBeanDefinitionRegistryUtils method registerBindingTargetBeanDefinition.

private static void registerBindingTargetBeanDefinition(Class<? extends Annotation> qualifier, String qualifierValue, String name, String bindingTargetInterfaceBeanName, String bindingTargetInterfaceMethodName, BeanDefinitionRegistry registry) {
    if (registry.containsBeanDefinition(name)) {
        throw new BeanDefinitionStoreException(bindingTargetInterfaceBeanName, name, "bean definition with this name already exists - " + registry.getBeanDefinition(name));
    }
    RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
    rootBeanDefinition.setFactoryBeanName(bindingTargetInterfaceBeanName);
    rootBeanDefinition.setUniqueFactoryMethodName(bindingTargetInterfaceMethodName);
    rootBeanDefinition.addQualifier(new AutowireCandidateQualifier(qualifier, qualifierValue));
    registry.registerBeanDefinition(name, rootBeanDefinition);
}
Also used : BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier)

Example 42 with BeanDefinitionStoreException

use of org.springframework.beans.factory.BeanDefinitionStoreException in project com.revolsys.open by revolsys.

the class BeanConfigurrer method processPlaceholderAttributes.

protected void processPlaceholderAttributes(final ConfigurableListableBeanFactory beanFactory, final String beanName, final Map<String, Object> attributes) throws BeansException {
    final StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver("${", "}", this.ignoreUnresolvablePlaceholders, null, attributes);
    final BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);
    // locations.
    if (!(beanName.equals(this.beanName) && beanFactory.equals(this.applicationContext))) {
        final BeanDefinition bd = beanFactory.getBeanDefinition(beanName);
        try {
            visitor.visitBeanDefinition(bd);
        } catch (final BeanDefinitionStoreException ex) {
            throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName, ex.getMessage());
        }
    }
    // NEW in Spring 2.5: resolve placeholders in alias target names and aliases
    // as well.
    beanFactory.resolveAliases(valueResolver);
}
Also used : StringValueResolver(org.springframework.util.StringValueResolver) PlaceholderResolvingStringValueResolver(com.revolsys.spring.util.PlaceholderResolvingStringValueResolver) PlaceholderResolvingStringValueResolver(com.revolsys.spring.util.PlaceholderResolvingStringValueResolver) BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition)

Example 43 with BeanDefinitionStoreException

use of org.springframework.beans.factory.BeanDefinitionStoreException in project com.revolsys.open by revolsys.

the class BeanConfigurrer method processPlaceholderAttributes.

protected void processPlaceholderAttributes(final ConfigurableListableBeanFactory beanFactory, final Map<String, Object> attributes) throws BeansException {
    final Map<String, Object> attributeMap = new LinkedHashMap<>();
    for (final Entry<String, Object> entry : attributes.entrySet()) {
        final String key = entry.getKey();
        final Object value = entry.getValue();
        if (!(value instanceof BeanReference)) {
            attributeMap.put(key, value);
        }
    }
    final StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver("${", "}", this.ignoreUnresolvablePlaceholders, null, attributeMap);
    final BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);
    final String[] beanNames = beanFactory.getBeanDefinitionNames();
    for (int i = 0; i < beanNames.length; i++) {
        // locations.
        if (!(beanNames[i].equals(this.beanName) && beanFactory.equals(this.applicationContext))) {
            final BeanDefinition bd = beanFactory.getBeanDefinition(beanNames[i]);
            try {
                visitor.visitBeanDefinition(bd);
            } catch (final BeanDefinitionStoreException ex) {
                throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i], ex.getMessage());
            }
        }
    }
    // NEW in Spring 2.5: resolve placeholders in alias target names and aliases
    // as well.
    beanFactory.resolveAliases(valueResolver);
}
Also used : PlaceholderResolvingStringValueResolver(com.revolsys.spring.util.PlaceholderResolvingStringValueResolver) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) LinkedHashMap(java.util.LinkedHashMap) StringValueResolver(org.springframework.util.StringValueResolver) PlaceholderResolvingStringValueResolver(com.revolsys.spring.util.PlaceholderResolvingStringValueResolver) BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor) BeanReference(com.revolsys.spring.BeanReference)

Example 44 with BeanDefinitionStoreException

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

the class GatewayProxyFactoryBean method createGatewayForMethod.

private MethodInvocationGateway createGatewayForMethod(Method method) {
    Gateway gatewayAnnotation = method.getAnnotation(Gateway.class);
    String requestChannelName = null;
    String replyChannelName = null;
    Expression requestTimeout = this.defaultRequestTimeout;
    Expression replyTimeout = this.defaultReplyTimeout;
    String payloadExpression = this.globalMethodMetadata != null ? this.globalMethodMetadata.getPayloadExpression() : null;
    Map<String, Expression> headerExpressions = new HashMap<String, Expression>();
    if (gatewayAnnotation != null) {
        requestChannelName = gatewayAnnotation.requestChannel();
        replyChannelName = gatewayAnnotation.replyChannel();
        /*
			 * INT-2636 Unspecified annotation attributes should not
			 * override the default values supplied by explicit configuration.
			 * There is a small risk that someone has used Long.MIN_VALUE explicitly
			 * to indicate an indefinite timeout on a gateway method and that will
			 * no longer work as expected; they will need to use, say, -1 instead.
			 */
        if (requestTimeout == null || gatewayAnnotation.requestTimeout() != Long.MIN_VALUE) {
            requestTimeout = new ValueExpression<>(gatewayAnnotation.requestTimeout());
        }
        if (StringUtils.hasText(gatewayAnnotation.requestTimeoutExpression())) {
            requestTimeout = ExpressionUtils.longExpression(gatewayAnnotation.requestTimeoutExpression());
        }
        if (replyTimeout == null || gatewayAnnotation.replyTimeout() != Long.MIN_VALUE) {
            replyTimeout = new ValueExpression<>(gatewayAnnotation.replyTimeout());
        }
        if (StringUtils.hasText(gatewayAnnotation.replyTimeoutExpression())) {
            replyTimeout = ExpressionUtils.longExpression(gatewayAnnotation.replyTimeoutExpression());
        }
        if (payloadExpression == null || StringUtils.hasText(gatewayAnnotation.payloadExpression())) {
            payloadExpression = gatewayAnnotation.payloadExpression();
        }
        if (!ObjectUtils.isEmpty(gatewayAnnotation.headers())) {
            for (GatewayHeader gatewayHeader : gatewayAnnotation.headers()) {
                String value = gatewayHeader.value();
                String expression = gatewayHeader.expression();
                String name = gatewayHeader.name();
                boolean hasValue = StringUtils.hasText(value);
                if (hasValue == StringUtils.hasText(expression)) {
                    throw new BeanDefinitionStoreException("exactly one of 'value' or 'expression' " + "is required on a gateway's header.");
                }
                headerExpressions.put(name, hasValue ? new LiteralExpression(value) : EXPRESSION_PARSER.parseExpression(expression));
            }
        }
    } else if (this.methodMetadataMap != null && this.methodMetadataMap.size() > 0) {
        GatewayMethodMetadata methodMetadata = this.methodMetadataMap.get(method.getName());
        if (methodMetadata != null) {
            if (StringUtils.hasText(methodMetadata.getPayloadExpression())) {
                payloadExpression = methodMetadata.getPayloadExpression();
            }
            if (!CollectionUtils.isEmpty(methodMetadata.getHeaderExpressions())) {
                headerExpressions.putAll(methodMetadata.getHeaderExpressions());
            }
            requestChannelName = methodMetadata.getRequestChannelName();
            replyChannelName = methodMetadata.getReplyChannelName();
            String reqTimeout = methodMetadata.getRequestTimeout();
            if (StringUtils.hasText(reqTimeout)) {
                requestTimeout = ExpressionUtils.longExpression(reqTimeout);
            }
            String repTimeout = methodMetadata.getReplyTimeout();
            if (StringUtils.hasText(repTimeout)) {
                replyTimeout = ExpressionUtils.longExpression(repTimeout);
            }
        }
    }
    Map<String, Object> headers = null;
    // We don't want to eagerly resolve the error channel here
    Object errorChannel = this.errorChannel == null ? this.errorChannelName : this.errorChannel;
    if (errorChannel != null && method.getReturnType().equals(void.class)) {
        headers = new HashMap<>();
        headers.put(MessageHeaders.ERROR_CHANNEL, errorChannel);
    }
    if (getMessageBuilderFactory() instanceof DefaultMessageBuilderFactory) {
        Set<String> headerNames = new HashSet<>(headerExpressions.keySet());
        if (this.globalMethodMetadata != null) {
            headerNames.addAll(this.globalMethodMetadata.getHeaderExpressions().keySet());
        }
        List<MethodParameter> methodParameters = GatewayMethodInboundMessageMapper.getMethodParameterList(method);
        for (MethodParameter methodParameter : methodParameters) {
            Header header = methodParameter.getParameterAnnotation(Header.class);
            if (header != null) {
                String headerName = GatewayMethodInboundMessageMapper.determineHeaderName(header, methodParameter);
                headerNames.add(headerName);
            }
        }
        for (String header : headerNames) {
            if ((MessageHeaders.ID.equals(header) || MessageHeaders.TIMESTAMP.equals(header))) {
                throw new BeanInitializationException("Messaging Gateway cannot override 'id' and 'timestamp' read-only headers.\n" + "Wrong headers configuration for " + getComponentName());
            }
        }
    }
    GatewayMethodInboundMessageMapper messageMapper = new GatewayMethodInboundMessageMapper(method, headerExpressions, this.globalMethodMetadata != null ? this.globalMethodMetadata.getHeaderExpressions() : null, headers, this.argsMapper, this.getMessageBuilderFactory());
    if (StringUtils.hasText(payloadExpression)) {
        messageMapper.setPayloadExpression(payloadExpression);
    }
    messageMapper.setBeanFactory(getBeanFactory());
    MethodInvocationGateway gateway = new MethodInvocationGateway(messageMapper);
    if (this.errorChannel != null) {
        gateway.setErrorChannel(this.errorChannel);
    } else if (StringUtils.hasText(this.errorChannelName)) {
        gateway.setErrorChannelName(this.errorChannelName);
    }
    if (this.getTaskScheduler() != null) {
        gateway.setTaskScheduler(this.getTaskScheduler());
    }
    gateway.setBeanName(this.getComponentName());
    if (StringUtils.hasText(requestChannelName)) {
        gateway.setRequestChannelName(requestChannelName);
    } else if (StringUtils.hasText(this.defaultRequestChannelName)) {
        gateway.setRequestChannelName(this.defaultRequestChannelName);
    } else {
        gateway.setRequestChannel(this.defaultRequestChannel);
    }
    if (StringUtils.hasText(replyChannelName)) {
        gateway.setReplyChannelName(replyChannelName);
    } else if (StringUtils.hasText(this.defaultReplyChannelName)) {
        gateway.setReplyChannelName(this.defaultReplyChannelName);
    } else {
        gateway.setReplyChannel(this.defaultReplyChannel);
    }
    if (requestTimeout == null) {
        gateway.setRequestTimeout(-1);
    } else if (requestTimeout instanceof ValueExpression) {
        gateway.setRequestTimeout(requestTimeout.getValue(Long.class));
    } else {
        messageMapper.setSendTimeoutExpression(requestTimeout);
    }
    if (replyTimeout == null) {
        gateway.setReplyTimeout(-1);
    } else if (replyTimeout instanceof ValueExpression) {
        gateway.setReplyTimeout(replyTimeout.getValue(Long.class));
    } else {
        messageMapper.setReplyTimeoutExpression(replyTimeout);
    }
    if (this.getBeanFactory() != null) {
        gateway.setBeanFactory(this.getBeanFactory());
    }
    if (replyTimeout != null) {
        gateway.setReceiveTimeoutExpression(replyTimeout);
    }
    gateway.setShouldTrack(this.shouldTrack);
    gateway.afterPropertiesSet();
    return gateway;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) HashMap(java.util.HashMap) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) LiteralExpression(org.springframework.expression.common.LiteralExpression) Header(org.springframework.messaging.handler.annotation.Header) GatewayHeader(org.springframework.integration.annotation.GatewayHeader) LiteralExpression(org.springframework.expression.common.LiteralExpression) ValueExpression(org.springframework.integration.expression.ValueExpression) Expression(org.springframework.expression.Expression) Gateway(org.springframework.integration.annotation.Gateway) ValueExpression(org.springframework.integration.expression.ValueExpression) DefaultMessageBuilderFactory(org.springframework.integration.support.DefaultMessageBuilderFactory) MethodParameter(org.springframework.core.MethodParameter) GatewayHeader(org.springframework.integration.annotation.GatewayHeader) HashSet(java.util.HashSet)

Example 45 with BeanDefinitionStoreException

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

the class MessagingGatewayRegistrar method parse.

public BeanDefinitionHolder parse(Map<String, Object> gatewayAttributes) {
    String defaultPayloadExpression = (String) gatewayAttributes.get("defaultPayloadExpression");
    @SuppressWarnings("unchecked") Map<String, Object>[] defaultHeaders = (Map<String, Object>[]) gatewayAttributes.get("defaultHeaders");
    String defaultRequestChannel = (String) gatewayAttributes.get("defaultRequestChannel");
    String defaultReplyChannel = (String) gatewayAttributes.get("defaultReplyChannel");
    String errorChannel = (String) gatewayAttributes.get("errorChannel");
    String asyncExecutor = (String) gatewayAttributes.get("asyncExecutor");
    String mapper = (String) gatewayAttributes.get("mapper");
    boolean hasMapper = StringUtils.hasText(mapper);
    boolean hasDefaultPayloadExpression = StringUtils.hasText(defaultPayloadExpression);
    Assert.state(!hasMapper || !hasDefaultPayloadExpression, "'defaultPayloadExpression' is not allowed when a 'mapper' is provided");
    boolean hasDefaultHeaders = !ObjectUtils.isEmpty(defaultHeaders);
    Assert.state(!hasMapper || !hasDefaultHeaders, "'defaultHeaders' are not allowed when a 'mapper' is provided");
    BeanDefinitionBuilder gatewayProxyBuilder = BeanDefinitionBuilder.genericBeanDefinition(GatewayProxyFactoryBean.class);
    if (hasDefaultHeaders || hasDefaultPayloadExpression) {
        BeanDefinitionBuilder methodMetadataBuilder = BeanDefinitionBuilder.genericBeanDefinition(GatewayMethodMetadata.class);
        if (hasDefaultPayloadExpression) {
            methodMetadataBuilder.addPropertyValue("payloadExpression", defaultPayloadExpression);
        }
        Map<String, Object> headerExpressions = new ManagedMap<String, Object>();
        for (Map<String, Object> header : defaultHeaders) {
            String headerValue = (String) header.get("value");
            String headerExpression = (String) header.get("expression");
            boolean hasValue = StringUtils.hasText(headerValue);
            if (hasValue == StringUtils.hasText(headerExpression)) {
                throw new BeanDefinitionStoreException("exactly one of 'value' or 'expression' " + "is required on a gateway's header.");
            }
            BeanDefinition expressionDef = new RootBeanDefinition(hasValue ? LiteralExpression.class : ExpressionFactoryBean.class);
            expressionDef.getConstructorArgumentValues().addGenericArgumentValue(hasValue ? headerValue : headerExpression);
            headerExpressions.put((String) header.get("name"), expressionDef);
        }
        methodMetadataBuilder.addPropertyValue("headerExpressions", headerExpressions);
        gatewayProxyBuilder.addPropertyValue("globalMethodMetadata", methodMetadataBuilder.getBeanDefinition());
    }
    if (StringUtils.hasText(defaultRequestChannel)) {
        gatewayProxyBuilder.addPropertyValue("defaultRequestChannelName", defaultRequestChannel);
    }
    if (StringUtils.hasText(defaultReplyChannel)) {
        gatewayProxyBuilder.addPropertyValue("defaultReplyChannelName", defaultReplyChannel);
    }
    if (StringUtils.hasText(errorChannel)) {
        gatewayProxyBuilder.addPropertyValue("errorChannelName", errorChannel);
    }
    if (asyncExecutor == null || AnnotationConstants.NULL.equals(asyncExecutor)) {
        gatewayProxyBuilder.addPropertyValue("asyncExecutor", null);
    } else if (StringUtils.hasText(asyncExecutor)) {
        gatewayProxyBuilder.addPropertyReference("asyncExecutor", asyncExecutor);
    }
    if (StringUtils.hasText(mapper)) {
        gatewayProxyBuilder.addPropertyReference("mapper", mapper);
    }
    gatewayProxyBuilder.addPropertyValue("defaultRequestTimeoutExpressionString", gatewayAttributes.get("defaultRequestTimeout"));
    gatewayProxyBuilder.addPropertyValue("defaultReplyTimeoutExpressionString", gatewayAttributes.get("defaultReplyTimeout"));
    gatewayProxyBuilder.addPropertyValue("methodMetadataMap", gatewayAttributes.get("methods"));
    String serviceInterface = (String) gatewayAttributes.get("serviceInterface");
    if (!StringUtils.hasText(serviceInterface)) {
        serviceInterface = "org.springframework.integration.gateway.RequestReplyExchanger";
    }
    String id = (String) gatewayAttributes.get("name");
    if (!StringUtils.hasText(id)) {
        id = Introspector.decapitalize(serviceInterface.substring(serviceInterface.lastIndexOf(".") + 1));
    }
    gatewayProxyBuilder.addConstructorArgValue(serviceInterface);
    AbstractBeanDefinition beanDefinition = gatewayProxyBuilder.getBeanDefinition();
    beanDefinition.addMetadataAttribute(new BeanMetadataAttribute(IntegrationConfigUtils.FACTORY_BEAN_OBJECT_TYPE, serviceInterface));
    return new BeanDefinitionHolder(beanDefinition, id);
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) LiteralExpression(org.springframework.expression.common.LiteralExpression) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanMetadataAttribute(org.springframework.beans.BeanMetadataAttribute) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Map(java.util.Map) ManagedMap(org.springframework.beans.factory.support.ManagedMap) MultiValueMap(org.springframework.util.MultiValueMap) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Aggregations

BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)59 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)18 Test (org.junit.Test)12 IOException (java.io.IOException)11 LinkedHashSet (java.util.LinkedHashSet)7 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)6 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)6 HashSet (java.util.HashSet)5 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)5 BeanDefinitionVisitor (org.springframework.beans.factory.config.BeanDefinitionVisitor)5 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)5 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 MockServletContext (org.springframework.mock.web.test.MockServletContext)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)4 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)4 Resource (org.springframework.core.io.Resource)4 Method (java.lang.reflect.Method)3 LinkedHashMap (java.util.LinkedHashMap)3