本文目錄一覽:
java註解是怎麼實現的
註解的使用一般是與java的反射一起使用,下面是一個例子
註解相當於一種標記,在程序中加了註解就等於為程序打上了某種標記,沒加,則等於沒有某種標記,以後,javac編譯器,開發工具和其他程序可以用反射來了解你的類及各種元素上有無何種標記,看你有什麼標記,就去干相應的事。標記可以加在包,類,欄位,方法,方法的參數以及局部變數上。
自定義註解及其應用
1)、定義一個最簡單的註解
public @interface MyAnnotation {
//……
}
2)、把註解加在某個類上:
@MyAnnotation
public class AnnotationTest{
//……
}
以下為模擬案例
自定義註解@MyAnnotation
1 package com.ljq.test;
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 * 定義一個註解
10 *
11 *
12 * @author jiqinlin
13 *
14 */
15 //Java中提供了四種元註解,專門負責註解其他的註解,分別如下
16
17 //@Retention元註解,表示需要在什麼級別保存該注釋信息(生命周期)。可選的RetentionPoicy參數包括:
18 //RetentionPolicy.SOURCE: 停留在java源文件,編譯器被丟掉
19 //RetentionPolicy.CLASS:停留在class文件中,但會被VM丟棄(默認)
20 //RetentionPolicy.RUNTIME:內存中的位元組碼,VM將在運行時也保留註解,因此可以通過反射機制讀取註解的信息
21
22 //@Target元註解,默認值為任何元素,表示該註解用於什麼地方。可用的ElementType參數包括
23 //ElementType.CONSTRUCTOR: 構造器聲明
24 //ElementType.FIELD: 成員變數、對象、屬性(包括enum實例)
25 //ElementType.LOCAL_VARIABLE: 局部變數聲明
26 //ElementType.METHOD: 方法聲明
27 //ElementType.PACKAGE: 包聲明
28 //ElementType.PARAMETER: 參數聲明
29 //ElementType.TYPE: 類、介面(包括註解類型)或enum聲明
30
31 //@Documented將註解包含在JavaDoc中
32
33 //@Inheried允許子類繼承父類中的註解
34
35
36 @Retention(RetentionPolicy.RUNTIME)
37 @Target({ElementType.METHOD, ElementType.TYPE})
38 public @interface MyAnnotation {
39 //為註解添加屬性
40 String color();
41 String value() default “我是林計欽”; //為屬性提供默認值
42 int[] array() default {1, 2, 3};
43 Gender gender() default Gender.MAN; //添加一個枚舉
44 MetaAnnotation metaAnnotation() default @MetaAnnotation(birthday=”我的出身日期為1988-2-18″);
45 //添加枚舉屬性
46
47 }
註解測試類AnnotationTest
1 package com.ljq.test;
2
3 /**
4 * 註解測試類
5 *
6 *
7 * @author jiqinlin
8 *
9 */
10 //調用註解並賦值
11 @MyAnnotation(metaAnnotation=@MetaAnnotation(birthday = “我的出身日期為1988-2-18″),color=”red”, array={23, 26})
12 public class AnnotationTest {
13
14 public static void main(String[] args) {
15 //檢查類AnnotationTest是否含有@MyAnnotation註解
16 if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
17 //若存在就獲取註解
18 MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
19 System.out.println(annotation);
20 //獲取註解屬性
21 System.out.println(annotation.color());
22 System.out.println(annotation.value());
23 //數組
24 int[] arrs=annotation.array();
25 for(int arr:arrs){
26 System.out.println(arr);
27 }
28 //枚舉
29 Gender gender=annotation.gender();
30 System.out.println(“性別為:”+gender);
31 //獲取註解屬性
32 MetaAnnotation meta=annotation.metaAnnotation();
33 System.out.println(meta.birthday());
34 }
35 }
36 }
枚舉類Gender,模擬註解中添加枚舉屬性
1 package com.ljq.test;
2 /**
3 * 枚舉,模擬註解中添加枚舉屬性
4 *
5 * @author jiqinlin
6 *
7 */
8 public enum Gender {
9 MAN{
10 public String getName(){return “男”;}
11 },
12 WOMEN{
13 public String getName(){return “女”;}
14 }; //記得有「;」
15 public abstract String getName();
16 }
註解類MetaAnnotation,模擬註解中添加註解屬性
1 package com.ljq.test;
2
3 /**
4 * 定義一個註解,模擬註解中添加註解屬性
5 *
6 * @author jiqinlin
7 *
8 */
9 public @interface MetaAnnotation {
10 String birthday();
11 }
java 自定義的註解有什麼作用
自定義註解,可以應用到反射中,比如自己寫個小框架。
如實現實體類某些屬性不自動賦值,或者驗證某個對象屬性完整性等
本人自己用過的驗證屬性值完整性:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreProperty {
}
然後實體類中:
public class TarResearch implements Serializable{
@IgnoreProperty
private static final long serialVersionUID = 1L;
@IgnoreProperty
private Integer researchId;
@IgnoreProperty
private TarUser userId;
private String version;
private String grade;
….
}
然後action類中
// 驗證數據完整性
ClassTarResearch userClass = TarResearch .class;
Field[] field = userClass.getDeclaredFields();
for (int i = 0; i field.length; i++) {
if (field[i].getAnnotation(IgnoreProperty.class) != null) {
continue;
}
String fie = field[i].getName().substring(0, 1).toUpperCase()
+ field[i].getName().substring(1);
Method method = userClass.getMethod(“get” + fie);
Object obj = method.invoke(u);
if (obj == null) {
sendResponseMsg(response, “數據錯誤”);
return null;
}
}
Java自定義註解註解實現實體類與資料庫表欄位的映射
這個
按照我的理解
首先自定義註解要有自己的編譯解釋方法的
在這個便已解釋方法中估計需要連接資料庫(當然最基本的jdbc什麼的)
當然連接資料庫什麼的涉及到配置文件
通過jdbc獲取到資料庫信息
把註解中的參數與資料庫中的信息關聯(簡單的就可以存成ListMap等方式)由於註解的參數一般是類名。你可以
用反射的方式或其他方式(比如位元組碼什麼的)來處理獲取類的屬性
然後
我感覺關聯完了
就沒啥然後了
感覺主要是在註解的編譯解釋方法中做寫東西
當然
提高效率也可以用資料庫連接池什麼的
原創文章,作者:QIXE,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/149516.html