admin管理员组

文章数量:1641820

前言

本文旨在分析在Spring容器启动之时,针对特定注释的解析需要使用题目中所述方法,该方法的寻找路劲为:特定的注释的解析

①org.springframework.context.annotation.AnnotatedBeanDefinitionReader#registerBean(java.lang.Class<?>)
--->
②org.springframework.context.annotation.AnnotatedBeanDefinitionReader#doRegisterBean
--->
org.springframework.context.annotation.AnnotationConfigUtils#processCommonDefinitionAnnotations(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)
--->
③org.springframework.context.annotation.AnnotationConfigUtils#processCommonDefinitionAnnotations(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition, org.springframework.core.type.AnnotatedTypeMetadata)
--->
④org.springframework.context.annotation.AnnotationConfigUtils#processCommonDefinitionAnnotations(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition, org.springframework.core.type.AnnotatedTypeMetadata)

1.1

④源码为:

static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd, AnnotatedTypeMetadata metadata) {
        //同分析@Scope注解处理流程中调用方法,总之其作用为:在待注入bean中寻找@Lazy注解,见之前文章中分析
        //传送门:https://blog.csdn/seujava_er/article/details/108639480
        //注意:如果@Lazy注解找不到,则lazy为null 
		AnnotationAttributes lazy = attributesFor(metadata, Lazy.class); 
        //@Lazy注解被找到,则进入下if代码块中
		if (lazy != null) {
			abd.setLazyInit(lazy.getBoolean("value"));  //设置该待注入bean是需要进行懒加载的
		}
		else if (abd.getMetadata() != metadata) {

        // 补充检查,如果lazy != null成立,则会对传入的metadata进行检查
        //判断abd这个BeanDefinition接口的实现类的实例是否和metadata一致
        //不一致,则往下执行
        //重新获得lazy,保证数据的正确性
			lazy = attributesFor(abd.getMetadata(), Lazy.class);
			if (lazy != null) {
				abd.setLazyInit(lazy.getBoolean("value"));
			}
		}
        //在待注入bean中检查是否存在@Primary注释
		if (metadata.isAnnotated(Primary.class.getName())) {
			abd.setPrimary(true);  //存在则设置标志位primary为true
		}

       //在待注入bean中寻找@Lazy注解,作用同前
		AnnotationAttributes dependsOn = attributesFor(metadata, DependsOn.class);
		if (dependsOn != null) {
			abd.setDependsOn(dependsOn.getStringArray("value"));
		}

        //在待注入bean中寻找@Role注解,作用同前
		AnnotationAttributes role = attributesFor(metadata, Role.class);
		if (role != null) {
			abd.setRole(role.getNumber("value").intValue());
		}

       //在待注入bean中寻找@Description注解,作用同前
		AnnotationAttributes description = attributesFor(metadata, Description.class);
		if (description != null) {
			abd.setDescription(description.getString("value"));
		}
	}

1.2 参考文献

【1】https://segmentfault/a/1190000017413393?utm_source=tag-newest

【2】https://www.bilibili/video/BV1tx411o77Z?p=5

【3】《Spring源码解析》

本文标签: