Search in sources :

Example 36 with BeanDefinitionHolder

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

the class ClassPathBeanDefinitionScanner method doScan.

/**
 * Perform a scan within the specified base packages,
 * returning the registered bean definitions.
 * <p>This method does <i>not</i> register an annotation config processor
 * but rather leaves this up to the caller.
 * @param basePackages the packages to check for annotated classes
 * @return set of beans registered if any for tooling registration purposes (never {@code null})
 */
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
    Assert.notEmpty(basePackages, "At least one base package must be specified");
    Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
    for (String basePackage : basePackages) {
        Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
        for (BeanDefinition candidate : candidates) {
            ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
            candidate.setScope(scopeMetadata.getScopeName());
            String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
            if (candidate instanceof AbstractBeanDefinition) {
                postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
            }
            if (candidate instanceof AnnotatedBeanDefinition) {
                AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
            }
            if (checkCandidate(beanName, candidate)) {
                BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
                definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
                beanDefinitions.add(definitionHolder);
                registerBeanDefinition(definitionHolder, this.registry);
            }
        }
    }
    return beanDefinitions;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 37 with BeanDefinitionHolder

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

the class ComponentScanBeanDefinitionParser method registerComponents.

protected void registerComponents(XmlReaderContext readerContext, Set<BeanDefinitionHolder> beanDefinitions, Element element) {
    Object source = readerContext.extractSource(element);
    CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), source);
    for (BeanDefinitionHolder beanDefHolder : beanDefinitions) {
        compositeDef.addNestedComponent(new BeanComponentDefinition(beanDefHolder));
    }
    // Register annotation config processors, if necessary.
    boolean annotationConfig = true;
    if (element.hasAttribute(ANNOTATION_CONFIG_ATTRIBUTE)) {
        annotationConfig = Boolean.parseBoolean(element.getAttribute(ANNOTATION_CONFIG_ATTRIBUTE));
    }
    if (annotationConfig) {
        Set<BeanDefinitionHolder> processorDefinitions = AnnotationConfigUtils.registerAnnotationConfigProcessors(readerContext.getRegistry(), source);
        for (BeanDefinitionHolder processorDefinition : processorDefinitions) {
            compositeDef.addNestedComponent(new BeanComponentDefinition(processorDefinition));
        }
    }
    readerContext.fireComponentRegistered(compositeDef);
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition)

Example 38 with BeanDefinitionHolder

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

the class AnnotationConfigBeanDefinitionParser method parse.

@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
    Object source = parserContext.extractSource(element);
    // Obtain bean definitions for all relevant BeanPostProcessors.
    Set<BeanDefinitionHolder> processorDefinitions = AnnotationConfigUtils.registerAnnotationConfigProcessors(parserContext.getRegistry(), source);
    // Register component for the surrounding <context:annotation-config> element.
    CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
    parserContext.pushContainingComponent(compDefinition);
    // Nest the concrete beans in the surrounding component.
    for (BeanDefinitionHolder processorDefinition : processorDefinitions) {
        parserContext.registerComponent(new BeanComponentDefinition(processorDefinition));
    }
    // Finally register the composite component.
    parserContext.popAndRegisterContainingComponent();
    return null;
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) Nullable(org.springframework.lang.Nullable)

Example 39 with BeanDefinitionHolder

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

the class MappingMongoConverterParser method parseConverter.

@Nullable
public BeanMetadataElement parseConverter(Element element, ParserContext parserContext) {
    String converterRef = element.getAttribute("ref");
    if (StringUtils.hasText(converterRef)) {
        return new RuntimeBeanReference(converterRef);
    }
    Element beanElement = DomUtils.getChildElementByTagName(element, "bean");
    if (beanElement != null) {
        BeanDefinitionHolder beanDef = parserContext.getDelegate().parseBeanDefinitionElement(beanElement);
        beanDef = parserContext.getDelegate().decorateBeanDefinitionIfRequired(beanElement, beanDef);
        return beanDef;
    }
    parserContext.getReaderContext().error("Element <converter> must specify 'ref' or contain a bean definition for the converter", element);
    return null;
}
Also used : BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Nullable(org.springframework.lang.Nullable)

Example 40 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project geronimo-xbean by apache.

the class XBeanNamespaceHandler method parseCustomMapElement.

protected Object parseCustomMapElement(MappingMetaData metadata, Element element, String name) {
    Map map = new ManagedMap();
    Element parent = (Element) element.getParentNode();
    String entryName = metadata.getMapEntryName(getLocalName(parent), name);
    String keyName = metadata.getMapKeyName(getLocalName(parent), name);
    String dups = metadata.getMapDupsMode(getLocalName(parent), name);
    boolean flat = metadata.isFlatMap(getLocalName(parent), name);
    String defaultKey = metadata.getMapDefaultKey(getLocalName(parent), name);
    if (entryName == null)
        entryName = "property";
    if (keyName == null)
        keyName = "key";
    if (dups == null)
        dups = "replace";
    // TODO : support further customizations
    // String valueName = "value";
    // boolean keyIsAttr = true;
    // boolean valueIsAttr = false;
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element) {
            Element childElement = (Element) node;
            String localName = childElement.getLocalName();
            String uri = childElement.getNamespaceURI();
            if (localName == null || localName.equals("xmlns") || localName.startsWith("xmlns:")) {
                continue;
            }
            // attributes from namespace-specific attributes
            if (!flat && !isEmpty(uri) && localName.equals(entryName)) {
                String key = childElement.getAttribute(keyName);
                if (key == null || key.length() == 0) {
                    key = defaultKey;
                }
                if (key == null) {
                    throw new RuntimeException("No key defined for map " + entryName);
                }
                Object keyValue = getValue(key, null);
                Element valueElement = getFirstChildElement(childElement);
                Object value;
                if (valueElement != null) {
                    String valueElUri = valueElement.getNamespaceURI();
                    String valueElLocalName = valueElement.getLocalName();
                    if (valueElUri == null || valueElUri.equals(SPRING_SCHEMA) || valueElUri.equals(SPRING_SCHEMA_COMPAT) || valueElUri.equals(BeanDefinitionParserDelegate.BEANS_NAMESPACE_URI)) {
                        if (BeanDefinitionParserDelegate.BEAN_ELEMENT.equals(valueElLocalName)) {
                            value = parserContext.getDelegate().parseBeanDefinitionElement(valueElement, null);
                        } else {
                            value = parserContext.getDelegate().parsePropertySubElement(valueElement, null);
                        }
                    } else {
                        value = parserContext.getDelegate().parseCustomElement(valueElement);
                    }
                } else {
                    value = getElementText(childElement);
                }
                addValueToMap(map, keyValue, value, dups);
            } else if (flat && !isEmpty(uri)) {
                String key = childElement.getAttribute(keyName);
                if (key == null || key.length() == 0) {
                    key = defaultKey;
                }
                if (key == null) {
                    throw new RuntimeException("No key defined for map entry " + entryName);
                }
                Object keyValue = getValue(key, null);
                childElement.removeAttribute(keyName);
                BeanDefinitionHolder bdh = parseBeanFromExtensionElement(childElement);
                addValueToMap(map, keyValue, bdh, dups);
            }
        }
    }
    return map;
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap) ManagedMap(org.springframework.beans.factory.support.ManagedMap) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

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