You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.7 KiB
3.7 KiB
Spring BeanNameGenerator
-
Author: HuiFer
-
源码阅读仓库: SourceHot-spring
-
org.springframework.beans.factory.support.BeanNameGenerator
-
方法用来生成 beanName
public interface BeanNameGenerator {
/**
* Generate a bean name for the given bean definition.
* 生成 beanName
* @param definition the bean definition to generate a name for
* @param registry the bean definition registry that the given definition
* is supposed to be registered with
* @return the generated bean name
*/
String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry);
}
DefaultBeanNameGenerator
-
org.springframework.beans.factory.support.DefaultBeanNameGenerator
-
调用工具类方法进行生成
@Override
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
return BeanDefinitionReaderUtils.generateBeanName(definition, registry);
}
- ClassName + # + 十六进制字符
- parentName + $child + # + 十六进制字符
- factoryBeanName +$created+# + 十六进制字符
- beanName + # + 序号
public static String generateBeanName(
BeanDefinition definition, BeanDefinitionRegistry registry, boolean isInnerBean)
throws BeanDefinitionStoreException {
// 获取 bean class 的名称
// Class.getName()
String generatedBeanName = definition.getBeanClassName();
if (generatedBeanName == null) {
// 父类名称是否存在
if (definition.getParentName() != null) {
generatedBeanName = definition.getParentName() + "$child";
}
// 工厂 beanName 是否为空
else if (definition.getFactoryBeanName() != null) {
generatedBeanName = definition.getFactoryBeanName() + "$created";
}
}
if (!StringUtils.hasText(generatedBeanName)) {
throw new BeanDefinitionStoreException("Unnamed bean definition specifies neither " +
"'class' nor 'parent' nor 'factory-bean' - can't generate bean name");
}
String id = generatedBeanName;
if (isInnerBean) {
// Inner bean: generate identity hashcode suffix.
// 组装名称
// 生成名称 + # + 16 进制的一个字符串
id = generatedBeanName + GENERATED_BEAN_NAME_SEPARATOR + ObjectUtils.getIdentityHexString(definition);
}
else {
// Top-level bean: use plain class name with unique suffix if necessary.
// 唯一beanName设置
// // beanName + # + 序号
return uniqueBeanName(generatedBeanName, registry);
}
return id;
}
AnnotationBeanNameGenerator
- 获取注解的value作为beanName
- 类名首字母小写
@Override
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
if (definition instanceof AnnotatedBeanDefinition) {
// 从注解中获取 beanName
// 获取注解的value属性值
String beanName = determineBeanNameFromAnnotation((AnnotatedBeanDefinition) definition);
if (StringUtils.hasText(beanName)) {
// Explicit bean name found.
// 如果存在直接返回
return beanName;
}
}
// Fallback: generate a unique default bean name.
// 默认beanName
// 类名,首字母小写
return buildDefaultBeanName(definition, registry);
}
FullyQualifiedAnnotationBeanNameGenerator
- 全类名
@Override
protected String buildDefaultBeanName(BeanDefinition definition) {
String beanClassName = definition.getBeanClassName();
Assert.state(beanClassName != null, "No bean class name set");
return beanClassName;
}