Spring Boot 2 實踐記錄之 組合注解原理


Spring 的組合注解功能,網上有很多文章介紹,不過都是介紹其使用方法,鮮有其原理解析。

組合注解並非 Java 的原生能力。就是說,想通過用「注解A」來注解「注解B」,再用「注解B」 來注解 C(類或方法),就能夠使 C 同時擁有「注解A」和「注解B」是行不通的。

示例如下:

先定義注解 SuperAnno:

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SuperAnno {
}

再定義注解 SubAnno,並使用 SuperAnno 注解 SubAnno:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@SuperAnno
@SomeAnno
public @interface SubAnno {
}

定義 Test,使用 SubAnno 注解 Test:

@SubAnno
public class Test {

}

測試一下,看看我們能不能拿到 Test 的 SuperAnno 注解:

import java.lang.annotation.Annotation;

public class Main {

    public static void main(String[] args) {
    // write your code here
        Test test = new Test();
        Class<?> cl = test.getClass();
        Annotation[] annotations = cl.getAnnotations();
        for(Annotation annotation: annotations) {
            System.out.println(annotation.annotationType());
        }
    }
}

打印出來的結果為:

interface com.company.SubAnno

唔,SuperAnno 沒有出現。

怎么拿到合並的注解呢?

核心的思路就是拿到注解后,遞歸獲取其上的注解,見下例:

import javax.annotation.*;
import java.lang.annotation.*;
import java.util.ArrayList;

public class Main {

    private ArrayList<Annotation> annos = new ArrayList<>();

    public static void main(String[] args) {
    // write your code here
        Test test = new Test();
        Class<?> cl = test.getClass();
        Main main = new Main();
        main.getAnnos(cl);
        for(Annotation anno: main.annos) {
            System.out.println(anno.annotationType());
        }
    }

    private void getAnnos(Class<?> cl) {
        Annotation[] annotations = cl.getAnnotations();
        for(Annotation annotation: annotations) {
            if (annotation.annotationType() != Deprecated.class &&
                    annotation.annotationType() != SuppressWarnings.class &&
                    annotation.annotationType() != Override.class &&
                    annotation.annotationType() != PostConstruct.class &&
                    annotation.annotationType() != PreDestroy.class &&
                    annotation.annotationType() != Resource.class &&
                    annotation.annotationType() != Resources.class &&
                    annotation.annotationType() != Generated.class &&
                    annotation.annotationType() != Target.class &&
                    annotation.annotationType() != Retention.class &&
                    annotation.annotationType() != Documented.class &&
                    annotation.annotationType() != Inherited.class
            ) {
                annos.add(annotation);
                getAnnos(annotation.annotationType());
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM