java 获取所有添加了指定注解的接口

 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Component
public class TestCon {
    @Autowired
    private ResourceLoader resourceLoader;

    private static final String VALUE = "value";

    /**
     * 获取指定包下所有添加了执行注解的方法信息
     *
     * @param classPath          包名
     * @param tagAnnotationClass 指定注解类型
     * @param <T>
     * @return
     * @throws Exception
     */
    public <T> Map<String, Map<String, Object>> getAllAddTagAnnotationUrl(String classPath, Class<T> tagAnnotationClass)
            throws Exception {
        Map<String, Map<String, Object>> resMap = new HashMap<>();
        ResourcePatternResolver resolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
        MetadataReaderFactory metaReader = new CachingMetadataReaderFactory(resourceLoader);
        Resource[] resources = resolver.getResources(classPath);

        for (org.springframework.core.io.Resource r : resources) {
            MetadataReader reader = metaReader.getMetadataReader(r);
            resMap = resolveClass(reader, resMap, tagAnnotationClass);
        }

        return resMap;
    }

    private <T> Map<String, Map<String, Object>> resolveClass(
            MetadataReader reader, Map<String, Map<String, Object>> resMap, Class<T> tagAnnotationClass)
            throws Exception {
        String tagAnnotationClassCanonicalName = tagAnnotationClass.getCanonicalName();
        //获取注解元数据
        AnnotationMetadata annotationMetadata = reader.getAnnotationMetadata();

        //获取类中RequestMapping注解的属性
        Map<String, Object> annotationAttributes =
                annotationMetadata.getAnnotationAttributes(RequestMapping.class.getCanonicalName());

        //若类无RequestMapping注解
        if (annotationAttributes == null) {
            return resMap;
        }

        //获取RequestMapping注解的value
        String[] pathParents = (String[]) annotationAttributes.get(VALUE);
        if (0 == pathParents.length) {
            return resMap;
        }

        //获取RequestMapping注解的value
        String pathParent = pathParents[0];

        //获取当前类中已添加要扫描注解的方法
        Set<MethodMetadata> annotatedMethods = annotationMetadata.getAnnotatedMethods(tagAnnotationClassCanonicalName);

        Set<String> annotationTypes = annotationMetadata.getAnnotationTypes();
        boolean allFlag = false;
        if (annotationTypes.contains(tagAnnotationClassCanonicalName)) {
            annotatedMethods = getMethodMetadata(annotationMetadata);
            allFlag = true;
        }

        for (MethodMetadata annotatedMethod : annotatedMethods) {
            //获取当前方法中要扫描注解的属性
            Map<String, Object> targetAttr = annotatedMethod.getAnnotationAttributes(tagAnnotationClassCanonicalName);
            //获取当前方法中要xxxMapping注解的属性
            Map<String, Object> mappingAttr = getPathByMethod(annotatedMethod);
            if (mappingAttr == null) {
                continue;
            }

            String[] childPath = (String[]) mappingAttr.get(VALUE);
            if (targetAttr == null || childPath == null || childPath.length == 0) {

                if (!allFlag) {
                    continue;
                }

            }

            String s = childPath[0];
            boolean equals = s.substring(0, 1).equals("/");
            boolean pathParentequals = pathParent.substring(0, 1).equals("/");
            if (!pathParentequals) {
                pathParent = "/" + pathParent;
            }
            String path = "";
            if (equals) {
                path = pathParent + childPath[0];
            } else {
                path = pathParent + "/" + childPath[0];
            }

            boolean isHas = resMap.containsKey(path);
            if (isHas) {
                throw new Exception("重复定义了相同的映射关系");
            }

            resMap.put(path, targetAttr);
        }

        return resMap;
    }

