詳解java注解相關知識
定義
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、如果注解中有屬性,且沒有定義默認值,那么在使用注解的時候,必須給屬性賦值。
public @interface Annotation {
String name();
int age();
}
public class Test {
@Annotation(name="lxc", age=20)
public void test() {
}
}
但是注解中如果有默認值,在使用注解時,可以不給屬性賦值
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)標記一個注解只能出現(xiàn)在類或者方法上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
public @interface Annotation {
String userName();
MyEnum[] student();
}
(2)標記一個注解可以被反射機制所讀取
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// 標記注解只能出現(xiàn)在類上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 標記注解可以被反射機制所讀取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
String userName();
MyEnum[] student();
}
獲取注解中的屬性值
通過反射機制來獲取。
(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;
// 標記注解只能出現(xiàn)在類上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 標記注解可以被反射機制所讀取
@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;
// 標記注解只能出現(xiàn)在類上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 標記注解可以被反射機制所讀取
@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
}
}
}

到此這篇關于詳解java注解的使用的文章就介紹到這了,更多相關java注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot集成elasticsearch使用圖文詳解
Spring Boot集成Elasticsearch其實非常簡單,這篇文章主要給大家介紹了關于SpringBoot集成elasticsearch使用的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-04-04
詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)
這篇文章主要介紹了詳解Springboot整合ActiveMQ(Queue和Topic兩種模式),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
Java實現(xiàn)將類數(shù)據(jù)逐行寫入CSV文件的方法詳解
這篇文章主要為大家詳細介紹了Java如何實現(xiàn)將類數(shù)據(jù)逐行寫入CSV文件,文中的示例代碼講解詳細,具有一定的參考價值,需要的可以借鑒一下2022-11-11

