• 一文了解BeanNameGenerator


    一文了解BeanNameGenerator

    前言

    springboot的项目中,我们会使用到很多的注解,比如@Service@Bean等,添加这些注解SpringBoot框架会帮忙创建bean,并且用唯一的名字来区分,那我们就来看看源是怎么实现的吧。

    相关源码

    BeanNameGenerator接口

    如下是BeanNameGenerator接口代码,在代码中有一个generateBeanName的方法,接下来看看该方法的具体实现。

    public interface BeanNameGenerator {
    
    	/**
    	 * Generate a bean name for the given bean definition.
    	 * @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);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    如下图所示:该方法有很多的实现,比如XML,注解, ClassPath
    在这里插入图片描述

    由于我们在代码中常用注解的方式来注册一个bean,因此我们来继续看看AnnotationBeanNameGenerator类,它实现了BeanNameGenerator接口,具体看如下的代码:

    AnnotationBeanNameGenerator实现类
    public class AnnotationBeanNameGenerator implements BeanNameGenerator {
    
    	private static final String COMPONENT_ANNOTATION_CLASSNAME = "org.springframework.stereotype.Component";
    
    
    	@Override
    	public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
    		if (definition instanceof AnnotatedBeanDefinition) {
    			String beanName = determineBeanNameFromAnnotation((AnnotatedBeanDefinition) definition);
    			if (StringUtils.hasText(beanName)) {
    				// Explicit bean name found.
    				return beanName;
    			}
    		}
    		// Fallback: generate a unique default bean name.
    		return buildDefaultBeanName(definition, registry);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    AnnotationBeanNameGenerator类中,实现了generateBeanName方法,在该方法中,使用determineBeanNameFromAnnotation解析出definition中的bean名字。具体看看代码:

    /**
    	 * Derive a bean name from one of the annotations on the class.
    	 * @param annotatedDef the annotation-aware bean definition
    	 * @return the bean name, or {@code null} if none is found
    	 */
    @Nullable
    protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) {
        AnnotationMetadata amd = annotatedDef.getMetadata();
        Set<String> types = amd.getAnnotationTypes();
        String beanName = null;
        for (String type : types) {
            AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(amd, type);
            if (attributes != null && isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) {
                Object value = attributes.get("value");
                if (value instanceof String) {
                    String strVal = (String) value;
                    if (StringUtils.hasLength(strVal)) {
                        if (beanName != null && !strVal.equals(beanName)) {
                            throw new IllegalStateException("Stereotype annotations suggest inconsistent " +
                                                            "component names: '" + beanName + "' versus '" + strVal + "'");
                        }
                        beanName = strVal;
                    }
                }
            }
        }
        return beanName;
    }
    
    • 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

    在该方法中,对传入的参数annotatedDef提取元数据,提取注解的类型,接着在for循环中使用AnnotationConfigUtils解析出attributes,然后从attributes提取出value,即bean的名字。

  • 相关阅读:
    postgresql源码学习(36)—— 事务日志11 - 日志归档
    微信H5页面点击直接跳转app-微信开放标签
    HTML问题
    JavaScript+css实现的动态生成3D树效果html页面前端源码
    这份阿里P8架构师学习路线仅一晚GitHub狂揽6W+赞,巅峰之作?
    Spring事务@Transactional 注解下,事务失效的七种场景
    想学C语言,跟着一个大佬学,中间一步错,后面就步步错,我该怎么办啊?
    Next.js 13 appDir 实战 i18n
    ESP32-添加多目录的自定义组件
    AcrelEMs-EDU智慧校园能源管理系统解决方案
  • 原文地址:https://blog.csdn.net/qq_31900497/article/details/126609092