Search in sources :

Example 1 with ScopeMetadata

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

the class AnnotationScopeMetadataResolverTests method resolveScopeMetadataShouldApplyScopedProxyModeToPrototype.

@Test
public void resolveScopeMetadataShouldApplyScopedProxyModeToPrototype() {
    this.scopeMetadataResolver = new AnnotationScopeMetadataResolver(INTERFACES);
    AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnnotatedWithPrototypeScope.class);
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
    Assertions.assertThat(scopeMetadata).as("resolveScopeMetadata(..) must *never* return null.").isNotNull();
    Assertions.assertThat(scopeMetadata.getScopeName()).isEqualTo(BeanDefinition.SCOPE_PROTOTYPE);
    Assertions.assertThat(scopeMetadata.getScopedProxyMode()).isEqualTo(INTERFACES);
}
Also used : AnnotatedBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition) ScopeMetadata(cn.taketoday.context.loader.ScopeMetadata) AnnotatedGenericBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 2 with ScopeMetadata

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

the class AnnotationScopeMetadataResolverTests method customRequestScope.

@Test
public void customRequestScope() {
    AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnnotatedWithCustomRequestScope.class);
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
    Assertions.assertThat(scopeMetadata).as("resolveScopeMetadata(..) must *never* return null.").isNotNull();
    Assertions.assertThat(scopeMetadata.getScopeName()).isEqualTo("request");
    Assertions.assertThat(scopeMetadata.getScopedProxyMode()).isEqualTo(NO);
}
Also used : AnnotatedBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition) ScopeMetadata(cn.taketoday.context.loader.ScopeMetadata) AnnotatedGenericBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with ScopeMetadata

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

the class AnnotationScopeMetadataResolverTests method customRequestScopeWithAttribute.

@Test
public void customRequestScopeWithAttribute() {
    AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnnotatedWithCustomRequestScopeWithAttributeOverride.class);
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
    Assertions.assertThat(scopeMetadata).as("resolveScopeMetadata(..) must *never* return null.").isNotNull();
    Assertions.assertThat(scopeMetadata.getScopeName()).isEqualTo("request");
    Assertions.assertThat(scopeMetadata.getScopedProxyMode()).isEqualTo(TARGET_CLASS);
}
Also used : AnnotatedBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition) ScopeMetadata(cn.taketoday.context.loader.ScopeMetadata) AnnotatedGenericBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 4 with ScopeMetadata

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

the class AnnotationScopeMetadataResolverTests method resolveScopeMetadataShouldNotApplyScopedProxyModeToSingleton.

@Test
public void resolveScopeMetadataShouldNotApplyScopedProxyModeToSingleton() {
    AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnnotatedWithSingletonScope.class);
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
    Assertions.assertThat(scopeMetadata).as("resolveScopeMetadata(..) must *never* return null.").isNotNull();
    Assertions.assertThat(scopeMetadata.getScopeName()).isEqualTo(BeanDefinition.SCOPE_SINGLETON);
    Assertions.assertThat(scopeMetadata.getScopedProxyMode()).isEqualTo(NO);
}
Also used : AnnotatedBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition) ScopeMetadata(cn.taketoday.context.loader.ScopeMetadata) AnnotatedGenericBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 5 with ScopeMetadata

use of cn.taketoday.context.loader.ScopeMetadata in project today-infrastructure 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)

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