Top Description Fields Constructors Methods
org.springframework.context.annotation

public Class AnnotationBeanNameGenerator

extends Object
implements BeanNameGenerator
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.support.BeanNameGenerator
Known Direct Subclasses
org.springframework.context.annotation.FullyQualifiedAnnotationBeanNameGenerator
Imports
java.lang.annotation.Annotation, java.util.Collections, .HashSet, .LinkedHashSet, .List, .Map, .Set, java.util.concurrent.ConcurrentHashMap, java.util.stream.Collectors, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.factory.annotation.AnnotatedBeanDefinition, org.springframework.beans.factory.config.BeanDefinition, org.springframework.beans.factory.support.BeanDefinitionRegistry, .BeanNameGenerator, org.springframework.core.annotation.AnnotationAttributes, .MergedAnnotation, .MergedAnnotation.Adapt, .MergedAnnotations, org.springframework.core.type.AnnotationMetadata, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .StringUtils

BeanNameGenerator implementation for bean classes annotated with the @Component annotation or with another annotation that is itself annotated with @Component as a meta-annotation. For example, Spring's stereotype annotations (such as @Repository) are themselves annotated with @Component.

Also supports Jakarta EE's jakarta.annotation.ManagedBean and JSR-330's jakarta.inject.Named annotations (as well as their pre-Jakarta javax.annotation.ManagedBean and javax.inject.Named equivalents), if available. Note that Spring component annotations always override such standard annotations.

If the annotation's value doesn't indicate a bean name, an appropriate name will be built based on the short name of the class (with the first letter lower-cased), unless the first two letters are uppercase. For example:

com.xyz.FooServiceImpl -> fooServiceImpl
com.xyz.URLFooServiceImpl -> URLFooServiceImpl
Authors
Juergen Hoeller, Mark Fisher, Sam Brannen
Since
2.5
See Also
org.springframework.stereotype.Component#value(), org.springframework.stereotype.Repository#value(), org.springframework.stereotype.Service#value(), org.springframework.stereotype.Controller#value(), jakarta.inject.Named#value(), FullyQualifiedAnnotationBeanNameGenerator

Field Summary

Modifier and TypeField and Description
private static final MergedAnnotation.Adapt[]
private static final String
private static final Set<String>
conventionBasedStereotypeCheckCache

Set used to track which stereotype annotations have already been checked to see if they use a convention-based override for the value attribute in @Component.

public static final AnnotationBeanNameGenerator
INSTANCE

A convenient constant for a default AnnotationBeanNameGenerator instance, as used for component scanning purposes.

private static final Log
private final Map<String, Set<String>>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected String

Returns:

the default bean name (never null)
buildDefaultBeanName
(BeanDefinition
the bean definition to build a bean name for
definition
,
BeanDefinitionRegistry
the registry that the given bean definition is being registered with
registry
)

Derive a default bean name from the given bean definition.

protected String

Returns:

the default bean name (never null)
buildDefaultBeanName
(BeanDefinition
the bean definition to build a bean name for
definition
)

Derive a default bean name from the given bean definition.

protected String

Returns:

the bean name, or null if none is found
determineBeanNameFromAnnotation
(AnnotatedBeanDefinition
the annotation-aware bean definition
annotatedDef
)

Derive a bean name from one of the annotations on the class.

public String
generateBeanName(BeanDefinition
the bean definition to generate a name for
definition
,
BeanDefinitionRegistry
the bean definition registry that the given definition is supposed to be registered with
registry
)

Implements org.springframework.beans.factory.support.BeanNameGenerator.generateBeanName.

Generate a bean name for the given bean definition.
private String

Returns:

the explicit bean name, or null if not found
getExplicitBeanName
(AnnotationMetadata
the AnnotationMetadata for the underlying class
metadata
)

Get the explicit bean name for the underlying class, as configured via @Component and taking into account @AliasFor semantics for annotation attribute overrides for @Component's value attribute.

private Set<String>
protected boolean

Returns:

whether the annotation qualifies as a stereotype with component name
isStereotypeWithNameValue
(String
the name of the annotation class to check
annotationType
,
Set<String>
the names of meta-annotations on the given annotation
metaAnnotationTypes
,
Map<String, Object>
the map of attributes for the given annotation
attributes
)

