spring mvc 小記(一):spring註解與java原註解
阿新 • • 發佈:2019-01-23
使用spring已有2年之久,卻還是停留在使用階段,感覺這麼下去不是辦法,所以還是想往深處一探究竟。
今天無意中查詢到java註解,才瞭解到原來那些框架裡的註解全是基於java所提供的元註解上編寫的,也就是說,我們自己也可以自定義註解。參考資料出處:http://blog.sina.com.cn/s/blog_93dc666c0101gzn5.html
首先,新建一個自定義的註解類
其中各引數具體的含義可以自行去網上查詢,現在註解類已建完,可以直接拿來去註解別的類或方法了,新建一個測試方法<span style="font-size:18px;">import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR}) @Retention(RetentionPolicy.RUNTIME) public @interface TestA { String name(); int id() default 0; Class gid(); }</span>
將註解放上去後,沒報錯,好像也沒啥特別的效果,這個時候就可以想想spring之類的框架了,比如為什麼URL可以執行被註解的方法,大概情況就是,通過註解找到被註解的方法,然後通過java反射去執行方法,那麼我們也可以試試通過註解找到這些被註解的元素:<span style="font-size:18px;">import java.util.HashMap; import java.util.Map; @TestA(name="type",gid=Long.class) //類成員註解 public class UserAnnotation { @TestA(name="param",id=1,gid=Long.class) //類成員註解 private Integer age; @TestA (name="construct",id=2,gid=Long.class)//構造方法註解 public UserAnnotation(){ } @TestA(name="public method",id=3,gid=Long.class) //類方法註解 public void a(){ Map m = new HashMap(0); } @TestA(name="protected method",id=4,gid=Long.class) //類方法註解 protected void b(){ Map m = new HashMap(0); } @TestA(name="private method",id=5,gid=Long.class) //類方法註解 private void c(){ Map m = new HashMap(0); } }</span>
執行main方法,看到這個被註解的變數打印出來了,邏輯是查詢所有變數,判斷被註解變數,列印。<span style="font-size:18px;">public class ParseAnnotation { public static void parseTypeAnnotation() throws ClassNotFoundException { Class clazz = Class.forName("com.tan.Test.UserAnnotation"); Annotation[] annotations = clazz.getAnnotations(); for (Annotation annotation : annotations) { TestA testA = (TestA)annotation; System.out.println("id= "+testA.id()+"; name= "+testA.name()+"; gid = "+testA.gid()); } } public static void parseMethodAnnotation(){ Method[] methods = UserAnnotation.class.getDeclaredMethods(); for (Method method : methods) { boolean hasAnnotation = method.isAnnotationPresent(TestA.class); if (hasAnnotation) { TestA annotation = method.getAnnotation(TestA.class); System.out.println("method = " + method.getName() + " ; id = " + annotation.id() + " ; description = " + annotation.name() + "; gid= "+annotation.gid()); } } } public static void parseConstructAnnotation(){ Constructor[] constructors = UserAnnotation.class.getConstructors(); for (Constructor constructor : constructors) { boolean hasAnnotation = constructor.isAnnotationPresent(TestA.class); if (hasAnnotation) { TestA annotation =(TestA) constructor.getAnnotation(TestA.class); System.out.println("constructor = " + constructor.getName() + " ; id = " + annotation.id() + " ; description = " + annotation.name() + "; gid= "+annotation.gid()); } } } //引數註解解析 public static void parseParameterAnnotation() { Field[] fields = UserAnnotation.class.getDeclaredFields(); for(int i=0;i<fields.length;i++){ boolean hasAnnotation = fields[i].isAnnotationPresent(TestA.class); if(hasAnnotation){ TestA annotation = fields[i].getAnnotation(TestA.class); System.out.println("變數名:"+fields[i].getName()+"註解:"+annotation.name()+"==="+annotation.id()+"=="+annotation.gid()); } } } public static void main(String[] args) throws ClassNotFoundException { // parseTypeAnnotation(); // parseMethodAnnotation(); // parseConstructAnnotation(); parseParameterAnnotation(); } }</span>