美文网首页StringBoot
Spring扫描自定义注解,获取注解信息

Spring扫描自定义注解,获取注解信息

作者: King斌 | 来源:发表于2022-08-25 15:54 被阅读0次

    自定义注解

    package com.demo.anno;
    
    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Inherited;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
     
    
    @Documented
    @Target({ElementType.TYPE, ElementType.METHOD})//该注解只能写在类上
    @Retention(RetentionPolicy.RUNTIME)//停留在源码阶段
    @Inherited//继承
    public @interface MyAnnotation {
    
        String name() default "";
    
        String value() default "";
    
        String message() default "";
    
    }
    

    获取注解工具类

    /*
     * 版权所有 2009-2022山东新北洋信息技术股份有限公司 保留所有权利。
     */
    package com.demo.anno;
    
    
    import org.springframework.core.io.Resource;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.core.io.support.ResourcePatternResolver;
    import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
    import org.springframework.core.type.classreading.MetadataReader;
    import org.springframework.core.type.classreading.MetadataReaderFactory;
    import org.springframework.util.ClassUtils;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
     
    public class MyAnnoUtil {
    
        // 注解扫描包
        private static final String BASE_PACKAGE = "com.demo.service.impl";
    
        private static final String RESOURCE_PATTERN = "/**/*.class";
    
        public static List<Map<String, String>> ANNO_LIST = new ArrayList<>();
    
        static {
            //spring工具类,可以获取指定路径下的全部类
            ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
            try {
                String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
                        ClassUtils.convertClassNameToResourcePath(BASE_PACKAGE) + RESOURCE_PATTERN;
                Resource[] resources = resourcePatternResolver.getResources(pattern);
                //MetadataReader 的工厂类
                MetadataReaderFactory readerfactory = new CachingMetadataReaderFactory(resourcePatternResolver);
                for (Resource resource : resources) {
                    //用于读取类信息
                    MetadataReader reader = readerfactory.getMetadataReader(resource);
                    //扫描到的class
                    String classname = reader.getClassMetadata().getClassName();
                    Class<?> clazz = Class.forName(classname);
                    //判断是否有指定主解
                    MyAnnotation anno = clazz.getAnnotation(MyAnnotation.class);
                    if (anno != null) {
                        //将注解中的类型值作为key,对应的类作为 value
                        HashMap<String, String> annoMap = new HashMap<>();
                        annoMap.put("name", anno.name());
                        annoMap.put("value", anno.value());
                        annoMap.put("message", anno.message());
                        ANNO_LIST.add(annoMap);
                    }
                }
            } catch (IOException | ClassNotFoundException e) {
                throw e;
            }
        }
    
    }
    
    

    相关文章

      网友评论

        本文标题:Spring扫描自定义注解,获取注解信息

        本文链接:https://www.haomeiwen.com/subject/gsfzgrtx.html