Java元註解,簡單案例

來源:互聯網
上載者:User

標籤:swa   dem   win   學習   oid   san   pwd   not   time   

    • 【註解】

程式中有 注釋 和註解

* 注釋:給開發人員.

* 註解:給電腦看的.

 

    註解使用:學習架構支援註解開發.

    • 【JDK提供的註解】

@Override :描述方法的重寫.

@SuppressWarnings :壓制警告.

@Deprecated :標記過時.

    • 自訂註解:

定義一個類:class

定義一個借口:interface

定義一個枚舉:enum

定義一個註解:@interface

    用法:

 

@interface MyAnno1{    }帶有屬性的註解:@interface MyAnno2{    int a() default 1;    String b();    // 註解屬性的類型:基礎資料型別 (Elementary Data Type),字串類型String,Class,註解類型,枚舉類型,以及以上類型的一維數組.    // Date d();    Class clazz();    MyAnno3 m3(); // 註解    Color c(); // 枚舉    String[] arrs();    }@MyAnno4("aaa") // 如果屬性名稱為value 那麼使用的時候 value可以省略(只出現這一個value的屬性情況下).public class AnnotationDemo3 {}@interface MyAnno4{    String value();    int a() default 1;}

 

 

【自訂註解案例】

  註解類

 1 package com.xujingyang.annotation; 2  3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7  8 //保留到運行時 9 @Retention(RetentionPolicy.RUNTIME)10 //只能作用在方法上11 @Target(ElementType.METHOD)12 public @interface MyTest {13     14 }

測試註解類

 1 package com.xujingyang.annotation; 2  3 public class Test { 4     @MyTest 5     public void f1(){ 6         System.out.println("f1方法執行了~~~~"); 7     } 8      9     public void f2(){10         System.out.println("f2方法執行了~~~~");11     }12     13     @MyTest14     public void f3(){15         System.out.println("f3方法執行了~~~~");16     }17 }

主測試類別

 1 package com.xujingyang.annotation; 2  3 import java.lang.reflect.Method; 4  5 public class Main { 6     public static void main(String[] args) throws Exception{ 7         Class clazz=Test.class; 8          9         Method[] methods = clazz.getMethods();10         for (Method method : methods) {11 //            method.invoke(clazz.newInstance());12 //            System.out.println(method.getName());13             boolean b = method.isAnnotationPresent(MyTest.class);//判斷是否有添加此註解14             if(b){15                 method.invoke(clazz.newInstance());16             }17         }18     }19 }

列印結果

 

 

案例二,使用註解方式載入擷取JDBC串連的參數

  註解類

 1 package com.xujingyang.jdbc; 2  3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 //保留到運行時 8 @Retention(RetentionPolicy.RUNTIME) 9 //只能在方法上添加此註解10 @Target(ElementType.METHOD)11 public @interface JDBCInfo {12     //定義載入資料庫的幾種屬性,可以用default關鍵字賦預設值13     String DriverClass() default "com.mysql.jdbc.Driver";14     String Url();15     String User() default "root";16     String Pwd() default "root";17 }

 測試類別

 1 package com.xujingyang.jdbc; 2  3 import java.lang.reflect.Method; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6  7 public class Conn { 8     public static void main(String[] args) throws Exception { 9         System.out.println(getConnection());//列印串連地址,說明成功10     }11     12     @JDBCInfo(Url="jdbc:mysql://localhost:3306/day16")13     public static Connection getConnection() throws Exception{14         //載入類的位元組碼對象15         Class<Conn> clazz=Conn.class;16         17         //擷取此方法18         Method method = clazz.getMethod("getConnection");19         20         //擷取註解對象21         JDBCInfo info = method.getAnnotation(JDBCInfo.class);22         23         //擷取各個已賦值的屬性24         String driverClass = info.DriverClass();25         String url = info.Url();26         String user = info.User();27         String pwd = info.Pwd();28         29         //註冊驅動30         Class.forName(driverClass);31         32         //獲得串連33         return DriverManager.getConnection(url, user, pwd);34     }35 }

 

列印結果

 

 

註解的簡單用法就記這麼多了,更深入的研究待以後來搞

Java元註解,簡單案例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.