Alice52 / spring-5.2.x

spring source code
https://github.com/Alice52/spring-5.2.x/issues/2
0 stars 1 forks source link

[annotation] customize annotation #22

Closed Alice52 closed 2 years ago

Alice52 commented 4 years ago

type

  1. method annotation
  2. handler annotation
    • HandlerInterceptorAdapter
  3. customize annotation
    • [@Mobile]: @Constraint(validatedBy = MobileDescriptor.class)
    • [@Email]: EmailValidator: 创建 validator是会内置
    • validator: ConstraintValidator
    • [@Transactional]: aop 上做拦截: 可以使用 @Around 注解做拦截 || MethodInterceptor + DefaultPointcutAdvisor + AnnotationMatchingPointcut
      • 可以自定义延迟消费注解: @RocketMQBasedDelay
      • @LocalIdempotentRequest
      • @LocalLimitRequest
      • [@DeSensitive]: 数据脱敏, 序列化相关
      • [@DateTimeFormat]: 屏蔽底层细节-运行时参数类型获取对应的 Parser/Printer 进行工作
  4. spring annotation

    • 使用AOP切面做拦截指定注解: 注解 + 切面
    • 使用APC获取被指定注解修饰的类: ListableBeanFactory.findAnnotationOnBean(String beanName, Class<A> annotationType)
    • 使用spring的工具类: AnnotationUtils.findAnnotation(Class<?> clazz, Class<A> annotationType)
    • 使用DLBF获取被指定注解修饰的类: AnnotationUtils.getAnnotation(Method method, Class<A> annotationType)

      public static <A extends Annotation> A findAnnotationOnBean(
            String beanName, Class<A> annotationType) {
        AbstractApplicationContext context =
                (AbstractApplicationContext) ApplicationContextHolder.getInstance();
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
      
        return Optional.of(beanFactory)
                .map(each -> (RootBeanDefinition) beanFactory.getMergedBeanDefinition(beanName))
                .map(RootBeanDefinition::getResolvedFactoryMethod)
                .map(factoryMethod -> AnnotationUtils.getAnnotation(factoryMethod, annotationType))
                .orElse(null);
      }

issue list

  1. how to define?

    @ParamsValidate(value = {@ParamValidate(name = "name", notNull = true, message = "不能为空", maxLen = 200)})

reference

  1. https://www.cnblogs.com/peida/archive/2013/04/24/3036689.html
Alice52 commented 2 years ago

annotation member type limit: got from oracle 官方的 docs

  1. A primitive type
  2. String
  3. Class
  4. An enum type
  5. An annotation type
  6. An array type :类型为以上任一类型的数组