欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解java注解相關(guān)知識

 更新時間:2021年06月25日 11:50:02   作者:紅旗下的小兵  
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著java注解的使用展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下

定義

 1、如果注解中有屬性,那么必須給屬性賦值。

package com.lxc.Test;
 
// 定義一個注解
public @interface Annotation {
    String name(); // 看似name像一個方法,實際上我們把name稱為屬性
}

使用上邊注解:

package com.lxc.Test;
 
public class Test {
    @Annotation(name="lxc")
    public void test() {
    }
}

2、如果注解中有屬性,且沒有定義默認(rèn)值,那么在使用注解的時候,必須給屬性賦值。

public @interface Annotation {
    String name();
    int age();
}
public class Test {
    @Annotation(name="lxc", age=20)
    public void test() {
    }
}

但是注解中如果有默認(rèn)值,在使用注解時,可以不給屬性賦值

public class Test {
    @Annotation(name="lxc")
    public void test() {
    }
}
public @interface Annotation {
    String name();
    String password() default "123";
}

3、value() 屬性

如果注解中的一個屬性名是value,且有且只有一個value(),在使用注解的時候,屬性名可以省略

public class Test {
    @Annotation("lxc")
    public void test() {
    }
}
public @interface Annotation {
    String value();
    String password() default "123";
}

注解中屬性的類型有哪些

byte、short、int、float、double、boolean、char、String、Class、枚舉

數(shù)組:

如果數(shù)組屬性中有一個元素,那么數(shù)組的大括號可以省略:

public @interface Annotation {
    String[] name();
}
public class Test {
    // @Annotation(name={"lxc"}) // 寫法一
    @Annotation(name="lxc") // 寫法二
    public void test() {
    }
}

枚舉:

public enum MyEnum {
    className, name, age,
}
public @interface Annotation {
    String[] name();
    MyEnum[] student();
}
public class Test {
    @Annotation(name="lxc", student = {MyEnum.className, MyEnum.age})
    public void test() {
    }
}

注解如何使用:

(1)標(biāo)記一個注解只能出現(xiàn)在類或者方法上

@Target(value = {ElementType.TYPE, ElementType.METHOD})
public @interface Annotation {
    String userName();
    MyEnum[] student();
}

(2)標(biāo)記一個注解可以被反射機(jī)制所讀取

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
// 標(biāo)記注解只能出現(xiàn)在類上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 標(biāo)記注解可以被反射機(jī)制所讀取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
    String userName();
    MyEnum[] student();
}

獲取注解中的屬性值

通過反射機(jī)制來獲取。

(1)獲取類上邊注解的屬性:

注解類:

package com.lxc.Test;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
// 標(biāo)記注解只能出現(xiàn)在類上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 標(biāo)記注解可以被反射機(jī)制所讀取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
    String userName() default "呂星辰";
}

使用注解類:

// myAnnotation 
@Annotation(userName = "哈哈")
public class myAnnotation {
}

獲取注解類中 的屬性:

package com.lxc.Test;
/**
 * c.isAnnotationPresent(注解類.class) : 判斷一個類上是否有注解,返回true、false
 * c.getAnnotation(注解類.class) : 獲取注解類的實例
 *
 */
public class Test {
    public static void main(String[] args) throws Exception{
        Class c = Class.forName("com.lxc.Test.myAnnotation");
        System.out.println(c.isAnnotationPresent(Annotation.class));
        // 判斷一個類是否有:Annotation這個注解
        if(c.isAnnotationPresent(Annotation.class)) {
            // 獲取注解對象
            Annotation ann = (Annotation) c.getAnnotation(Annotation.class);
            // 通過注解對象獲取屬性值
            System.out.println(ann.userName());
        }
    }
}

(2)獲取類中方法上邊注解的屬性:

注解類:

package com.lxc.Test;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
// 標(biāo)記注解只能出現(xiàn)在類上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 標(biāo)記注解可以被反射機(jī)制所讀取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
    String userName();
    String password();
}