    private Set<MethodMetadata> getMethodMetadata(AnnotationMetadata annotatedMethod) {

        Set<MethodMetadata> annotatedMethods = new HashSet<>();
        Set<MethodMetadata> annotatedMethodsGet =
                annotatedMethod.getAnnotatedMethods(GetMapping.class.getCanonicalName());

        Set<MethodMetadata> annotatedMethodsPost =
                annotatedMethod.getAnnotatedMethods(PostMapping.class.getCanonicalName());
        Set<MethodMetadata> annotatedMethodsDelete =
                annotatedMethod.getAnnotatedMethods(DeleteMapping.class.getCanonicalName());

        Set<MethodMetadata> annotatedMethodsPut =
                annotatedMethod.getAnnotatedMethods(PutMapping.class.getCanonicalName());

        Set<MethodMetadata> annotatedMethodsRequest =
                annotatedMethod.getAnnotatedMethods(RequestMapping.class.getCanonicalName());
        annotatedMethods.addAll(annotatedMethodsGet);
        annotatedMethods.addAll(annotatedMethodsPost);
        annotatedMethods.addAll(annotatedMethodsDelete);
        annotatedMethods.addAll(annotatedMethodsPut);
        annotatedMethods.addAll(annotatedMethodsRequest);

        return annotatedMethods;
    }

    private Map<String, Object> getPathByMethod(MethodMetadata annotatedMethod) {
        Map<String, Object> annotationAttributes =
                annotatedMethod.getAnnotationAttributes(GetMapping.class.getCanonicalName());
        if (annotationAttributes != null && annotationAttributes.get(VALUE) != null) {
            return annotationAttributes;
        }
        annotationAttributes = annotatedMethod.getAnnotationAttributes(PostMapping.class.getCanonicalName());
        if (annotationAttributes != null && annotationAttributes.get(VALUE) != null) {
            return annotationAttributes;
        }

        annotationAttributes = annotatedMethod.getAnnotationAttributes(DeleteMapping.class.getCanonicalName());
        if (annotationAttributes != null && annotationAttributes.get(VALUE) != null) {
            return annotationAttributes;
        }

        annotationAttributes = annotatedMethod.getAnnotationAttributes(PutMapping.class.getCanonicalName());
        if (annotationAttributes != null && annotationAttributes.get(VALUE) != null) {
            return annotationAttributes;
        }
        annotationAttributes = annotatedMethod.getAnnotationAttributes(RequestMapping.class.getCanonicalName());
        return annotationAttributes;
    }
}

上面做完之后直接调用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RPC接口实现层注解是一种在RPC框架中用来标识接口实现类的注解。在RPC(Remote Procedure Call,远程过程调用)中,通过网络将客户端的请求发送给服务端并获取响应结果。在服务端,需要提供接口的具体实现类来处理来自客户端的请求。 为了简化RPC服务端的开发,可以采用注解来标识接口实现类。在Java中,常用的RPC框架如Dubbo、gRPC等都提供了相应的注解,如@Service、@GrpcService等。通过在接口实现类上添加这些注解,可以让RPC框架自动扫描并注册服务。 这些RPC接口实现层注解的作用如下: 1. 标识接口实现类:通过添加注解,可以告诉RPC框架哪些类是具体的接口实现类,框架会自动扫描并注册这些服务。 2. 定义服务名称:注解中通常会包含name或value属性,用来指定该服务的名称,客户端可以通过该名称来定位并调用相应的服务。 3. 指定服务端口:有些注解还提供了port属性,用来指定该服务监听的端口号。 4. 其他配置信息:一些RPC框架的注解还可能包含其他配置属性,如超时时间、负载均衡策略等。 在使用RPC接口实现层注解时,通常需要在配置文件中配置相应的扫描路径或其他相关配置,以告知RPC框架在哪些包下寻找接口实现类。框架会根据这些配置信息进行服务注册、服务提供等操作,使得开发者可以通过简单的注解实现RPC服务的发布与调用。 总而言之,RPC接口实现层注解是一种便捷的方式,通过在接口实现类上添加注解,可以省去手动配置服务注册等繁琐的操作,提高了RPC服务端的开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值