JAVA Annotation
介紹
尤其是我們在學習Spring時,都繞不開Annotation,而且使用的非常頻繁,並且給我們帶來很大的便利, 所以我們有必要了解JAVA Annotation。
在此,我們自定義兩個Annotation:一個是Class相關的Annotation;另一個是Method相關的Annotation。然後,我們寫一個類來使用那兩個Annotation,這樣我們就能比較快速的了解Annotation。
自定義一個Class相關的Annotation:Message.java
package shuai.study.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Message { String mess(); }
------------------------------------------------------------------------------------------------------------------------------------------------
對於上麵的Annotation,你可能不清楚@Target(ElementType.TYPE) 和 @Retention(RetentionPolicy.RUNTIME) 是什麼意思,那麼我們就可以看一下JDK的源碼:
public enum ElementType { /** Class, interface (including annotation type), or enum declaration */ TYPE, /** Field declaration (includes enum constants) */ FIELD, /** Method declaration */ METHOD, /** Parameter declaration */ PARAMETER, /** Constructor declaration */ CONSTRUCTOR, /** Local variable declaration */ LOCAL_VARIABLE, /** Annotation type declaration */ ANNOTATION_TYPE, /** Package declaration */ PACKAGE }
public enum RetentionPolicy { /** * Annotations are to be discarded by the compiler. */ SOURCE, /** * Annotations are to be recorded in the class file by the compiler * but need not be retained by the VM at run time. This is the default * behavior. */ CLASS, /** * Annotations are to be recorded in the class file by the compiler and * retained by the VM at run time, so they may be read reflectively. * * @see java.lang.reflect.AnnotatedElement */ RUNTIME }
JDK的源碼就是NB,寫得非常清楚!
------------------------------------------------------------------------------------------------------------------------------------------------另一個Method相關的Annotation:Employee.java
package shuai.study.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Employee { String name() default "XXX"; int budget() default 0; }
再寫一個類Company.java,來使用上麵的兩個Annotation:
package shuai.study.annotation; @Message(mess = "NB company publish month star, please pay attention to this message") public class Company { @Employee(name = "SB", budget = 6666) public void monthStar() { System.out.println("向錢看,向厚賺!"); } }
最後再來一個測試啟動類AnnotationTest.java:
package shuai.study.annotation; import java.lang.reflect.Method; public class AnnotationTest { public static void main(String[] args) throws Exception { Class<?> companyClass = Class.forName("shuai.study.annotation.Company"); if (companyClass.isAnnotationPresent(Message.class)) { Message message = companyClass.getAnnotation(Message.class); System.out.println("=== " + message.mess() + " ==="); } Method[] methods = companyClass.getMethods(); int length = methods.length; for (int index = 0; index < length; index++) { if (methods[index].isAnnotationPresent(Employee.class)) { Employee employee = methods[index].getAnnotation(Employee.class); System.out.println("Employee Name: " + employee.name()); System.out.println("Employee Budget: " + employee.budget()); } } } }
最後更新:2017-04-03 05:39:33