在方法上使用注解及獲取方法上邊的注解:
分析:想獲取方法上的注解,首先需要獲取該方法,獲取該方法的前提,獲取該方法的類:

package com.lxc.Test;
 
import java.lang.reflect.Method;
 
public class UserAnnotation {
    @Annotation(userName = "lxc", password = "123")
    public void getUser() {}
 
    public static void main(String[] args) throws Exception{
        // 通過反射獲取類
        Class c = Class.forName("com.lxc.Test.UserAnnotation");
        // 通過反射獲取類中的方法
        Method getUserMethod = c.getDeclaredMethod("getUser");
        // 判斷方法是否有 Annotation 這個注解
        if(getUserMethod.isAnnotationPresent(Annotation.class)) {
            Annotation ann = getUserMethod.getAnnotation(Annotation.class);
            System.out.println(ann.userName()); // lxc
            System.out.println(ann.password()); // 123
        }
    }
}

到此這篇關(guān)于詳解java注解的使用的文章就介紹到這了,更多相關(guān)java注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot集成elasticsearch使用圖文詳解

    SpringBoot集成elasticsearch使用圖文詳解

    Spring Boot集成Elasticsearch其實非常簡單,這篇文章主要給大家介紹了關(guān)于SpringBoot集成elasticsearch使用的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • springboot2.x引入feign踩的坑及解決

    springboot2.x引入feign踩的坑及解決

    這篇文章主要介紹了springboot2.x引入feign踩的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java中的MapStruct知識點總結(jié)

    Java中的MapStruct知識點總結(jié)

    這篇文章主要介紹了Java中的MapStruct知識點總結(jié),MapStruct是一個Java注解處理器,用于生成類型安全的映射代碼,它可以自動處理源對象和目標(biāo)對象之間的映射,減少了手動編寫重復(fù)的映射代碼的工作量,需要的朋友可以參考下
    2023-10-10
  • 詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)

    詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)

    這篇文章主要介紹了詳解Springboot整合ActiveMQ(Queue和Topic兩種模式),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • SpringBoot自動配置原理詳解

    SpringBoot自動配置原理詳解

    SpringBoot的誕生就是為了簡化Spring中繁瑣的XML配置,其本質(zhì)依然還是Spring框架,使用SpringBoot之后可以不使用任何XML配置來啟動一個服務(wù),使得我們在使用微服務(wù)架構(gòu)時可以更加快速的建立一個應(yīng)用。本文將為具體介紹一下SpringBoot的原理,需要的可以參考一下
    2021-12-12
  • 解析Java編程之Synchronized鎖住的對象

    解析Java編程之Synchronized鎖住的對象

    這篇文章主要介紹了解析Java編程之Synchronized鎖住的對象,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • JVM中的GC初識

    JVM中的GC初識

    GC(Garbage Collection)稱之為垃圾回收,是對內(nèi)存中的垃圾對象,采用一定的算法進(jìn)行內(nèi)存回收的一個動作,這篇文章主要介紹了JVM中的GC初識,需要的朋友可以參考下
    2022-05-05
  • Spring整合redis的操作代碼

    Spring整合redis的操作代碼

    這篇文章主要介紹了Spring整合redis的操作代碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • Java實現(xiàn)將類數(shù)據(jù)逐行寫入CSV文件的方法詳解

    Java實現(xiàn)將類數(shù)據(jù)逐行寫入CSV文件的方法詳解

    這篇文章主要為大家詳細(xì)介紹了Java如何實現(xiàn)將類數(shù)據(jù)逐行寫入CSV文件,文中的示例代碼講解詳細(xì),具有一定的參考價值,需要的可以借鑒一下
    2022-11-11
  • 詳解Java打包鏡像部署

    詳解Java打包鏡像部署

    這篇文章主要介紹了Java打包鏡像部署,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-11-11

最新評論