Skip to content

Commit c366e20

Browse files
committed
Common constants for default AnnotationBeanNameGenerator instances
Closes gh-22591
1 parent b2dbefc commit c366e20

File tree

5 files changed

+46
-20
lines changed

5 files changed

+46
-20
lines changed

spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,7 +49,7 @@ public class AnnotatedBeanDefinitionReader {
4949

5050
private final BeanDefinitionRegistry registry;
5151

52-
private BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
52+
private BeanNameGenerator beanNameGenerator = AnnotationBeanNameGenerator.INSTANCE;
5353

5454
private ScopeMetadataResolver scopeMetadataResolver = new AnnotationScopeMetadataResolver();
5555

@@ -110,7 +110,8 @@ public void setEnvironment(Environment environment) {
110110
* <p>The default is a {@link AnnotationBeanNameGenerator}.
111111
*/
112112
public void setBeanNameGenerator(@Nullable BeanNameGenerator beanNameGenerator) {
113-
this.beanNameGenerator = (beanNameGenerator != null ? beanNameGenerator : new AnnotationBeanNameGenerator());
113+
this.beanNameGenerator =
114+
(beanNameGenerator != null ? beanNameGenerator : AnnotationBeanNameGenerator.INSTANCE);
114115
}
115116

116117
/**

spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -63,6 +63,13 @@
6363
*/
6464
public class AnnotationBeanNameGenerator implements BeanNameGenerator {
6565

66+
/**
67+
* A convenient constant for a default {@code AnnotationBeanNameGenerator} instance,
68+
* as used for component scanning purposes.
69+
* @since 5.2
70+
*/
71+
public static final AnnotationBeanNameGenerator INSTANCE = new AnnotationBeanNameGenerator();
72+
6673
private static final String COMPONENT_ANNOTATION_CLASSNAME = "org.springframework.stereotype.Component";
6774

6875

spring-context/src/main/java/org/springframework/context/annotation/ClassPathBeanDefinitionScanner.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,7 +69,7 @@ public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateCo
6969
@Nullable
7070
private String[] autowireCandidatePatterns;
7171

72-
private BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
72+
private BeanNameGenerator beanNameGenerator = AnnotationBeanNameGenerator.INSTANCE;
7373

7474
private ScopeMetadataResolver scopeMetadataResolver = new AnnotationScopeMetadataResolver();
7575

@@ -208,7 +208,8 @@ public void setAutowireCandidatePatterns(@Nullable String... autowireCandidatePa
208208
* <p>Default is a {@link AnnotationBeanNameGenerator}.
209209
*/
210210
public void setBeanNameGenerator(@Nullable BeanNameGenerator beanNameGenerator) {
211-
this.beanNameGenerator = (beanNameGenerator != null ? beanNameGenerator : new AnnotationBeanNameGenerator());
211+
this.beanNameGenerator =
212+
(beanNameGenerator != null ? beanNameGenerator : AnnotationBeanNameGenerator.INSTANCE);
212213
}
213214

214215
/**

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java

+22-11
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,24 @@
8686
public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor,
8787
PriorityOrdered, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware {
8888

89+
/**
90+
* A {@code BeanNameGenerator} using fully qualified class names as default bean names.
91+
* <p>This default for configuration-level import purposes may be overridden through
92+
* {@link #setBeanNameGenerator}. Note that the default for component scanning purposes
93+
* is a plain {@link AnnotationBeanNameGenerator#INSTANCE}, unless overridden through
94+
* {@link #setBeanNameGenerator} with a unified user-level bean name generator.
95+
* @since 5.2
96+
* @see #setBeanNameGenerator
97+
*/
98+
public static final AnnotationBeanNameGenerator IMPORT_BEAN_NAME_GENERATOR = new AnnotationBeanNameGenerator() {
99+
@Override
100+
protected String buildDefaultBeanName(BeanDefinition definition) {
101+
String beanClassName = definition.getBeanClassName();
102+
Assert.state(beanClassName != null, "No bean class name set");
103+
return beanClassName;
104+
}
105+
};
106+
89107
private static final String IMPORT_REGISTRY_BEAN_NAME =
90108
ConfigurationClassPostProcessor.class.getName() + ".importRegistry";
91109

@@ -117,18 +135,11 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
117135

118136
private boolean localBeanNameGeneratorSet = false;
119137

120-
/* Using short class names as default bean names */
121-
private BeanNameGenerator componentScanBeanNameGenerator = new AnnotationBeanNameGenerator();
138+
/* Using short class names as default bean names by default. */
139+
private BeanNameGenerator componentScanBeanNameGenerator = AnnotationBeanNameGenerator.INSTANCE;
122140

123-
/* Using fully qualified class names as default bean names */
124-
private BeanNameGenerator importBeanNameGenerator = new AnnotationBeanNameGenerator() {
125-
@Override
126-
protected String buildDefaultBeanName(BeanDefinition definition) {
127-
String beanClassName = definition.getBeanClassName();
128-
Assert.state(beanClassName != null, "No bean class name set");
129-
return beanClassName;
130-
}
131-
};
141+
/* Using fully qualified class names as default bean names by default. */
142+
private BeanNameGenerator importBeanNameGenerator = IMPORT_BEAN_NAME_GENERATOR;
132143

133144

134145
@Override

spring-context/src/main/java/org/springframework/context/annotation/ImportBeanDefinitionRegistrar.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,15 @@ public interface ImportBeanDefinitionRegistrar {
6161
* {@link #registerBeanDefinitions(AnnotationMetadata, BeanDefinitionRegistry)}.
6262
* @param importingClassMetadata annotation metadata of the importing class
6363
* @param registry current bean definition registry
64-
* @param importBeanNameGenerator the configuration-level bean name generator
65-
* strategy for imported beans
64+
* @param importBeanNameGenerator the bean name generator strategy for imported beans:
65+
* {@link ConfigurationClassPostProcessor#IMPORT_BEAN_NAME_GENERATOR} by default, or a
66+
* user-provided one if {@link ConfigurationClassPostProcessor#setBeanNameGenerator}
67+
* has been set. In the latter case, the passed-in strategy will be the same used for
68+
* component scanning in the containing application context (otherwise, the default
69+
* component-scan naming strategy is {@link AnnotationBeanNameGenerator#INSTANCE}).
6670
* @since 5.2
71+
* @see ConfigurationClassPostProcessor#IMPORT_BEAN_NAME_GENERATOR
72+
* @see ConfigurationClassPostProcessor#setBeanNameGenerator
6773
*/
6874
default void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,
6975
BeanNameGenerator importBeanNameGenerator) {

0 commit comments

Comments
 (0)