Spring元数据Metadata

本文介绍了Spring中的元数据概念,包括ClassMetadata、StandardClassMetadata、AnnotatedTypeMetadata和MethodMetadata。ClassMetadata提供对class信息的抽象,StandardClassMetadata是基于Java反射的实现,AnnotatedTypeMetadata用于封装注解元素,提供方便的注解访问。MethodMetadata描述方法信息,有基于反射和ASM的实现。元数据在框架设计中起关键作用,特别是在注解编程中,能够解决Java抽象的局限性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

元数据

元数据:数据的数据,比如Class就是一种元数据

可以看到顶层接口有两个:ClassMetadataAnnotatedTypeMetadata 

注解上的注解Spring将其定义为元注解(meta-annotation),如 @Component标注在 @Service上,@Component就被称作为元注解。后面我们就将注解的注解称为元注解。 

ClassMetadata:对Class的抽象和适配

此接口的所有方法,都是获取class上的元素


public interface ClassMetadata {

	/**
	 * Return the name of the underlying class.
	 */
	String getClassName();

	/**
	 * Return whether the underlying class represents an interface.
	 */
	boolean isInterface();

	/**
	 * Return whether the underlying class represents an annotation.
	 * @since 4.1
	 */
	boolean isAnnotation();

	/**
	 * Return whether the underlying class is marked as abstract.
	 */
	boolean isAbstract();

	/**
	 * // 是否允许创建  不是接口且不是抽象类  这里就返回true了
	 * 
	 */
	boolean isConcrete();

	/**
	 * Return whether the underlying class is marked as 'final'.
	 */
	boolean isFinal();

	/**
	 * 是否是独立的(能够创建对象的)  比如是Class、或者内部类、静态内部类
	 */
	boolean isIndependent();

	/**
	 * 是否有内部类
	 */
	boolean hasEnclosingClass();

	/**
	 * Return the name of the enclosing class of the underlying class,
	 * or {@code null} if the underlying class is a top-level class.
	 */
	@Nullable
	String getEnclosingClassName();

	/**
	 * Return whether the underlying class has a super class.
	 */
	boolean hasSuperClass();

	/**
	 * Return the name of the super class of the underlying class,
	 * or {@code null} if there is no super class defined.
	 */
	@Nullable
	String getSuperClassName();

	// 会把实现的所有接口名称都返回  具体依赖于Class#getSuperclass
	String[] getInterfaceNames();

	// 基于:Class#getDeclaredClasses  返回类中定义的公共、私有、保护的内部类
	String[] getMemberClassNames();

}

继承树如下

StandardClassMetadata

基于Java标准的(Standard)反射实现元数据的获取

/*
 * Copyright 2002-2017 the original author or auth
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值