Search in sources :

Example 6 with ScopeMetadata

use of cn.taketoday.context.loader.ScopeMetadata in project today-infrastructure by TAKETODAY.

the class ClassPathBeanDefinitionScanner method scan.

/**
 * Perform a scan within the specified base packages and consume 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 operator set of beans registered if any for tooling registration purposes
 * @param basePackages the packages to check for annotated classes
 */
public void scan(@Nullable UnaryOperator<BeanDefinitionHolder> operator, String... basePackages) {
    for (String basePackage : basePackages) {
        try {
            scanCandidateComponents(basePackage, (metadataReader, factory) -> {
                ScannedGenericBeanDefinition candidate = new ScannedGenericBeanDefinition(metadataReader);
                Resource resource = metadataReader.getResource();
                candidate.setSource(resource);
                candidate.setResource(resource);
                ScopeMetadata scopeMetadata = scopeMetadataResolver.resolveScopeMetadata(candidate);
                candidate.setScope(scopeMetadata.getScopeName());
                String beanName = beanNameGenerator.generateBeanName(candidate, registry);
                postProcessBeanDefinition(candidate, beanName);
                AnnotationConfigUtils.processCommonDefinitionAnnotations(candidate);
                if (checkCandidate(beanName, candidate)) {
                    BeanDefinitionHolder holder = new BeanDefinitionHolder(candidate, beanName);
                    holder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, holder, registry);
                    if (operator != null) {
                        holder = operator.apply(holder);
                    }
                    registerBeanDefinition(holder, registry);
                }
            });
        } catch (IOException ex) {
            throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
        }
    }
}
Also used : ScopeMetadata(cn.taketoday.context.loader.ScopeMetadata) BeanDefinitionStoreException(cn.taketoday.beans.factory.BeanDefinitionStoreException) BeanDefinitionHolder(cn.taketoday.beans.factory.config.BeanDefinitionHolder) Resource(cn.taketoday.core.io.Resource) IOException(java.io.IOException)

Example 7 with ScopeMetadata

use of cn.taketoday.context.loader.ScopeMetadata in project today-infrastructure by TAKETODAY.

the class TestScopeMetadataResolver method resolveScopeMetadata.

@Override
public ScopeMetadata resolveScopeMetadata(BeanDefinition beanDefinition) {
    ScopeMetadata metadata = new ScopeMetadata();
    metadata.setScopeName("myCustomScope");
    return metadata;
}
Also used : ScopeMetadata(cn.taketoday.context.loader.ScopeMetadata)

Example 8 with ScopeMetadata

use of cn.taketoday.context.loader.ScopeMetadata in project today-framework by TAKETODAY.

the class ClassPathBeanDefinitionScanner method scan.

/**
 * Perform a scan within the specified base packages and consume 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 operator set of beans registered if any for tooling registration purposes
 * @param basePackages the packages to check for annotated classes
 */
public void scan(@Nullable UnaryOperator<BeanDefinitionHolder> operator, String... basePackages) {
    for (String basePackage : basePackages) {
        try {
            scanCandidateComponents(basePackage, (metadataReader, factory) -> {
                ScannedGenericBeanDefinition candidate = new ScannedGenericBeanDefinition(metadataReader);
                Resource resource = metadataReader.getResource();
                candidate.setSource(resource);
                candidate.setResource(resource);
                ScopeMetadata scopeMetadata = scopeMetadataResolver.resolveScopeMetadata(candidate);
                candidate.setScope(scopeMetadata.getScopeName());
                String beanName = beanNameGenerator.generateBeanName(candidate, registry);
                postProcessBeanDefinition(candidate, beanName);
                AnnotationConfigUtils.processCommonDefinitionAnnotations(candidate);
                if (checkCandidate(beanName, candidate)) {
                    BeanDefinitionHolder holder = new BeanDefinitionHolder(candidate, beanName);
                    holder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, holder, registry);
                    if (operator != null) {
                        holder = operator.apply(holder);
                    }
                    registerBeanDefinition(holder, registry);
                }
            });
        } catch (IOException ex) {
            throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
        }
    }
}
Also used : ScopeMetadata(cn.taketoday.context.loader.ScopeMetadata) BeanDefinitionStoreException(cn.taketoday.beans.factory.BeanDefinitionStoreException) BeanDefinitionHolder(cn.taketoday.beans.factory.config.BeanDefinitionHolder) Resource(cn.taketoday.core.io.Resource) IOException(java.io.IOException)

