Search in sources :

Example 21 with BeanDefinitionHolder

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

the class WireTapParser method parse.

@Override
public String parse(Element element, ParserContext parserContext) {
    Object source = parserContext.extractSource(element);
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(WireTap.class);
    String targetRef = element.getAttribute("channel");
    if (!StringUtils.hasText(targetRef)) {
        parserContext.getReaderContext().error("The 'channel' attribute is required.", element);
    }
    builder.addConstructorArgReference(targetRef);
    String selectorRef = element.getAttribute("selector");
    String selectorExpression = element.getAttribute("selector-expression");
    if (StringUtils.hasText(selectorRef) && StringUtils.hasText(selectorExpression)) {
        parserContext.getReaderContext().error("Only one of 'selector' or 'selector-expression' is allowed", source);
    }
    if (StringUtils.hasText(selectorRef)) {
        builder.addConstructorArgReference(selectorRef);
    } else if (StringUtils.hasText(selectorExpression)) {
        BeanDefinitionBuilder expressionBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionFactoryBean.class);
        expressionBuilder.addConstructorArgValue(selectorExpression);
        BeanDefinitionBuilder eemsBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionEvaluatingSelector.class);
        eemsBuilder.addConstructorArgValue(expressionBuilder.getBeanDefinition());
        builder.addConstructorArgValue(eemsBuilder.getBeanDefinition());
    }
    String timeout = element.getAttribute("timeout");
    if (StringUtils.hasText(timeout)) {
        builder.addPropertyValue("timeout", Long.parseLong(timeout));
    }
    String id = element.getAttribute("id");
    if (StringUtils.hasText(id)) {
        BeanDefinitionReaderUtils.registerBeanDefinition(new BeanDefinitionHolder(builder.getBeanDefinition(), id), parserContext.getRegistry());
        return id;
    }
    return BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), parserContext.getRegistry());
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ExpressionFactoryBean(org.springframework.integration.config.ExpressionFactoryBean) ExpressionEvaluatingSelector(org.springframework.integration.filter.ExpressionEvaluatingSelector) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder)

Example 22 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project HotswapAgent by HotswapProjects.

the class ClassPathBeanDefinitionScannerAgent method defineBean.

/**
 * Resolve candidate to a bean definition and (re)load in Spring.
 * Synchronize to avoid parallel bean definition - usually on reload the beans are interrelated
 * and parallel load will cause concurrent modification exception.
 *
 * @param candidate the candidate to reload
 */
public void defineBean(BeanDefinition candidate) {
    synchronized (getClass()) {
        // TODO sychronize on DefaultListableFactory.beanDefinitionMap?
        ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
        candidate.setScope(scopeMetadata.getScopeName());
        String beanName = this.beanNameGenerator.generateBeanName(candidate, registry);
        if (candidate instanceof AbstractBeanDefinition) {
            postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
        }
        if (candidate instanceof AnnotatedBeanDefinition) {
            processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
        }
        removeIfExists(beanName);
        BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
        definitionHolder = applyScopedProxyMode(scopeMetadata, definitionHolder, registry);
        LOGGER.reload("Registering Spring bean '{}'", beanName);
        LOGGER.debug("Bean definition '{}'", beanName, candidate);
        registerBeanDefinition(definitionHolder, registry);
        freezeConfiguration();
    }
    ProxyReplacer.clearAllProxies();
}
Also used : AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder)

Example 23 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project fabric8 by fabric8io.