Check whether the given annotation is a stereotype that is allowed to suggest a component name through its value() attribute.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ADAPTATIONSback to summary
private static final MergedAnnotation.Adapt[] ADAPTATIONS
COMPONENT_ANNOTATION_CLASSNAMEback to summary
private static final String COMPONENT_ANNOTATION_CLASSNAME
conventionBasedStereotypeCheckCacheback to summary
private static final Set<String> conventionBasedStereotypeCheckCache

Set used to track which stereotype annotations have already been checked to see if they use a convention-based override for the value attribute in @Component.

Since
6.1
See Also
determineBeanNameFromAnnotation(AnnotatedBeanDefinition)
INSTANCEback to summary
public static final AnnotationBeanNameGenerator INSTANCE

A convenient constant for a default AnnotationBeanNameGenerator instance, as used for component scanning purposes.

Since
5.2
loggerback to summary
private static final Log logger
metaAnnotationTypesCacheback to summary
private final Map<String, Set<String>> metaAnnotationTypesCache

Constructor Detail

AnnotationBeanNameGeneratorback to summary
public AnnotationBeanNameGenerator()

Method Detail

buildDefaultBeanNameback to summary
protected String buildDefaultBeanName(BeanDefinition definition, BeanDefinitionRegistry registry)

Derive a default bean name from the given bean definition.

The default implementation delegates to buildDefaultBeanName(BeanDefinition).

Parameters
definition:BeanDefinition

the bean definition to build a bean name for

registry:BeanDefinitionRegistry

the registry that the given bean definition is being registered with

Returns:String

the default bean name (never null)

buildDefaultBeanNameback to summary
protected String buildDefaultBeanName(BeanDefinition definition)

Derive a default bean name from the given bean definition.

The default implementation simply builds a decapitalized version of the short class name: e.g. "mypackage.MyJdbcDao" → "myJdbcDao".

Note that inner classes will thus have names of the form "outerClassName.InnerClassName", which because of the period in the name may be an issue if you are autowiring by name.

Parameters
definition:BeanDefinition

the bean definition to build a bean name for

Returns:String

the default bean name (never null)

determineBeanNameFromAnnotationback to summary
protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef)

Derive a bean name from one of the annotations on the class.

Parameters
annotatedDef:AnnotatedBeanDefinition

the annotation-aware bean definition

Returns:String

the bean name, or null if none is found

Annotations
@Nullable
generateBeanNameback to summary
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry)

Implements org.springframework.beans.factory.support.BeanNameGenerator.generateBeanName.

Doc from org.springframework.beans.factory.support.BeanNameGenerator.generateBeanName.

Generate a bean name for the given bean definition.

Parameters
definition:BeanDefinition

the bean definition to generate a name for

registry:BeanDefinitionRegistry

the bean definition registry that the given definition is supposed to be registered with

Returns:String

the generated bean name

Annotations
@Override
getExplicitBeanNameback to summary
private String getExplicitBeanName(AnnotationMetadata metadata)

Get the explicit bean name for the underlying class, as configured via @Component and taking into account @AliasFor semantics for annotation attribute overrides for @Component's value attribute.

Parameters
metadata:AnnotationMetadata

the AnnotationMetadata for the underlying class

Returns:String

the explicit bean name, or null if not found

Annotations
@Nullable
Since
6.1
See Also
org.springframework.stereotype.Component#value()
getMetaAnnotationTypesback to summary
private Set<String> getMetaAnnotationTypes(MergedAnnotation<Annotation> mergedAnnotation)
isStereotypeWithNameValueback to summary
protected boolean isStereotypeWithNameValue(String annotationType, Set<String> metaAnnotationTypes, Map<String, Object> attributes)

Check whether the given annotation is a stereotype that is allowed to suggest a component name through its value() attribute.

Parameters
annotationType:String

the name of the annotation class to check

metaAnnotationTypes:Set<String>

the names of meta-annotations on the given annotation

attributes:Map<String, Object>

the map of attributes for the given annotation

Returns:boolean

whether the annotation qualifies as a stereotype with component name