Example 9 with ScopeMetadata

use of cn.taketoday.context.loader.ScopeMetadata in project today-framework by TAKETODAY.

the class AnnotatedBeanDefinitionReader method doRegisterBean.

/**
 * Register a bean from the given bean class, deriving its metadata from
 * class-declared annotations.
 *
 * @param beanClass the class of the bean
 * @param name an explicit name for the bean
 * @param qualifiers specific qualifier annotations to consider, if any,
 * in addition to qualifiers at the bean class level
 * @param supplier a callback for creating an instance of the bean
 * (may be {@code null})
 * @param customizers one or more callbacks for customizing the factory's
 * {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
 */
private <T> void doRegisterBean(Class<T> beanClass, @Nullable String name, @Nullable Class<? extends Annotation>[] qualifiers, @Nullable Supplier<T> supplier, @Nullable BeanDefinitionCustomizer[] customizers) {
    AnnotatedGenericBeanDefinition definition = new AnnotatedGenericBeanDefinition(beanClass);
    if (this.conditionEvaluator.shouldSkip(definition.getMetadata())) {
        return;
    }
    definition.setInstanceSupplier(supplier);
    ScopeMetadata scopeMetadata = scopeMetadataResolver.resolveScopeMetadata(definition);
    definition.setScope(scopeMetadata.getScopeName());
    String beanName = name != null ? name : beanNameGenerator.generateBeanName(definition, registry);
    AnnotationConfigUtils.processCommonDefinitionAnnotations(definition);
    if (qualifiers != null) {
        for (Class<? extends Annotation> qualifier : qualifiers) {
            if (Primary.class == qualifier) {
                definition.setPrimary(true);
            } else if (Lazy.class == qualifier) {
                definition.setLazyInit(true);
            } else {
                definition.addQualifier(new AutowireCandidateQualifier(qualifier));
            }
        }
    }
    applyDynamicCustomizers(definition, customizers);
    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(definition, beanName);
    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
    BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
Also used : ScopeMetadata(cn.taketoday.context.loader.ScopeMetadata) BeanDefinitionHolder(cn.taketoday.beans.factory.config.BeanDefinitionHolder) AnnotatedGenericBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedGenericBeanDefinition) AutowireCandidateQualifier(cn.taketoday.beans.factory.support.AutowireCandidateQualifier)

Example 10 with ScopeMetadata

use of cn.taketoday.context.loader.ScopeMetadata in project today-framework by TAKETODAY.

the class TestScopeMetadataResolver method resolveScopeMetadata.

@Override
public ScopeMetadata resolveScopeMetadata(BeanDefinition beanDefinition) {
    ScopeMetadata metadata = new ScopeMetadata();
    metadata.setScopeName("myCustomScope");
    return metadata;
}
Also used : ScopeMetadata(cn.taketoday.context.loader.ScopeMetadata)

Aggregations

ScopeMetadata (cn.taketoday.context.loader.ScopeMetadata)22 AnnotatedGenericBeanDefinition (cn.taketoday.beans.factory.annotation.AnnotatedGenericBeanDefinition)16 AnnotatedBeanDefinition (cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition)14 Test (org.junit.jupiter.api.Test)14 BeanDefinitionHolder (cn.taketoday.beans.factory.config.BeanDefinitionHolder)5 BeanDefinitionStoreException (cn.taketoday.beans.factory.BeanDefinitionStoreException)4 MetadataReader (cn.taketoday.core.type.classreading.MetadataReader)4 MetadataReaderFactory (cn.taketoday.core.type.classreading.MetadataReaderFactory)4 SimpleMetadataReaderFactory (cn.taketoday.core.type.classreading.SimpleMetadataReaderFactory)4 IOException (java.io.IOException)4 AutowireCandidateQualifier (cn.taketoday.beans.factory.support.AutowireCandidateQualifier)2 Resource (cn.taketoday.core.io.Resource)2 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)1 AbstractBeanDefinition (cn.taketoday.beans.factory.support.AbstractBeanDefinition)1 LinkedHashSet (java.util.LinkedHashSet)1