Search in sources :

Example 16 with AnnotatedGenericBeanDefinition

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

the class AnnotationBeanNameGeneratorTests method generateBeanNameWithNamedComponent.

@Test
public void generateBeanNameWithNamedComponent() {
    BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
    AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentWithName.class);
    String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
    assertNotNull("The generated beanName must *never* be null.", beanName);
    assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName));
    assertEquals("walden", beanName);
}
Also used : SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.Test)

Example 17 with AnnotatedGenericBeanDefinition

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

the class AnnotationBeanNameGeneratorTests method generateBeanNameFromMetaComponentWithNonStringValue.

@Test
public void generateBeanNameFromMetaComponentWithNonStringValue() {
    BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
    AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromNonStringMeta.class);
    String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
    assertEquals("annotationBeanNameGeneratorTests.ComponentFromNonStringMeta", beanName);
}
Also used : SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.Test)

Example 18 with AnnotatedGenericBeanDefinition

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

the class AnnotationScopeMetadataResolverTests method resolveScopeMetadataShouldNotApplyScopedProxyModeToSingleton.

@Test
public void resolveScopeMetadataShouldNotApplyScopedProxyModeToSingleton() {
    AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnnotatedWithSingletonScope.class);
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
    assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata);
    assertEquals(BeanDefinition.SCOPE_SINGLETON, 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 19 with AnnotatedGenericBeanDefinition

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

the class ClassPathScanningCandidateComponentProvider method addCandidateComponentsFromIndex.

private Set<BeanDefinition> addCandidateComponentsFromIndex(String basePackage) {
    Set<BeanDefinition> candidates = new LinkedHashSet<>();
    try {
        Set<String> types = new HashSet<>();
        for (TypeFilter filter : this.includeFilters) {
            String stereotype = extractStereotype(filter);
            if (stereotype == null) {
                throw new IllegalArgumentException("Failed to extract stereotype from " + filter);
            }
            types.addAll(this.componentsIndex.getCandidateTypes(basePackage, stereotype));
        }
        boolean traceEnabled = logger.isTraceEnabled();
        boolean debugEnabled = logger.isDebugEnabled();
        for (String type : types) {
            MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(type);
            if (isCandidateComponent(metadataReader)) {
                AnnotatedGenericBeanDefinition sbd = new AnnotatedGenericBeanDefinition(metadataReader.getAnnotationMetadata());
                if (isCandidateComponent(sbd)) {
                    if (debugEnabled) {
                        logger.debug("Using candidate component class from index: " + type);
                    }
                    candidates.add(sbd);
                } else {
                    if (debugEnabled) {
                        logger.debug("Ignored because not a concrete top-level class: " + type);
                    }
                }
            } else {
                if (traceEnabled) {
                    logger.trace("Ignored because matching an exclude filter: " + type);
                }
            }
        }
    } catch (IOException ex) {
        throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
    }
    return candidates;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) MetadataReader(org.springframework.core.type.classreading.MetadataReader) TypeFilter(org.springframework.core.type.filter.TypeFilter) AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) IOException(java.io.IOException) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 20 with AnnotatedGenericBeanDefinition

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

the class ConfigurationClassBeanDefinitionReader method registerBeanDefinitionForImportedConfigurationClass.

/**
	 * Register the {@link Configuration} class itself as a bean definition.
	 */
private void registerBeanDefinitionForImportedConfigurationClass(ConfigurationClass configClass) {
    AnnotationMetadata metadata = configClass.getMetadata();
    AnnotatedGenericBeanDefinition configBeanDef = new AnnotatedGenericBeanDefinition(metadata);
    ScopeMetadata scopeMetadata = scopeMetadataResolver.resolveScopeMetadata(configBeanDef);
    configBeanDef.setScope(scopeMetadata.getScopeName());
    String configBeanName = this.importBeanNameGenerator.generateBeanName(configBeanDef, this.registry);
    AnnotationConfigUtils.processCommonDefinitionAnnotations(configBeanDef, metadata);
    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(configBeanDef, configBeanName);
    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
    this.registry.registerBeanDefinition(definitionHolder.getBeanName(), definitionHolder.getBeanDefinition());
    configClass.setBeanName(configBeanName);
    if (logger.isDebugEnabled()) {
        logger.debug("Registered bean definition for imported class '" + configBeanName + "'");
    }
}
Also used : BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata)

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