MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(type); if (isCandidateComponent(metadataReader)) { AnnotatedGenericBeanDefinition sbd = new AnnotatedGenericBeanDefinition( metadataReader.getAnnotationMetadata()); if (isCandidateComponent(sbd)) {
/** * 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.isTraceEnabled()) { logger.trace("Registered bean definition for imported class '" + configBeanName + "'"); } }
/** * 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.isTraceEnabled()) { logger.trace("Registered bean definition for imported class '" + configBeanName + "'"); } }
@Test public void generateBeanNameFromMetaComponentWithStringValue() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromStringMeta.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertEquals("henry", beanName); }
/** * @since 4.0.1 * @see https://jira.spring.io/browse/SPR-11360 */ @Test public void generateBeanNameFromComposedControllerAnnotationWithStringValue() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition( ComposedControllerAnnotationWithStringValue.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertEquals("restController", beanName); }
@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); }
@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); }
@Test public void generateBeanNameWithDefaultNamedComponent() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(DefaultNamedComponent.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("thoreau", beanName); }
@Test public void generateBeanNameWithAnonymousComponentYieldsGeneratedBeanName() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnonymousComponent.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)); String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd); assertEquals(expectedGeneratedBeanName, beanName); }
@Test public void generateBeanNameWithNamedComponentWhereTheNameIsBlank() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentWithBlankName.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)); String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd); assertEquals(expectedGeneratedBeanName, beanName); }
/** * @since 4.0.1 * @see https://jira.spring.io/browse/SPR-11360 */ @Test public void generateBeanNameFromComposedControllerAnnotationWithoutName() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComposedControllerAnnotationWithoutName.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd); assertEquals(expectedGeneratedBeanName, beanName); }
/** * @since 4.0.1 * @see https://jira.spring.io/browse/SPR-11360 */ @Test public void generateBeanNameFromComposedControllerAnnotationWithBlankName() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComposedControllerAnnotationWithBlankName.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd); assertEquals(expectedGeneratedBeanName, beanName); }
@Nullable Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) { AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass); if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) { return;
@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()); }
@Test public void resolveScopeMetadataShouldReadScopedProxyModeFromAnnotation() { AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnnotatedWithScopedProxy.class); ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd); assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata); assertEquals("request", scopeMetadata.getScopeName()); assertEquals(TARGET_CLASS, scopeMetadata.getScopedProxyMode()); }
@Test public void customRequestScopeWithAttribute() { AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition( AnnotatedWithCustomRequestScopeWithAttributeOverride.class); ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd); assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata); assertEquals("request", scopeMetadata.getScopeName()); assertEquals(TARGET_CLASS, scopeMetadata.getScopedProxyMode()); }
@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()); }
@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()); }
@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()); }
@Test public void resolveScopeMetadataShouldApplyScopedProxyModeToPrototype() { this.scopeMetadataResolver = new AnnotationScopeMetadataResolver(INTERFACES); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnnotatedWithPrototypeScope.class); ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd); assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata); assertEquals(BeanDefinition.SCOPE_PROTOTYPE, scopeMetadata.getScopeName()); assertEquals(INTERFACES, scopeMetadata.getScopedProxyMode()); }