the class AbstractServiceRegistar method registerBeanDefinitions.

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    for (Method method : REFLECTIONS.getMethodsAnnotatedWith(Factory.class)) {
        String methodName = method.getName();
        Class sourceType = getSourceType(method);
        Class targetType = method.getReturnType();
        Class beanType = method.getDeclaringClass();
        BeanDefinitionHolder holder = createConverterBean(beanType, methodName, sourceType, targetType);
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
    }
    for (Field field : REFLECTIONS.getFieldsAnnotatedWith(ServiceName.class)) {
        Class targetClass = field.getType();
        Alias alias = field.getAnnotation(Alias.class);
        ServiceName name = field.getAnnotation(ServiceName.class);
        PortName port = field.getAnnotation(PortName.class);
        Protocol protocol = field.getAnnotation(Protocol.class);
        External external = field.getAnnotation(External.class);
        String serviceName = name != null ? name.value() : null;
        // We copy the service since we are going to add properties to it.
        Service serviceInstance = new ServiceBuilder(getService(serviceName)).build();
        String servicePort = port != null ? port.value() : null;
        String serviceProtocol = protocol != null ? protocol.value() : DEFAULT_PROTOCOL;
        Boolean serviceExternal = external != null && external.value();
        String serviceAlias = alias != null ? alias.value() : createAlias(serviceName, targetClass, serviceProtocol, servicePort, serviceExternal);
        // Add annotation info as additional properties
        serviceInstance.getAdditionalProperties().put(ALIAS, serviceAlias);
        serviceInstance.getAdditionalProperties().put(PROTOCOL, serviceProtocol);
        serviceInstance.getAdditionalProperties().put(EXTERNAL, serviceExternal);
        // We don't want to add a fallback value to the attributes.
        if (port != null) {
            serviceInstance.getAdditionalProperties().put(PORT, servicePort);
        }
        BeanDefinitionHolder holder = createServiceDefinition(serviceInstance, serviceAlias, serviceProtocol, servicePort, targetClass);
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
    }
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) Method(java.lang.reflect.Method) PortName(io.fabric8.annotations.PortName) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Field(java.lang.reflect.Field) ServiceName(io.fabric8.annotations.ServiceName) Alias(io.fabric8.annotations.Alias) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) External(io.fabric8.annotations.External) Protocol(io.fabric8.annotations.Protocol)

Example 24 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project fabric8 by fabric8io.

the class AbstractServiceRegistar method createServiceDefinition.

private BeanDefinitionHolder createServiceDefinition(Service service, String alias, String protocol, String port, Class type) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(KubernetesServiceFactoryBean.class);
    builder.addPropertyValue("name", alias);
    builder.addPropertyValue("service", service);
    builder.addPropertyValue("port", port);
    builder.addPropertyValue("type", type.getCanonicalName());
    builder.setAutowireMode(Autowire.BY_TYPE.value());
    // Add protocol qualifier
    builder.getBeanDefinition().addQualifier(new AutowireCandidateQualifier(ServiceName.class, KubernetesHelper.getName(service)));
    builder.getBeanDefinition().addQualifier(new AutowireCandidateQualifier(Protocol.class, protocol));
    builder.getBeanDefinition().addQualifier(new AutowireCandidateQualifier(PortName.class, port != null ? port : ""));
    return new BeanDefinitionHolder(builder.getBeanDefinition(), alias);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ServiceName(io.fabric8.annotations.ServiceName) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) Protocol(io.fabric8.annotations.Protocol) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier) PortName(io.fabric8.annotations.PortName)

Example 25 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project fabric8 by fabric8io.

the class AbstractServiceRegistar method createConverterBean.

private <S, T> BeanDefinitionHolder createConverterBean(Class type, String methodName, Class<S> sourceType, Class<T> targetType) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(FactoryConverter.class);
    String beanName = type.getName() + "." + methodName;
    builder.addPropertyValue("name", methodName);
    builder.addPropertyValue("type", type.getCanonicalName());
    builder.addPropertyValue("sourceType", sourceType.getCanonicalName());
    builder.addPropertyValue("targetType", targetType.getCanonicalName());
    builder.setAutowireMode(Autowire.BY_TYPE.value());
    return new BeanDefinitionHolder(builder.getBeanDefinition(), beanName);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder)

Aggregations

BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)94 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)30 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)20 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)18 Element (org.w3c.dom.Element)18 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)16 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)16 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)12 ManagedList (org.springframework.beans.factory.support.ManagedList)11 Node (org.w3c.dom.Node)11 ArrayList (java.util.ArrayList)8 ManagedMap (org.springframework.beans.factory.support.ManagedMap)6 LinkedHashSet (java.util.LinkedHashSet)5 Test (org.junit.jupiter.api.Test)5 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)5 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)5 NamedNodeMap (org.w3c.dom.NamedNodeMap)5 NodeList (org.w3c.dom.NodeList)5 Method (java.lang.reflect.Method)4 HashSet (java.util.HashSet)4