Java教程:JAVA自定義注解

注解概念

目前成都創(chuàng)新互聯(lián)公司已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計、青岡網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

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

注解和注釋的不同

  • 注釋注釋是對代碼的說明,給代碼的讀者看,便于幫讀者梳理業(yè)務(wù)邏輯;在程序代碼中經(jīng)??吹降囊訞 開頭的大部分是注解;
  • 注解注解也是對代碼的說明,需要配合工具(解析它的代碼)使用,參與代碼的編譯,給應(yīng)用程序看的;

注解分類

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

JDK注解

JAVA 內(nèi)置注解

  • @Override (標記重寫方法)
  • @Deprecated (標記過時)
  • @SuppressWarnings (忽略警告)
元注解 (注解的注解)
  • @Target (注解的作用目標)
  • @Retention (注解的生命周期)
  • @Document (注解是否被包含在JavaDoc中)
  • @Inherited (是否允許子類集成該注解)

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

請自行百度各個框架的注解詳解

自定義注解

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

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

@Target

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

Java教程:JAVA自定義注解
@Retention

表明該注解的生命周期

Java教程:JAVA自定義注解
@Document

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

@Inherited

表明使用了@Inherited注解的注解,所標記的類的子類也會擁有這個注解

注解格式


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

注解樣例

接下來我們以Spring中的 @Service 注解為例


@Target({ElementType
.TYPE})
// ElementType.TYPE 代表在注解上使用
@Retention(RetentionPolicy.RUNTIME)// RetentionPolicy.RUNTIME 代表運行時使用,可以通過反射獲取到
@Documented//包含在JavaDoc中
@Component//允許通過包掃描的方式自動檢測
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包下,所有的注解默認繼承了Annotation接口,但是它本身不能定義注解。

package java.lang.annotation;

/**
* 所有的注解默認繼承了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? 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{
   .
   .
   .
}

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

執(zhí)行main方法,運行結(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'

本文標題:Java教程:JAVA自定義注解
網(wǎng)頁網(wǎng)址:http://bm7419.com/article0/gigjoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、商城網(wǎng)站定制開發(fā)、網(wǎng)站營銷軟件開發(fā)、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)