詳解java注解相關(guān)知識(shí)
定義
1、如果注解中有屬性,那么必須給屬性賦值。
package com.lxc.Test;
// 定義一個(gè)注解
public @interface Annotation {
String name(); // 看似name像一個(gè)方法,實(shí)際上我們把name稱(chēng)為屬性
}
使用上邊注解:
package com.lxc.Test;
public class Test {
@Annotation(name="lxc")
public void test() {
}
}

2、如果注解中有屬性,且沒(méi)有定義默認(rèn)值,那么在使用注解的時(shí)候,必須給屬性賦值。
public @interface Annotation {
String name();
int age();
}
public class Test {
@Annotation(name="lxc", age=20)
public void test() {
}
}
但是注解中如果有默認(rèn)值,在使用注解時(shí),可以不給屬性賦值
public class Test {
@Annotation(name="lxc")
public void test() {
}
}
public @interface Annotation {
String name();
String password() default "123";
}
3、value() 屬性
如果注解中的一個(gè)屬性名是value,且有且只有一個(gè)value(),在使用注解的時(shí)候,屬性名可以省略
public class Test {
@Annotation("lxc")
public void test() {
}
}
public @interface Annotation {
String value();
String password() default "123";
}
注解中屬性的類(lèi)型有哪些
byte、short、int、float、double、boolean、char、String、Class、枚舉
數(shù)組:
如果數(shù)組屬性中有一個(gè)元素,那么數(shù)組的大括號(hào)可以省略:
public @interface Annotation {
String[] name();
}
public class Test {
// @Annotation(name={"lxc"}) // 寫(xiě)法一
@Annotation(name="lxc") // 寫(xiě)法二
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)記一個(gè)注解只能出現(xiàn)在類(lèi)或者方法上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
public @interface Annotation {
String userName();
MyEnum[] student();
}
(2)標(biāo)記一個(gè)注解可以被反射機(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)在類(lèi)上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 標(biāo)記注解可以被反射機(jī)制所讀取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
String userName();
MyEnum[] student();
}
獲取注解中的屬性值
通過(guò)反射機(jī)制來(lái)獲取。
(1)獲取類(lèi)上邊注解的屬性:
注解類(lèi):
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)在類(lèi)上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 標(biāo)記注解可以被反射機(jī)制所讀取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
String userName() default "呂星辰";
}
使用注解類(lèi):
// myAnnotation
@Annotation(userName = "哈哈")
public class myAnnotation {
}
獲取注解類(lèi)中 的屬性:
package com.lxc.Test;
/**
* c.isAnnotationPresent(注解類(lèi).class) : 判斷一個(gè)類(lèi)上是否有注解,返回true、false
* c.getAnnotation(注解類(lèi).class) : 獲取注解類(lèi)的實(shí)例
*
*/
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));
// 判斷一個(gè)類(lèi)是否有:Annotation這個(gè)注解
if(c.isAnnotationPresent(Annotation.class)) {
// 獲取注解對(duì)象
Annotation ann = (Annotation) c.getAnnotation(Annotation.class);
// 通過(guò)注解對(duì)象獲取屬性值
System.out.println(ann.userName());
}
}
}

(2)獲取類(lèi)中方法上邊注解的屬性:
注解類(lèi):
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)在類(lèi)上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 標(biāo)記注解可以被反射機(jī)制所讀取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
String userName();
String password();
}
在方法上使用注解及獲取方法上邊的注解:
分析:想獲取方法上的注解,首先需要獲取該方法,獲取該方法的前提,獲取該方法的類(lèi):
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{
// 通過(guò)反射獲取類(lèi)
Class c = Class.forName("com.lxc.Test.UserAnnotation");
// 通過(guò)反射獲取類(lèi)中的方法
Method getUserMethod = c.getDeclaredMethod("getUser");
// 判斷方法是否有 Annotation 這個(gè)注解
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot結(jié)合Redis哨兵模式的實(shí)現(xiàn)示例
這篇文章主要介紹了SpringBoot結(jié)合Redis哨兵模式的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
解決springcloud中Feign導(dǎo)入依賴(lài)為unknow的情況
這篇文章主要介紹了解決springcloud中Feign導(dǎo)入依賴(lài)為unknow的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java中ArrayBlockingQueue和LinkedBlockingQueue
這篇文章主要介紹了Java中ArrayBlockingQueue和LinkedBlockingQueue,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09
Java 時(shí)間格式轉(zhuǎn)換之impleDateFormat與Data API解析與使用
想必大家對(duì) SimpleDateFormat 并不陌生。SimpleDateFormat 是 Java 中一個(gè)非常常用的類(lèi),他是以區(qū)域敏感的方式格式化和解析日期的具體類(lèi)。 它允許格式化 (date -> text)、語(yǔ)法分析 (text -> date)和標(biāo)準(zhǔn)化2021-11-11
Spring Boot中使用Spring-Retry重試框架的實(shí)現(xiàn)
本文主要介紹了Spring Boot中使用Spring-Retry重試框架的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
利用Java實(shí)現(xiàn)解析網(wǎng)頁(yè)中的內(nèi)容
這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言做一個(gè)解析指定網(wǎng)址的網(wǎng)頁(yè)內(nèi)容小應(yīng)用,文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的可以嘗試下2022-10-10

