首页 > 其他分享 >springboot中获取指定包下的包含某个注解的全部类(转)

springboot中获取指定包下的包含某个注解的全部类(转)

时间:2023-04-12 16:45:03浏览次数:54  
标签:core springboot 包下 import springframework org 注解 class String

来自:https://www.cnblogs.com/lexiaoyao1995/p/13943784.html

需求

获取spring项目里的带有某个注解的全部类

难点
需要扫描指定包路径下的类,同时也要扫描其下所有子包

思路

可以自己实现,推荐使用spring的工具类

代码

package com.example.demo;

import com.example.demo.annos.MyAnno;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
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.test.context.junit4.SpringRunner;
import org.springframework.util.ClassUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    private final String BASE_PACKAGE = "com.example.demo";
    private final String RESOURCE_PATTERN = "/**/*.class";

    @Test
    public void test() {
        Map<String, Class> handlerMap = new HashMap<String, Class>();

        //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);
                //判断是否有指定主解
                MyAnno anno = clazz.getAnnotation(MyAnno.class);
                if (anno != null) {
                    //将注解中的类型值作为key,对应的类作为 value
                    handlerMap.put(classname, clazz);
                }
            }
        } catch (IOException | ClassNotFoundException e) {
        }
    }

}

 

标签:core,springboot,包下,import,springframework,org,注解,class,String
From: https://www.cnblogs.com/wangbin2188/p/17310316.html

相关文章

  • spring声明式事务(注解)
     xml中开启注解驱动:  ......
  • Springboot LIST进行分割方法
    在开发使用中我们经常会需要把一个LIST按组进行分割,下面记录一下我的使用方法以便以后使用/***LIST进行分割*@paramlist*@paramsubNum*@param<T>*@return*/publicstatic<T>List<List<T>>splistList(List<T>list,int......
  • Springboot集成dubbo完整过程(三)
    准备工作1,准备mysql服务环境2,准备redis服务环境3,准备zookeeper服务环境4,准备逆向生成bean的xml配置文件5,准备slf4j日志xml配置文件6,准备一个sql脚本1,搭建创建服务工程1,创建一个空的父工程,用来统一管理依赖2,创建一个interface接口工程,主要存放业务bean,接口类3,创建一......
  • springboot -eclipse安装springboot插件注意事项
    1.下载包,本地安装,在线安装容易出问题;2.下载的包版本要和eclipse版本一致;3.mac电脑显示和隐藏文件的方法:shift+command+<或者>,分别是显示和隐藏;4.大概率碰到编译报错说找不到org.eclipse.debug.core的问题,第三条就是为了显示隐藏文件,删除/.metadata/.plugins目录下的org.eclipse.......
  • 动力节点王鹤SpringBoot3笔记——第七章 视图技术Thymeleaf
    7视图技术ThymeleafThymeleaf是一个表现层的模板引擎,一般被使用在Web环境中,它可以处理HTML,XML、JS等文档,简单来说,它可以将JSP作为JavaWeb应用的表现层,有能力展示与处理数据。Thymeleaf可以让表现层的界面节点与程序逻辑被共享,这样的设计,可以让界面设计人员、业......
  • 动力节点SpringBoot3笔记——视图技术Thymeleaf
    7视图技术ThymeleafThymeleaf是一个表现层的模板引擎,一般被使用在Web环境中,它可以处理HTML,XML、JS等文档,简单来说,它可以将JSP作为JavaWeb应用的表现层,有能力展示与处理数据。Thymeleaf可以让表现层的界面节点与程序逻辑被共享,这样的设计,可以让界面设计人员、业......
  • SpringBoot项目启动执行任务的几种方式
    1、直接在启动类下面调用方法@SpringBootApplicationpublicclassTestApplication{publicstaticvoidmain(String[]args){SpringApplication.run(TestApplication.class,args);System.out.println("在启动类添加初始下方法");}}2、使用@P......
  • p6spy 整合springboot
    1.导入pom<!--sql代理拦截,慢sql打印--><dependency><groupId>p6spy</groupId><artifactId>p6spy</artifactId><version>3.9.1</version></dependency>2.配置自定义日志importcom.p6spy.engine.common.P6Util;......
  • SpringBoot 集成 MybatisPlus 六——ActiveRecord 增、删、改
    1向表中插入记录1.1插入所有列在创建实体对象时,指定所有字段的内容,包括ID列。@TestpublicvoidtestAddUser(){Useruser=newUser(20,"成吉思汗","男","一代天骄");booleanres=user.insert();System.out.println(res);}调用MyBatisPlus时,后台执行的......
  • SpringBoot实现文件图片上传并转换为虚拟路径
    页面代码<!DOCTYPEhtml><htmllang="en"xmlns:th="http://www.thymeleaf.org"><head><metacharset="UTF-8"><title>Title</title></head><body><formaction="/putpic......