Spring BeanDefinitionReaderUtils

createBeanDefinition

  • org.springframework.beans.factory.support.BeanDefinitionReaderUtils.createBeanDefinition
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public static AbstractBeanDefinition createBeanDefinition(
      @Nullable String parentName, @Nullable String className, @Nullable ClassLoader classLoader) throws ClassNotFoundException {

   GenericBeanDefinition bd = new GenericBeanDefinition();
   // 设置 父bean
   bd.setParentName(parentName);
   if (className != null) {
      if (classLoader != null) {
         // 设置 class
         // 内部是通过反射创建 class
         bd.setBeanClass(ClassUtils.forName(className, classLoader));
      }
      else {
         // 设置 class name
         bd.setBeanClassName(className);
      }
   }
   return bd;
}

generateBeanName

  • org.springframework.beans.factory.support.BeanDefinitionReaderUtils.generateBeanName(org.springframework.beans.factory.config.BeanDefinition, org.springframework.beans.factory.support.BeanDefinitionRegistry, boolean)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
	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;
	}

uniqueBeanName

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public static String uniqueBeanName(String beanName, BeanDefinitionRegistry registry) {
   String id = beanName;
   int counter = -1;

   // Increase counter until the id is unique.
   while (counter == -1 || registry.containsBeanDefinition(id)) {
      counter++;
      // beanName + # + 序号
      id = beanName + GENERATED_BEAN_NAME_SEPARATOR + counter;
   }
   return id;
}

registerBeanDefinition

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public static void registerBeanDefinition(
      BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
      throws BeanDefinitionStoreException {

   // Register bean definition under primary name.
   // 获取 beanName
   String beanName = definitionHolder.getBeanName();
   // 注册bean definition
   registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

   // Register aliases for bean name, if any.
   // 别名列表
   String[] aliases = definitionHolder.getAliases();
   // 注册别名列表
   if (aliases != null) {
      for (String alias : aliases) {
         registry.registerAlias(beanName, alias);
      }
   }
}

registerWithGeneratedName

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public static String registerWithGeneratedName(
      AbstractBeanDefinition definition, BeanDefinitionRegistry registry)
      throws BeanDefinitionStoreException {

   // 生成一个 beanName
   String generatedName = generateBeanName(definition, registry, false);
   // 注册 bean Definition
   registry.registerBeanDefinition(generatedName, definition);
   return generatedName;
}