From c366e205e5b9f4834eb8e4420705c183e7282ac4 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 2 Apr 2019 16:52:36 +0200 Subject: [PATCH] Common constants for default AnnotationBeanNameGenerator instances Closes gh-22591 --- .../AnnotatedBeanDefinitionReader.java | 7 ++-- .../AnnotationBeanNameGenerator.java | 9 ++++- .../ClassPathBeanDefinitionScanner.java | 7 ++-- .../ConfigurationClassPostProcessor.java | 33 ++++++++++++------- .../ImportBeanDefinitionRegistrar.java | 10 ++++-- 5 files changed, 46 insertions(+), 20 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java b/spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java index b5e2825dd7..5bd012a730 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,7 +49,7 @@ public class AnnotatedBeanDefinitionReader { private final BeanDefinitionRegistry registry; - private BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator(); + private BeanNameGenerator beanNameGenerator = AnnotationBeanNameGenerator.INSTANCE; private ScopeMetadataResolver scopeMetadataResolver = new AnnotationScopeMetadataResolver(); @@ -110,7 +110,8 @@ public class AnnotatedBeanDefinitionReader { *

The default is a {@link AnnotationBeanNameGenerator}. */ public void setBeanNameGenerator(@Nullable BeanNameGenerator beanNameGenerator) { - this.beanNameGenerator = (beanNameGenerator != null ? beanNameGenerator : new AnnotationBeanNameGenerator()); + this.beanNameGenerator = + (beanNameGenerator != null ? beanNameGenerator : AnnotationBeanNameGenerator.INSTANCE); } /** diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java index bf9c507995..29ad5fce07 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,6 +63,13 @@ import org.springframework.util.StringUtils; */ public class AnnotationBeanNameGenerator implements BeanNameGenerator { + /** + * A convenient constant for a default {@code AnnotationBeanNameGenerator} instance, + * as used for component scanning purposes. + * @since 5.2 + */ + public static final AnnotationBeanNameGenerator INSTANCE = new AnnotationBeanNameGenerator(); + private static final String COMPONENT_ANNOTATION_CLASSNAME = "org.springframework.stereotype.Component"; diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ClassPathBeanDefinitionScanner.java b/spring-context/src/main/java/org/springframework/context/annotation/ClassPathBeanDefinitionScanner.java index af6ad5dac3..65cbb9bdb9 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ClassPathBeanDefinitionScanner.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ClassPathBeanDefinitionScanner.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -69,7 +69,7 @@ public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateCo @Nullable private String[] autowireCandidatePatterns; - private BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator(); + private BeanNameGenerator beanNameGenerator = AnnotationBeanNameGenerator.INSTANCE; private ScopeMetadataResolver scopeMetadataResolver = new AnnotationScopeMetadataResolver(); @@ -208,7 +208,8 @@ public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateCo *

Default is a {@link AnnotationBeanNameGenerator}. */ public void setBeanNameGenerator(@Nullable BeanNameGenerator beanNameGenerator) { - this.beanNameGenerator = (beanNameGenerator != null ? beanNameGenerator : new AnnotationBeanNameGenerator()); + this.beanNameGenerator = + (beanNameGenerator != null ? beanNameGenerator : AnnotationBeanNameGenerator.INSTANCE); } /** diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java index 732ff97792..a078a71649 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java @@ -86,6 +86,24 @@ import org.springframework.util.ClassUtils; public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor, PriorityOrdered, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware { + /** + * A {@code BeanNameGenerator} using fully qualified class names as default bean names. + *

This default for configuration-level import purposes may be overridden through + * {@link #setBeanNameGenerator}. Note that the default for component scanning purposes + * is a plain {@link AnnotationBeanNameGenerator#INSTANCE}, unless overridden through + * {@link #setBeanNameGenerator} with a unified user-level bean name generator. + * @since 5.2 + * @see #setBeanNameGenerator + */ + public static final AnnotationBeanNameGenerator IMPORT_BEAN_NAME_GENERATOR = new AnnotationBeanNameGenerator() { + @Override + protected String buildDefaultBeanName(BeanDefinition definition) { + String beanClassName = definition.getBeanClassName(); + Assert.state(beanClassName != null, "No bean class name set"); + return beanClassName; + } + }; + private static final String IMPORT_REGISTRY_BEAN_NAME = ConfigurationClassPostProcessor.class.getName() + ".importRegistry"; @@ -117,18 +135,11 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo private boolean localBeanNameGeneratorSet = false; - /* Using short class names as default bean names */ - private BeanNameGenerator componentScanBeanNameGenerator = new AnnotationBeanNameGenerator(); + /* Using short class names as default bean names by default. */ + private BeanNameGenerator componentScanBeanNameGenerator = AnnotationBeanNameGenerator.INSTANCE; - /* Using fully qualified class names as default bean names */ - private BeanNameGenerator importBeanNameGenerator = new AnnotationBeanNameGenerator() { - @Override - protected String buildDefaultBeanName(BeanDefinition definition) { - String beanClassName = definition.getBeanClassName(); - Assert.state(beanClassName != null, "No bean class name set"); - return beanClassName; - } - }; + /* Using fully qualified class names as default bean names by default. */ + private BeanNameGenerator importBeanNameGenerator = IMPORT_BEAN_NAME_GENERATOR; @Override diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ImportBeanDefinitionRegistrar.java b/spring-context/src/main/java/org/springframework/context/annotation/ImportBeanDefinitionRegistrar.java index b609f2bd6c..a453e4a7ac 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ImportBeanDefinitionRegistrar.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ImportBeanDefinitionRegistrar.java @@ -61,9 +61,15 @@ public interface ImportBeanDefinitionRegistrar { * {@link #registerBeanDefinitions(AnnotationMetadata, BeanDefinitionRegistry)}. * @param importingClassMetadata annotation metadata of the importing class * @param registry current bean definition registry - * @param importBeanNameGenerator the configuration-level bean name generator - * strategy for imported beans + * @param importBeanNameGenerator the bean name generator strategy for imported beans: + * {@link ConfigurationClassPostProcessor#IMPORT_BEAN_NAME_GENERATOR} by default, or a + * user-provided one if {@link ConfigurationClassPostProcessor#setBeanNameGenerator} + * has been set. In the latter case, the passed-in strategy will be the same used for + * component scanning in the containing application context (otherwise, the default + * component-scan naming strategy is {@link AnnotationBeanNameGenerator#INSTANCE}). * @since 5.2 + * @see ConfigurationClassPostProcessor#IMPORT_BEAN_NAME_GENERATOR + * @see ConfigurationClassPostProcessor#setBeanNameGenerator */ default void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {