首页 > 代码库 > JAVA Annotation
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 start, 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()); } } } }
JAVA Annotation
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。