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

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

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) {
        }
    }

}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于创建spring应用程序的框架。通过它,我们可以快速地搭建一个spring应用,并且可以通过简单的配置来完成很多繁琐的工作。在Spring Boot注解被广泛使用,以下是一些常用的注解及其作用: 1. @SpringBootApplication 该注解Spring Boot应用程序的入口,表示该应用是一个Spring Boot应用,会自动配置spring应用程序并扫描注解、配置等信息。相当于在spring使用@Configuration,@EnableAutoConfiguration和@ComponentScan 3个注解的组合体。 2. @RestController 该注解是一个组合注解包含@Controller和@ResponseBody注解。使用该注解可以把当前所有的方法返回数据直接放到HTTP响应体,而不是跳转到某个页面。 3. @Autowired 该注解是自动注入依赖的方式,Spring会根据型自动装配需要的Bean。 4. @Value 该注解获取配置文件的属性值,我们可以直接把值注入到指定的变量,而不需要通过代码手动解析配置文件的内容。 5. @RequestMapping/@GetMapping/@PostMapping 这些HTTP请求的注解用于绑定REST请求或网页的url路径,并指明请求方法。 6. @RequestBody 该注解将HTTP请求的json或xml数据自动绑定到Java对象。 7. @Valid/@NotNull/@NotEmpty/@NotBlank 这些注解是Java Bean Validation(JSR-303)的注解,用于数据校验,让开发人员可以自定义校验规则,使得数据合法性处理更方便、标准化。 总之,Spring Boot注解让开发人员能够更方便地开发和维护应用程序。使用注解,可以使代码更加简洁和易于阅读,使得开发效率得到大幅提升。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值