Search in sources :

Example 1 with AnnotatedGenericBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition in project spring-framework by spring-projects.

the class AnnotatedBeanDefinitionReader method doRegisterBean.

/**
	 * Register a bean from the given bean class, deriving its metadata from
	 * class-declared annotations.
	 * @param annotatedClass the class of the bean
	 * @param instanceSupplier a callback for creating an instance of the bean
	 * (may be {@code null})
	 * @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 definitionCustomizers one or more callbacks for customizing the
	 * factory's {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
	 * @since 5.0
	 */
<T> void doRegisterBean(Class<T> annotatedClass, Supplier<T> instanceSupplier, String name, Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {
    AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
    if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
        return;
    }
    abd.setInstanceSupplier(instanceSupplier);
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
    abd.setScope(scopeMetadata.getScopeName());
    String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
    AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
    if (qualifiers != null) {
        for (Class<? extends Annotation> qualifier : qualifiers) {
            if (Primary.class == qualifier) {
                abd.setPrimary(true);
            } else if (Lazy.class == qualifier) {
                abd.setLazyInit(true);
            } else {
                abd.addQualifier(new AutowireCandidateQualifier(qualifier));
            }
        }
    }
    if (definitionCustomizers != null) {
        for (BeanDefinitionCustomizer customizer : definitionCustomizers) {
            customizer.customize(abd);
        }
    }
    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
    BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
Also used : BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanDefinitionCustomizer(org.springframework.beans.factory.config.BeanDefinitionCustomizer) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier)

Example 2 with AnnotatedGenericBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition in project spring-boot by spring-projects.

the class AbstractDevToolsDataSourceAutoConfigurationTests method emptyFactoryMethodMetadataIgnored.

@Test
public void emptyFactoryMethodMetadataIgnored() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    DataSource dataSource = mock(DataSource.class);
    AnnotatedGenericBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(dataSource.getClass());
    context.registerBeanDefinition("dataSource", beanDefinition);
    context.register(DataSourcePropertiesConfiguration.class);
    context.register(DevToolsDataSourceAutoConfiguration.class);
    context.refresh();
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 3 with AnnotatedGenericBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition in project spring-framework by spring-projects.

the class AnnotationScopeMetadataResolverTests method customRequestScope.

@Test
public void customRequestScope() {
    AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnnotatedWithCustomRequestScope.class);
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
    assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata);
    assertEquals("request", scopeMetadata.getScopeName());
    assertEquals(NO, scopeMetadata.getScopedProxyMode());
}
Also used : AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.Test)

Example 4 with AnnotatedGenericBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition in project spring-framework by spring-projects.

the class AnnotationScopeMetadataResolverTests method customRequestScopeViaAsm.

@Test
public void customRequestScopeViaAsm() throws IOException {
    MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory();
    MetadataReader reader = readerFactory.getMetadataReader(AnnotatedWithCustomRequestScope.class.getName());
    AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(reader.getAnnotationMetadata());
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
    assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata);
    assertEquals("request", scopeMetadata.getScopeName());
    assertEquals(NO, scopeMetadata.getScopedProxyMode());
}
Also used : MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) MetadataReader(org.springframework.core.type.classreading.MetadataReader) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.Test)

Example 5 with AnnotatedGenericBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition in project spring-framework by spring-projects.

the class AnnotationScopeMetadataResolverTests method customRequestScopeWithAttributeViaAsm.

@Test
public void customRequestScopeWithAttributeViaAsm() throws IOException {
    MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory();
    MetadataReader reader = readerFactory.getMetadataReader(AnnotatedWithCustomRequestScopeWithAttributeOverride.class.getName());
    AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(reader.getAnnotationMetadata());
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
    assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata);
    assertEquals("request", scopeMetadata.getScopeName());
    assertEquals(TARGET_CLASS, scopeMetadata.getScopedProxyMode());
}
Also used : MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) MetadataReader(org.springframework.core.type.classreading.MetadataReader) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.Test)

Aggregations

AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)20 Test (org.junit.Test)17 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)17 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)9 SimpleBeanDefinitionRegistry (org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry)9 MetadataReader (org.springframework.core.type.classreading.MetadataReader)3 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)2 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)2 SimpleMetadataReaderFactory (org.springframework.core.type.classreading.SimpleMetadataReaderFactory)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 DataSource (javax.sql.DataSource)1 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 BeanDefinitionCustomizer (org.springframework.beans.factory.config.BeanDefinitionCustomizer)1 AutowireCandidateQualifier (org.springframework.beans.factory.support.AutowireCandidateQualifier)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 AnnotationMetadata (org.springframework.core.type.AnnotationMetadata)1 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)1