Search in sources :

Example 41 with BeanDefinitionHolder

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

the class XBeanBeanDefinitionDocumentReader method processBeanDefinition.

/**
 * Process the given bean element, parsing the bean definition
 * and registering it with the registry.
 */
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
    BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
    if (bdHolder != null) {
        bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
        // Register the final decorated instance.
        BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
        // Send registration event.
        getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
    }
}
Also used : BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition)

Example 42 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project cxf by apache.

the class AbstractBeanDefinitionParser method setFirstChildAsProperty.

protected void setFirstChildAsProperty(Element element, ParserContext ctx, BeanDefinitionBuilder bean, String propertyName) {
    Element first = getFirstChild(element);
    if (first == null) {
        throw new IllegalStateException(propertyName + " property must have child elements!");
    }
    String id;
    BeanDefinition child;
    if (first.getNamespaceURI().equals(BeanDefinitionParserDelegate.BEANS_NAMESPACE_URI)) {
        String name = first.getLocalName();
        if ("ref".equals(name)) {
            id = first.getAttribute("bean");
            if (id == null) {
                throw new IllegalStateException("<ref> elements must have a \"bean\" attribute!");
            }
            bean.addPropertyReference(propertyName, id);
            return;
        } else if ("bean".equals(name)) {
            BeanDefinitionHolder bdh = ctx.getDelegate().parseBeanDefinitionElement(first);
            child = bdh.getBeanDefinition();
            bean.addPropertyValue(propertyName, child);
            return;
        } else {
            throw new UnsupportedOperationException("Elements with the name " + name + " are not currently " + "supported as sub elements of " + element.getLocalName());
        }
    }
    child = ctx.getDelegate().parseCustomElement(first, bean.getBeanDefinition());
    bean.addPropertyValue(propertyName, child);
}
Also used : JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 43 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project dubbo-faker by moyada.

the class MonitorBeanDefinitionScanner method processBeanDefinitions.

private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
    initAppInfo();
    for (BeanDefinitionHolder holder : beanDefinitions) {
        AbstractBeanDefinition definition = (AbstractBeanDefinition) holder.getBeanDefinition();
        Class<?> beanClass = BeanDefinitionUtil.getClass(definition);
        if (null == beanClass) {
            continue;
        }
        ListenerInfo listenerInfo = ListenerAnalyser.getListenerInfo(beanClass);
        if (null == listenerInfo) {
            continue;
        }
        initData(listenerInfo);
        // 修改 BeanDefinition 类信息
        Class<?> newBeanClass = proxy(beanClass, listenerInfo.getListenerMethods());
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Creating MonitorProxy Wrapper Bean name '" + holder.getBeanName() + "', type is '" + definition.getBeanClassName() + "'");
        }
        definition.setBeanClass(newBeanClass);
    }
}
Also used : ListenerInfo(io.moyada.spring.boot.sharingan.context.ListenerInfo) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder)

Example 44 with BeanDefinitionHolder

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

the class ConfigurationClassParser method parse.

public void parse(Set<BeanDefinitionHolder> configCandidates) {
    this.deferredImportSelectors = new LinkedList<>();
    for (BeanDefinitionHolder holder : configCandidates) {
        BeanDefinition bd = holder.getBeanDefinition();
        try {
            if (bd instanceof AnnotatedBeanDefinition) {
                parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());
            } else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) {
                parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName());
            } else {
                parse(bd.getBeanClassName(), holder.getBeanName());
            }
        } catch (BeanDefinitionStoreException ex) {
            throw ex;
        } catch (Throwable ex) {
            throw new BeanDefinitionStoreException("Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex);
        }
    }
    processDeferredImportSelectors();
}
Also used : AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 45 with BeanDefinitionHolder

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

the class ConfigurationClassParser method doProcessConfigurationClass.

/**
	 * Apply processing and build a complete {@link ConfigurationClass} by reading the
	 * annotations, members and methods from the source class. This method can be called
	 * multiple times as relevant sources are discovered.
	 * @param configClass the configuration class being build
	 * @param sourceClass a source class
	 * @return the superclass, or {@code null} if none found or previously processed
	 */
protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass) throws IOException {
    // Recursively process any member (nested) classes first
    processMemberClasses(configClass, sourceClass);
    // Process any @PropertySource annotations
    for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(sourceClass.getMetadata(), PropertySources.class, org.springframework.context.annotation.PropertySource.class)) {
        if (this.environment instanceof ConfigurableEnvironment) {
            processPropertySource(propertySource);
        } else {
            logger.warn("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() + "]. Reason: Environment must implement ConfigurableEnvironment");
        }
    }
    // Process any @ComponentScan annotations
    Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
    if (!componentScans.isEmpty() && !this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
        for (AnnotationAttributes componentScan : componentScans) {
            // The config class is annotated with @ComponentScan -> perform the scan immediately
            Set<BeanDefinitionHolder> scannedBeanDefinitions = this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
            // Check the set of scanned definitions for any further config classes and parse recursively if necessary
            for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
                if (ConfigurationClassUtils.checkConfigurationClassCandidate(holder.getBeanDefinition(), this.metadataReaderFactory)) {
                    parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName());
                }
            }
        }
    }
    // Process any @Import annotations
    processImports(configClass, sourceClass, getImports(sourceClass), true);
    // Process any @ImportResource annotations
    if (sourceClass.getMetadata().isAnnotated(ImportResource.class.getName())) {
        AnnotationAttributes importResource = AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
        String[] resources = importResource.getStringArray("locations");
        Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
        for (String resource : resources) {
            String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
            configClass.addImportedResource(resolvedResource, readerClass);
        }
    }
    // Process individual @Bean methods
    Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
    for (MethodMetadata methodMetadata : beanMethods) {
        configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
    }
    // Process default methods on interfaces
    processInterfaces(configClass, sourceClass);
    // Process superclass, if any
    if (sourceClass.getMetadata().hasSuperClass()) {
        String superclass = sourceClass.getMetadata().getSuperClassName();
        if (!superclass.startsWith("java") && !this.knownSuperclasses.containsKey(superclass)) {
            this.knownSuperclasses.put(superclass, configClass);
            // Superclass found, return its annotation metadata and recurse
            return sourceClass.getSuperClass();
        }
    }
    // No superclass -> processing is complete
    return null;
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) MethodMetadata(org.springframework.core.type.MethodMetadata)

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