zoukankan      html  css  js  c++  java
  • @AliasFor 原理

     

    用法:

    import org.springframework.core.annotation.AliasFor;
    
    import java.lang.annotation.*;
    
    @Target(ElementType.TYPE)//目标是方法
    @Retention(RetentionPolicy.RUNTIME) //注解会在class中存在,运行时可通过反射获取
    public @interface Request {
    
        @AliasFor("service")
        String value() default "";
    
        @AliasFor("value")
        String service() default "";
    
        String lang() default "zh-CN";
    
    }
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @RequestMapping(method = RequestMethod.GET)
    public @interface GetMapping {
    
        /**
         * Alias for {@link RequestMapping#name}.
         */
        @AliasFor(annotation = RequestMapping.class)
        String name() default "";

    AliasFor的作用

      1.互为指定属性,比如 我们定义的service,但如果我们想 @Request("OrderService") 这样指定 service,就必须使用@AliasFor
      2.继承注解类中的互为别名关系 如GetMapping

    @Request(value = "test1")
    @Slf4j
    public class Test {
    
        @org.junit.Test
        @GetMapping("test4")
        public void test4() throws NoSuchMethodException {
            Request ann = AnnotationUtils.findAnnotation(getClass(),Request.class);
            System.out.println(ann.value());
            System.out.println(ann.service());
    
            GetMapping test4 = AnnotationUtils.findAnnotation(getClass().getMethod("test4"), GetMapping.class);
            System.out.println(Lists.newArrayList(test4.value()));
            System.out.println(Lists.newArrayList(test4.path()));
    
            RequestMapping rq = AnnotationUtils.findAnnotation(getClass().getMethod("test4"), RequestMapping.class);
            System.out.println(rq.method());
        }

     原理:

    //AnnotationUtils static <A extends Annotation> A synthesizeAnnotation(A annotation, @Nullable Object annotatedElement) {
    //判断当前的注解是否是合成的注解:方法上带有别名的注解。
    if (!isSynthesizable(annotationType)) {
                return annotation;
            }
         //如果是合成的注解:构造动态代理,获取互为别名的注解属性。
            DefaultAnnotationAttributeExtractor attributeExtractor =
                    new DefaultAnnotationAttributeExtractor(annotation, annotatedElement);
            InvocationHandler handler = new SynthesizedAnnotationInvocationHandler(attributeExtractor);
    
            // Can always expose Spring's SynthesizedAnnotation marker since we explicitly check for a
            // synthesizable annotation before (which needs to declare @AliasFor from the same package)
            Class<?>[] exposedInterfaces = new Class<?>[] {annotationType, SynthesizedAnnotation.class};
            return (A) Proxy.newProxyInstance(annotation.getClass().getClassLoader(), exposedInterfaces, handler);




  • 相关阅读:
    07java基础知识
    06java基础知识
    我们都忽略了Html5的力量,如果只看成一种技术就大错特错了!
    “微信应用号对行业影响”之一,app开发速来围观
    App开发中甲乙方冲突会闹出啥后果?H5 APP 开发可以改变现状吗
    开发APP不搞清楚这20个问题,必然沦为一场灾难
    H5 App设计者需要注意的21条禁忌
    H5 APP开发必读,20个你不知道的Html5新特征和窍门
    H5 App如此强悍,要降薪的恐怕已不只是iOS程序员
    关于APP,原生和H5开发技术的争论
  • 原文地址:https://www.cnblogs.com/z-test/p/11676293.html
Copyright © 2011-2022 走看看