Spring 自定义注解后,如何获取添加了该注解的所有类?终极解答

1、问题需求

       通常在业务开发时,我们可能会用到自定义注解(自定义注解的使用和解析,本章暂不介绍)。在使用自定义注解后,我们需要获取使用了该注解的所有类,然后做一个验证。那么、问题来了,我们如何获取被某注解标注的所有类呢?请欣赏下文:

2、具体实现

2.1、定义两个注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface RespMegTypeSup {
    public String value() default "未知错误";
}


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@RespMegTypeSup
public @interface RespMegType{
    public String value() default "未知错误";
}


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@RespMegTypeSup
public @interface RespMegType22 {
    public String value() default "未知错误";
}

2.2、首先我们定义自己的注解扫描器

 

       我们定义一个配置类,在其中postProcessBeanDefinitionRegistry,将我们自定义的注解添加到bean定义扫描器中。

/**
 * desc:
 * author: Administrator
 * date: 2020/11/4
 */
@Configuration
public class DefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry);
        scanner.setBeanNameGenerator(new AnnotationBeanNameGenerator());
        // 定义需要扫描的注解 -- 自定义注解
        scanner.addIncludeFilter(new AnnotationTypeFilter(RespMegTypeSup.class));
        // 定义扫描的包
        scanner.scan("com.xblog");
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }
}

2.3、获取Spring的ApplicationContent 上下问对象。

@Resource
private ApplicationContext applicationContext;

2.4、获取所有指定注解的类的bean信息

Map<String, Object> beansWithAnnotationMap = this.applicationContext.getBeansWithAnnotation(RespMegTypeSup.class);

 

至此、具体功能实现完毕。

如果觉得有帮助,麻烦给个赞。有好的实现方式,欢迎评论讨论。谢谢

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架允许我们自定义注解,用于标记特定的、方法或属性,并通过反射来获取或处理这些注解。下面是创建自定义注解的步骤: 1. 创建一个新的Java,用于定义自定义注解注解需要使用`@interface`关键字进行声明。例如: ```java package com.example.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface CustomAnnotation { // 在这里定义注解的属性 String value() default ""; } ``` 在上面的例子中,我们创建了一个名为`CustomAnnotation`的自定义注解,该注解被定义为在运行时可见,并且可以应用于级别。 2. 在中使用自定义注解。例如: ```java package com.example; import com.example.annotations.CustomAnnotation; @CustomAnnotation("Hello, world!") public class MyClass { // 的具体实现 } ``` 在上面的例子中,我们在`MyClass`上使用了自定义注解`CustomAnnotation`,并传递了一个字符串参数作为注解的值。 3. 使用反射获取自定义注解信息。例如: ```java package com.example; import com.example.annotations.CustomAnnotation; public class Main { public static void main(String[] args) { Class<?> clazz = MyClass.class; CustomAnnotation annotation = clazz.getAnnotation(CustomAnnotation.class); if (annotation != null) { String value = annotation.value(); System.out.println("Annotation value: " + value); } } } ``` 在上面的例子中,我们使用反射获取`MyClass`上的`CustomAnnotation`注解,并获取注解的值。 这就是使用Spring框架自定义注解的基本步骤。你可以根据实际需求在注解中定义不同的属性,并根据需要在应用程序中使用这些自定义注解

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值