Common constants for default AnnotationBeanNameGenerator instances
Closes gh-22591
This commit is contained in:
parent
b2dbefcfc5
commit
c366e205e5
|
@ -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 {
|
|||
* <p>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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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";
|
||||
|
||||
|
||||
|
|
|
@ -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
|
|||
* <p>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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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.
|
||||
* <p>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
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue