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

问题需求

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

通过扫描获取自定义注解的类

定义两个注解

@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 "未知错误";
}

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

我们定义一个配置类,在其中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 {
 
    }
}

获取Spring的ApplicationContent 上下问对象。

@Resource
private ApplicationContext applicationContext;

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

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

通过反射获取自定义注解的类

   	    private static final String STRATEGY_IMPLEMENTATION_PACKAGE = "com.izkml.mlyun.partybuilding.strategy";

        Reflections reflections = new Reflections(STRATEGY_IMPLEMENTATION_PACKAGE);
        Set<Class<?>> classSet = reflections.getTypesAnnotatedWith(PushNoteBiz.class);
        classSet.forEach(aClass -> {
            PushNoteBiz annotation = aClass.getAnnotation(PushNoteBiz.class);
            STRATEGY_MAP.put(annotation.value(), aClass);
        });

通过bean属性获取

不仅仅可以判断属于什么数据类型,还可以获取自定义注解

    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
        if (Objects.nonNull(beanProperty)) {
            //判断是否为string类型
            if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) {
                DataMasking anno = beanProperty.getAnnotation(DataMasking.class);
                if (Objects.isNull(anno)) {
                    anno = beanProperty.getContextAnnotation(DataMasking.class);
                }
                if (Objects.nonNull(anno)) {
                    return new DataMaskingSerialize(anno.type(), anno.start(), anno.end());
                }
            }
            return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
        }
        return serializerProvider.findNullValueSerializer(null);
    }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值