JAVA自定義注解

JAVA自定義注解


注解概念

注解是Java SE 5.0版本開(kāi)始引入的概念,它是對(duì)java源代碼的說(shuō)明,是一種元數(shù)據(jù)(描述數(shù)據(jù)的數(shù)據(jù))。

注解和注釋的不同

  • 注釋

    注釋是對(duì)代碼的說(shuō)明,給代碼的讀者看,便于幫讀者梳理業(yè)務(wù)邏輯;在程序代碼中經(jīng)??吹降囊訞 開(kāi)頭的大部分是注解;

  • 注解

    注解也是對(duì)代碼的說(shuō)明,需要配合工具(解析它的代碼)使用,參與代碼的編譯,給應(yīng)用程序看的;

注解分類

注解以@開(kāi)頭,我們會(huì)在應(yīng)用程序中見(jiàn)到各種各樣的注解,比如 @Autowired@Service,@Controller,@Override,@Test@Value 等等,按照來(lái)源劃分,可以分為 JDK的注解,第三方的注解自定義注解。

JDK注解

JAVA 內(nèi)置注解

  • @Override (標(biāo)記重寫方法)
  • @Deprecated (標(biāo)記過(guò)時(shí))
  • @SuppressWarnings (忽略警告)

元注解 (注解的注解)

  • @Target (注解的作用目標(biāo))
  • @Retention (注解的生命周期)
  • @Document (注解是否被包含在JavaDoc中)
  • @Inherited (是否允許子類集成該注解)

第三方注解(各種框架注解)

請(qǐng)自行百度各個(gè)框架的注解詳解

自定義注解

使用元注解自己定義的注解

JDK中有一些元注解,主要有@Target,@Retention,@Document,@Inherited用來(lái)修飾注解。

@Target

表明該注解可以應(yīng)用的java元素類型

Target類型 描述
ElementType.TYPE 應(yīng)用于類、接口(包括注解類型)、枚舉
ElementType.FIELD 應(yīng)用于屬性(包括枚舉中的常量)
ElementType.METHOD 應(yīng)用于方法
ElementType.PARAMETER 應(yīng)用于方法的形參
ElementType.CONSTRUCTOR 應(yīng)用于構(gòu)造函數(shù)
ElementType.LOCAL_VARIABLE 應(yīng)用于局部變量
ElementType.ANNOTATION_TYPE 應(yīng)用于注解類型
ElementType.PACKAGE 應(yīng)用于包
ElementType.TYPE_PARAMETER 1.8版本新增,應(yīng)用于類型變量)
ElementType.TYPE_USE 1.8版本新增,應(yīng)用于任何使用類型的語(yǔ)句中(例如聲明語(yǔ)句、泛型和強(qiáng)制轉(zhuǎn)換語(yǔ)句中的類型)

@Retention

表明該注解的生命周期

生命周期類型 描述
RetentionPolicy.SOURCE 編譯時(shí)被丟棄,不包含在類文件中
RetentionPolicy.CLASS JVM加載時(shí)被丟棄,包含在類文件中,默認(rèn)值
RetentionPolicy.RUNTIME 由JVM 加載,包含在類文件中,在運(yùn)行時(shí)可以被獲取到

@Document

表明該注解標(biāo)記的元素可以被Javadoc 或類似的工具文檔化

@Inherited

表明使用了@Inherited注解的注解,所標(biāo)記的類的子類也會(huì)擁有這個(gè)注解

注解格式

/**
 * 修飾符 @interface 注解名 {
 * 注解元素的聲明1
 * 注解元素的聲明2
 * }
 * 修飾符:訪問(wèn)修飾符必須為public,不寫默認(rèn)為pubic;
 * 關(guān)鍵字:必須為@interface;
 * 注解名: 注解名稱為自定義注解的名稱,使用時(shí)還會(huì)用到;
 * 注解類型元素:注解類型元素是注解中內(nèi)容,可以理解成自定義接口的實(shí)現(xiàn)部分;
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyTestAnnotation {
    /**
     *  注解的元素聲明的兩種形式
     *  type elementName();
     *  type elementName() default value;  
     */
    String value() default "test";
}

注解樣例

接下來(lái)我們以Spring中的 @Service 注解為例

@Target({ElementType.TYPE})// ElementType.TYPE 代表在注解上使用
@Retention(RetentionPolicy.RUNTIME)// RetentionPolicy.RUNTIME 代表運(yùn)行時(shí)使用,可以通過(guò)反射獲取到
@Documented//包含在JavaDoc中
@Component//允許通過(guò)包掃描的方式自動(dòng)檢測(cè)
public @interface Service {

    /**
     * The value may indicate a suggestion for a logical component name,
     * to be turned into a Spring bean in case of an autodetected component.
     * @return the suggested component name, if any (or empty String otherwise)
     */
    @AliasFor(annotation = Component.class)
    String value() default "";
}

@Annotation

JDK1.5有的,在rt.jar包下 java.lang.annotation包下,所有的注解默認(rèn)繼承了Annotation接口,但是它本身不能定義注解。

package java.lang.annotation;

/**
 * 所有的注解默認(rèn)繼承了Annotation接口,但是它本身不能定義注解。
 * The common interface extended by all annotation types.  Note that an
 * interface that manually extends this one does <i>not</i> define
 * an annotation type.  Also note that this interface does not itself
 * define an annotation type.
 *
 * More information about annotation types can be found in section 9.6 of
 * <cite>The Java&trade; Language Specification</cite>.
 *
 * The {@link java.lang.reflect.AnnotatedElement} interface discusses
 * compatibility concerns when evolving an annotation type from being
 * non-repeatable to being repeatable.
 * 
 * @author  Josh Bloch
 * @since   1.5
 */
public interface Annotation {
    .
    .
    .
}

實(shí)現(xiàn)自定義注解

第一步-定義自定義注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyTestAnnotation {
    String value() default "test";
}

第二步-配置注解

@Data
@Builder
@MyTestAnnotation
public class MyBean {
    private String name;
    private int age;
}

第三步-利用反射解析注解

public class MyTest {

    //isAnnotationPresent:判斷當(dāng)前元素是否被指定注解修飾
    //getAnnotation:返回指定的注解
    //getAnnotations:返回所有的注解
    public static void main(String[] args) {
        try {
            //獲取MyBean的Class對(duì)象
            MyBean myBean = MyBean.builder().build();
            Class clazz = myBean.getClass();
            
            //判斷myBean對(duì)象上是否有MyTestAnnotation注解
            if (clazz.isAnnotationPresent(MyTestAnnotation.class)) {
                System.out.println("MyBean類上配置了MyTestAnnotation注解!");
                //獲取該對(duì)象上MyTestAnnotation類型的注解
                MyTestAnnotation myTestAnnotation = (MyTestAnnotation) clazz.getAnnotation(MyTestAnnotation.class);
                System.out.println(myTestAnnotation.value());
            } else {
                System.out.println("MyBean類上沒(méi)有配置MyTestAnnotation注解!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

執(zhí)行main方法,運(yùn)行結(jié)果:

Connected to the target VM, address: '127.0.0.1:62125', transport: 'socket'
MyBean類上配置了MyTestAnnotation注解!
test
Disconnected from the target VM, address: '127.0.0.1:62125', transport: 'socket'

參考鏈接:https://blog.csdn.net/zt15732625878/article/details/99671586

參考鏈接:https://blog.csdn.net/zt15732625878/article/details/100061528

趙小胖個(gè)人博客

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容