辛星解析Spring3.2源码第二篇:BeanDefinitionReaderUtils

时间:2023-01-21 00:14:07

   通过它的后缀名utils可以看出,这是一个工具类,所提供的方法也都是静态方法,下面列举一下它的几个方法:

    -->  createBeanDefinition 即创建一个Bean定义

    -->  generateBeanName 即生成一个Bean名称,这里会根据是否是内部Bean来区别对待,如果是内部Bean的话,那么在井号后面带的是哈希值,如果是外部bean的话,那么是递增的序号

   --> generateBeanName 是重载的方法,它不是内部Bean

   --> registerBeanDefinition 即注册Bean定义,它这里需要Bean定义和注册器

   --> registerWithGeneratedName即通过生成的名称来注册Bean

  其实代码部分到不难理解,不过我到是有点好奇,为什么会有这样一个util,它似乎这个方法和Bean注册器的关联度更强一些。下面是源码内容(带中文注释):

/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.beans.factory.support;

import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
* Utility methods that are useful for bean definition reader implementations.
* Mainly intended for internal use.
*
* Bean定义阅读器助手
* 它主要是被内部使用
*
* @author Juergen Hoeller
* @author Rob Harrop
* @since 1.1
* @see PropertiesBeanDefinitionReader
* @see org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader
*/
public class BeanDefinitionReaderUtils {

/**
* Separator for generated bean names. If a class name or parent name is not
* unique, "#1", "#2" etc will be appended, until the name becomes unique.
*
* Bean名称生成时的分隔符
* 默认为井号,即"#"
*/
public static final String GENERATED_BEAN_NAME_SEPARATOR = BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR;


/**
* Create a new GenericBeanDefinition for the given parent name and class name,
* eagerly loading the bean class if a ClassLoader has been specified.
*
* 创建一个Bean定义
*
* @param parentName the name of the parent bean, if any
* @param className the name of the bean class, if any
* @param classLoader the ClassLoader to use for loading bean classes
* (can be {@code null} to just register bean classes by name)
* @return the bean definition
* @throws ClassNotFoundException if the bean class could not be loaded
*/
public static AbstractBeanDefinition createBeanDefinition(String parentName, String className, ClassLoader classLoader)
throws ClassNotFoundException {

GenericBeanDefinition bd = new GenericBeanDefinition();
bd.setParentName(parentName);
if (className != null) {
if (classLoader != null) {
bd.setBeanClass(ClassUtils.forName(className, classLoader));
} else {
bd.setBeanClassName(className);
}
}
return bd;
}

/**
* Generate a bean name for the given bean definition, unique within the
* given bean factory.
*
* 生成Bean名称
*
* @param definition the bean definition to generate a bean name for
* @param registry the bean factory that the definition is going to be
* registered with (to check for existing bean names)
* @param isInnerBean whether the given bean definition will be registered
* as inner bean or as top-level bean (allowing for special name generation
* for inner beans versus top-level beans)
* @return the generated bean name
* @throws BeanDefinitionStoreException if no unique name can be generated
* for the given bean definition
*/
public static String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry, boolean isInnerBean)
throws BeanDefinitionStoreException {

String generatedBeanName = definition.getBeanClassName();

// 获取到的类名为空的情况
if (generatedBeanName == null) {
if (definition.getParentName() != null) {
// 根据父类命名
generatedBeanName = definition.getParentName() + "$child";
} 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.
// 如果是内部bean,带有哈希值
id = generatedBeanName + GENERATED_BEAN_NAME_SEPARATOR + ObjectUtils.getIdentityHexString(definition);
} else {
// Top-level bean: use plain class name.
// Increase counter until the id is unique.
// 如果不是内部Bean,则直接序号递增即可
int counter = -1;
while (counter == -1 || registry.containsBeanDefinition(id)) {
counter++;
id = generatedBeanName + GENERATED_BEAN_NAME_SEPARATOR + counter;
}
}
return id;
}

/**
* Generate a bean name for the given top-level bean definition,
* unique within the given bean factory.
*
* 生成Bean名称
*
* @param beanDefinition the bean definition to generate a bean name for
* @param registry the bean factory that the definition is going to be
* registered with (to check for existing bean names)
* @return the generated bean name
* @throws BeanDefinitionStoreException if no unique name can be generated
* for the given bean definition
*/
public static String generateBeanName(BeanDefinition beanDefinition, BeanDefinitionRegistry registry)
throws BeanDefinitionStoreException {

return generateBeanName(beanDefinition, registry, false);
}

/**
* Register the given bean definition with the given bean factory.
*
* 注册Bean定义
*
* @param definitionHolder the bean definition including name and aliases
* @param registry the bean factory to register with
* @throws BeanDefinitionStoreException if registration failed
*/
public static void registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
throws BeanDefinitionStoreException {

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

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

/**
* Register the given bean definition with a generated name,
* unique within the given bean factory.
*
* 用自动生成的Bean名称来注册Bean
*
* @param definition the bean definition to generate a bean name for
* @param registry the bean factory to register with
* @return the generated bean name
* @throws BeanDefinitionStoreException if no unique name can be generated
* for the given bean definition or the definition cannot be registered
*/
public static String registerWithGeneratedName(AbstractBeanDefinition definition, BeanDefinitionRegistry registry)
throws BeanDefinitionStoreException {

// 获取生成的Bean名称
String generatedName = generateBeanName(definition, registry, false);
registry.registerBeanDefinition(generatedName, definition);
return generatedName;
}

}