Class AnnotationBeanNameGenerator
- All Implemented Interfaces:
org.springframework.beans.factory.support.BeanNameGenerator
- Direct Known Subclasses:
FullyQualifiedAnnotationBeanNameGenerator
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 ManagedBean
and
JSR-330's Named
annotations, 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
- Since:
- 2.5
- Author:
- Juergen Hoeller, Mark Fisher
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final AnnotationBeanNameGenerator
A convenient constant for a defaultAnnotationBeanNameGenerator
instance, as used for component scanning purposes. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected String
buildDefaultBeanName
(org.springframework.beans.factory.config.BeanDefinition definition) Derive a default bean name from the given bean definition.protected String
buildDefaultBeanName
(org.springframework.beans.factory.config.BeanDefinition definition, org.springframework.beans.factory.support.BeanDefinitionRegistry registry) Derive a default bean name from the given bean definition.protected String
determineBeanNameFromAnnotation
(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition annotatedDef) Derive a bean name from one of the annotations on the class.generateBeanName
(org.springframework.beans.factory.config.BeanDefinition definition, org.springframework.beans.factory.support.BeanDefinitionRegistry registry) 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 annotationvalue()
.
-
Field Details
-
INSTANCE
A convenient constant for a defaultAnnotationBeanNameGenerator
instance, as used for component scanning purposes.- Since:
- 5.2
-
-
Constructor Details
-
AnnotationBeanNameGenerator
public AnnotationBeanNameGenerator()
-
-
Method Details
-
generateBeanName
public String generateBeanName(org.springframework.beans.factory.config.BeanDefinition definition, org.springframework.beans.factory.support.BeanDefinitionRegistry registry) - Specified by:
generateBeanName
in interfaceorg.springframework.beans.factory.support.BeanNameGenerator
-
determineBeanNameFromAnnotation
@Nullable protected String determineBeanNameFromAnnotation(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition annotatedDef) Derive a bean name from one of the annotations on the class.- Parameters:
annotatedDef
- the annotation-aware bean definition- Returns:
- the bean name, or
null
if none is found
-
isStereotypeWithNameValue
protected boolean isStereotypeWithNameValue(String annotationType, Set<String> metaAnnotationTypes, @Nullable Map<String, Object> attributes) Check whether the given annotation is a stereotype that is allowed to suggest a component name through its annotationvalue()
.- Parameters:
annotationType
- the name of the annotation class to checkmetaAnnotationTypes
- the names of meta-annotations on the given annotationattributes
- the map of attributes for the given annotation- Returns:
- whether the annotation qualifies as a stereotype with component name
-
buildDefaultBeanName
protected String buildDefaultBeanName(org.springframework.beans.factory.config.BeanDefinition definition, org.springframework.beans.factory.support.BeanDefinitionRegistry registry) Derive a default bean name from the given bean definition.The default implementation delegates to
buildDefaultBeanName(BeanDefinition)
.- Parameters:
definition
- the bean definition to build a bean name forregistry
- the registry that the given bean definition is being registered with- Returns:
- the default bean name (never
null
)
-
buildDefaultBeanName
protected String buildDefaultBeanName(org.springframework.beans.factory.config.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
- the bean definition to build a bean name for- Returns:
- the default bean name (never
null
)
-