java教程之java注解annotation使用方法
1.概述
注解可以定義到方法上,類上,一個(gè)注解相當(dāng)與一個(gè)類,就相當(dāng)于實(shí)例了一個(gè)對(duì)象,加上了注解,就相當(dāng)于加了一個(gè)標(biāo)志。
常用的注解:
@Override:表示重新父類的方法,
這個(gè)也可以判斷是否覆蓋的父類方法,在方法前面加上此語句,如果提示的錯(cuò)誤,那么你不是覆蓋的父類的方法,要是提示的沒有錯(cuò)誤,那么就是覆蓋的父類的方法。
@SuppressWarnings("deprecation"):取消編譯器的警告(例如你使用的方法過時(shí)了)
@Deprecated:在方法的最上邊也上此語句,表示此方法過時(shí),了,或者使用在類上面
import java.util.ArrayList;
import java.util.List;
public class annotationDemo {
/*
* 對(duì)于集合,如果沒有指定存儲(chǔ)的類型,那么就會(huì)有安全警告,
* 如果不想提示安全警告的話,那么就所在類或者方法上添加@SuppressWarnings(參數(shù))
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List list=new ArrayList();
}
}
2.自定義注解
1.格式
權(quán)限 @interface 注解名稱 { }
步驟:
定義注解類--->定義應(yīng)用注解類的類--->對(duì)應(yīng)用注解類的類進(jìn)行反射的類(這個(gè)類可以另外定義,也可以是在應(yīng)用注解類中進(jìn)行測試)
import java.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
//定義此注解保留在字節(jié)碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
@MyAnnotation
// 應(yīng)用定義的注解類
public class ApplyMyAnnotation {
public static void main(String[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的注解類
MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}
2.聲明周期
格式:例如:@Retention(RetentionPolicy.CLASS)
在自定一的注解類上定義周期,@Retention(參數(shù)類型) 參數(shù)類型是RetentionPolicy
RetentionPolicy.CLASS:類文件上,運(yùn)行時(shí)虛擬機(jī)不保留注解
RetentionPolicy.RUNTIME:類文件上,運(yùn)行時(shí)虛擬就保留注解
RetentionPolicy.SOURCE:源文件上,丟棄注解
SuppressWarnings和Override是RetentionPolicy.SOURCE,
Deprecated是在RetentionPolicy.RUNTIME,要向運(yùn)行時(shí)調(diào)用定義的一樣,那么必須是RetentionPolicy.RUNTIME,
默認(rèn)的都是RetentionPolicy.CLASS:
3.指定目標(biāo)
格式:例如:方法上@Target(ElementType.METHOD)
定義的注解可以注解什么成員。如果不聲明此注解,那么就是可以放到任何程序的元素上。
可以是包,接口,參數(shù),方法,局部變量,字段…等。
//定義此注解保留在字節(jié)碼中
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})//可以定義在方法上和類上接口,表示類型
public @interface MyAnnotation {
}
@MyAnnotation
// 應(yīng)用定義的注解類
public class ApplyMyAnnotation {
@MyAnnotation//定義在方法上
public static void main(String[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的注解類
MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}
3.為注解添加屬性
1.類型
注解的屬性置可以是:8個(gè)基本數(shù)據(jù)類型,String,枚舉,注解,Class,數(shù)組類型,
2.注意點(diǎn)
當(dāng)注 解中只有一個(gè)屬性或者是只有一個(gè)屬性需要賦值的話,那么在調(diào)用的時(shí)候,就可以直接寫入,不需要指定屬性名,
當(dāng)注解的屬性是數(shù)組類型并且賦值的時(shí)候只賦值一個(gè)值,那么就可以省略{}.
3.示例
3.1.屬性類型(是String)
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//定義此注解保留在字節(jié)碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color()default "red";//設(shè)置默認(rèn)值是"red"
}
@MyAnnotation("java")
public class ApplyMyAnnotation {
public static void main(String[] args) {
/**
* 這是獲得類上的注解,也可以獲得方法上的注解,下面就以獲得類上的注解為例
*/
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的注解類
MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println("value="+annotation.value());
System.out.println("Color="+annotation.Color());
}
}
}
結(jié)果:
value=java
Color=red
從調(diào)用的程序中,也可以看出,只有一個(gè)屬性可以需要賦值的話,可以省略屬性名。否則@注解類(屬性名=值)
3.2.綜合類型
/*枚舉類*/
public enum Week{
SUN,MON;
}
/**
* 注解類
*/
public @interface annotationText {
String value();
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//定義此注解保留在字節(jié)碼中
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color()default "red";//設(shè)置默認(rèn)值是"red"
Week week() default Week.MON;//枚舉類型
int [] array() default {1,2,3};//數(shù)組類型
annotationText annotation() default @annotationText("MY");//注解類型
Class classDemo() default Integer.class;//Class類型
}
@MyAnnotation(value="java",Color="green",week=Week.SUN,array=5,annotation=@annotationText("YOU"),classDemo=String.class)//數(shù)組array={4,5,6}
public class ApplyMyAnnotation {
public static void main(String[] args) {
/**
* 這是獲得類上的注解,也可以獲得方法上的注解,下面就以獲得類上的注解為例
*/
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判斷此類上是否存在指定的注解類
MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println("value="+annotation.value());
System.out.println("Color="+annotation.Color());
System.out.println("week="+annotation.week());
System.out.println("array長度="+annotation.array()。length);
System.out.println("注解類型值="+annotation.annotation()。value());
System.out.println("Class類型值="+annotation.classDemo());
}
}
}
結(jié)果:
value=java
Color=green
week=SUN
array長度=1
注解類型值=YOU
Class類型值=classjava.lang.String
4.Method上的注解
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
/**
*注解類
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface annotationText{
Stringvalue();
}
publicclassApplyMyAnnotation{
publicstaticvoidmain(String[]args)throwsException{
Methodmethodshow=ApplyMyAnnotation.class.getMethod("show");
annotationTextanno=methodshow.getAnnotation(annotationText.class);
System.out.println(anno.value());
}
@annotationText("java")
publicvoidshow(){
System.out.println("hello");
}
}
結(jié)果:java
- Java注解之Retention、Documented、Inherited介紹
- 5分鐘搞懂java注解@Annotation的具體使用
- Java注解Annotation與自定義注解詳解
- Java注解機(jī)制之Spring自動(dòng)裝配實(shí)現(xiàn)原理詳解
- Java注解@Transactional事務(wù)類內(nèi)調(diào)用不生效問題及解決辦法
- 基于Java注解(Annotation)的自定義注解入門介紹
- 詳解Java注解教程及自定義注解
- 創(chuàng)建自定義的Java注解類的方法
- 深入理解Java注解類型(@Annotation)
- 輕松掌握J(rèn)ava注解,讓編程更智能、更優(yōu)雅
相關(guān)文章
Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格
這篇文章主要為大家詳細(xì)介紹了Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08Java8中Lambda表達(dá)式使用和Stream API詳解
這篇文章主要給大家介紹了關(guān)于Java8中Lambda表達(dá)式使用和Stream API的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java8具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05深入了解Spring Boot2.3.0及以上版本的Liveness和Readiness功能
這篇文章主要介紹了Spring Boot2.3.0及以上版本的Liveness和Readiness功能示例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10EL調(diào)用Java方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
簡單來說,我們在一個(gè)類中的某個(gè)方法,可以使用EL進(jìn)行調(diào)用,這個(gè)能被EL表達(dá)式調(diào)用的方法稱之為EL函數(shù),但是這種方式必須滿足兩點(diǎn)要求,具體哪兩點(diǎn),大家可以參考下本文2017-07-07Spring中使用AOP進(jìn)行事務(wù)管理實(shí)例
這篇文章主要介紹了Spring中使用AOP進(jìn)行事務(wù)管理實(shí)例,當(dāng)在Spring項(xiàng)目中涉及數(shù)據(jù)庫操作時(shí),事務(wù)管理是非常重要的,它可以確保數(shù)據(jù)庫操作的一致性和完整性,Spring提供了強(qiáng)大的事務(wù)管理功能,可以通過聲明式或編程式兩種方式進(jìn)行配置,需要的朋友可以參考下2023-09-09SpringCloud基于Feign的可編程式接口調(diào)用實(shí)現(xiàn)
本文主要介紹了SpringCloud基于Feign的可編程式接口調(diào)用實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Spring Cloud負(fù)載均衡及遠(yuǎn)程調(diào)用實(shí)現(xiàn)詳解
這篇文章主要介紹了Spring Cloud負(fù)載均衡及遠(yuǎn)程調(diào)用實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08