使用MetadataReader解析注解

本文介绍了如何使用Spring的MetadataReader来解析注解。MetadataReader作为类元数据的读取器,其作用在于获取和处理类上的注解信息。文章详细讲解了MetadataReader的创建过程,通常通过MetadataReaderFactory来创建,并探讨了AnnotationMetadata的功能,展示了它如何帮助开发者方便地获取和利用注解数据。
摘要由CSDN通过智能技术生成

背景

在工作3年以上的时候,在写业务代码的时候我们经常会自定义一些注解用于用于简化代码的开发。一个比较实用的例子见 上一篇博客 。注解其实只是作为一个标识,而真正起作用的代码需要我们自己定义,但是前提是我们要找到这个注解,并解析这个注解的相关信息,我们可以使用jdk提供的类来做,但是它不够灵活,这篇文章介绍另一种方式,也是spring解析注解用的方式 MetadataReader,它是spring提供的一个工具。

MetadataReader介绍

一、MetadataReader 的作用

MetadataReader表示类的元数据读取器。我们来看一下这个接口的功能

public interface MetadataReader {
   

	/**
	 * Return the resource reference for the class file.
	 * 返回class文件的resource
	 */
	Resource getResource();

	/**
	 * Read basic class metadata for the underlying class.
	 */
	ClassMetadata getClassMetadata();

	/**
	 * Read full annotation metadata for the underlying class,
	 * including metadata for annotated methods.
	 */
	AnnotationMetadata getAnnotationMetadata();

}

它的默认实现类有
在这里插入图片描述
我们比较常用的类是 SimpleMetadataReader

二、创建MetadataReader

我们大致可以从接口中看到MetadataReader的作用,那么在实际使用过程中该如何创建这个reader呢?
spring使用了工厂设计模式来创建,它提供了一些 MetadataReader 的工厂类,这些工厂类的作用见接口

public interface MetadataReaderFactory {
   

	/**
	 * Obtain a MetadataReader for the given class name.
	 * @param className the class name (to be resolved to a ".class" file)
	 * @return a holder for the ClassReader instance (never {@code null})
	 * @throws IOException in case of I/O failure
	 */
	MetadataReader getMetadataReader(String className) throws IOException;

	/**
	 * Obtain a MetadataReader for the given resource.
	 * @param resource the resource (pointing to a ".class" file)
	 * @return a holder for the ClassReader instance (never {@code null})
	 * @throws IOException in case of I/O failure
	 */
	MetadataReader getMetadataReader(Resource resource) throws IOException;

}

它的实现类有:
在这里插入图片描述
我们常使用的为 SimpleMetadataReaderFactory

		final SimpleMetadataReaderFactory simpleMetadataReaderFactory = new SimpleMetadataReaderFactory();
        final MetadataReader metadataReader = simpleMetadataReaderFactory.getMetadataReader("com.interfaceJ.matedata.UserService");
        final AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        // 如果这个类有方法上注解了 @EnableRoles,则创建动态代理
        if (annotationMetadata.hasAnnotatedMethods(EnableRoles.class.getName())) {
   
            for (MethodMetadata method : annotationMetadata.getAnnotatedMethods(EnableRoles.class.getName())) {
   
                // 。。。创建动态代理
                final Map<String, Object> attributes = method.getAnnotationAttributes(EnableRoles.class.getName
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值