Search in sources :

Example 11 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project dubbo by alibaba.

the class ServiceAnnotationBeanPostProcessor method registerServiceBeans.

/**
 * Registers Beans whose classes was annotated {@link Service}
 *
 * @param packagesToScan The base packages to scan
 * @param registry       {@link BeanDefinitionRegistry}
 */
private void registerServiceBeans(Set<String> packagesToScan, BeanDefinitionRegistry registry) {
    DubboClassPathBeanDefinitionScanner scanner = new DubboClassPathBeanDefinitionScanner(registry, environment, resourceLoader);
    BeanNameGenerator beanNameGenerator = resolveBeanNameGenerator(registry);
    scanner.setBeanNameGenerator(beanNameGenerator);
    scanner.addIncludeFilter(new AnnotationTypeFilter(Service.class));
    for (String packageToScan : packagesToScan) {
        // Registers @Service Bean first
        scanner.scan(packageToScan);
        // Finds all BeanDefinitionHolders of @Service whether @ComponentScan scans or not.
        Set<BeanDefinitionHolder> beanDefinitionHolders = findServiceBeanDefinitionHolders(scanner, packageToScan, registry, beanNameGenerator);
        if (!CollectionUtils.isEmpty(beanDefinitionHolders)) {
            for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) {
                registerServiceBean(beanDefinitionHolder, registry, scanner);
            }
            if (logger.isInfoEnabled()) {
                logger.info(beanDefinitionHolders.size() + " annotated Dubbo's @Service Components { " + beanDefinitionHolders + " } were scanned under package[" + packageToScan + "]");
            }
        } else {
            if (logger.isWarnEnabled()) {
                logger.warn("No Spring Bean annotating Dubbo's @Service was found under package[" + packageToScan + "]");
            }
        }
    }
}
Also used : AnnotationBeanNameGenerator(org.springframework.context.annotation.AnnotationBeanNameGenerator) BeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator) AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) Service(com.alibaba.dubbo.config.annotation.Service) DubboClassPathBeanDefinitionScanner(com.alibaba.dubbo.config.spring.context.annotation.DubboClassPathBeanDefinitionScanner)

Example 12 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project pentaho-platform by pentaho.

the class BeanParser method parseInternal.

@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    String originalClassName = element.getAttribute("class");
    element.setAttribute("class", BeanBuilder.class.getName());
    BeanDefinitionHolder holder = parserContext.getDelegate().parseBeanDefinitionElement(element);
    BeanDefinition definition = holder.getBeanDefinition();
    definition.setAttribute("originalClassName", originalClassName);
    parserContext.getDelegate().decorateBeanDefinitionIfRequired(element, holder);
    definition.setBeanClassName(BeanBuilder.class.getName());
    // BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(BeanBuilder.class.getName());
    definition.getPropertyValues().addPropertyValue("type", originalClassName);
    Map<String, String> propMap = new HashMap<String, String>();
    Element objectproperties = DomUtils.getChildElementByTagName(element, Const.ATTRIBUTES);
    if (objectproperties != null) {
        List props = DomUtils.getChildElementsByTagName(objectproperties, Const.ATTR);
        if (props != null) {
            for (Object o : props) {
                Element prop = (Element) o;
                String key = prop.getAttribute(Const.KEY);
                String value = prop.getAttribute(Const.VALUE);
                propMap.put(key, value);
            }
        }
    }
    definition.getPropertyValues().addPropertyValue(Const.ATTRIBUTES, propMap);
    return new GenericBeanDefinition(definition);
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) HashMap(java.util.HashMap) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) Element(org.w3c.dom.Element) List(java.util.List) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition)

Example 13 with BeanDefinitionHolder

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

the class IntegrationRegistrar method registerIntegrationEvaluationContext.

/**
 * Register {@link IntegrationEvaluationContextFactoryBean} bean, if necessary.
 * @param registry The {@link BeanDefinitionRegistry} to register additional {@link BeanDefinition}s.
 */
private void registerIntegrationEvaluationContext(BeanDefinitionRegistry registry) {
    if (!registry.containsBeanDefinition(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME)) {
        BeanDefinitionBuilder integrationEvaluationContextBuilder = BeanDefinitionBuilder.genericBeanDefinition(IntegrationEvaluationContextFactoryBean.class);
        integrationEvaluationContextBuilder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        BeanDefinitionHolder integrationEvaluationContextHolder = new BeanDefinitionHolder(integrationEvaluationContextBuilder.getBeanDefinition(), IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME);
        BeanDefinitionReaderUtils.registerBeanDefinition(integrationEvaluationContextHolder, registry);
    }
    if (!registry.containsBeanDefinition(IntegrationContextUtils.INTEGRATION_SIMPLE_EVALUATION_CONTEXT_BEAN_NAME)) {
        BeanDefinitionBuilder integrationEvaluationContextBuilder = BeanDefinitionBuilder.genericBeanDefinition(IntegrationSimpleEvaluationContextFactoryBean.class);
        integrationEvaluationContextBuilder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        BeanDefinitionHolder integrationEvaluationContextHolder = new BeanDefinitionHolder(integrationEvaluationContextBuilder.getBeanDefinition(), IntegrationContextUtils.INTEGRATION_SIMPLE_EVALUATION_CONTEXT_BEAN_NAME);
        BeanDefinitionReaderUtils.registerBeanDefinition(integrationEvaluationContextHolder, registry);
    }
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder)

Example 14 with BeanDefinitionHolder

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

use of org.springframework.beans.factory.config.BeanDefinitionHolder 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)

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