介绍
尤其是我们在学习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的资源资料内容。
JDK5.0 - Java Annotation 介绍 (ppt格式)
JAVA Annotation学习示例 的一个简单示例 有源码 也有相关博客说明
java annotation 手册.doc,java annotation 手册.doc,java annotation 手册.doc
Java Annotation注解技术
Java Annotation的讲解和例子~~~
本文针对java初学者或者annotation初次使用者全面地说明了annotation的使用方法、定义方式、分类。初学者可以通过以上的说明制作简单的annotation程序,但是对于一些高级的annotation应用(例如使用自定义annotation...
Java Annotation详解 Java Annotation详解 Java Annotation详解
struts 拦截器,扩展,以及java annotation 这个是我学习所用,放在此处,作为中转。
JavaAnnotation必须掌握的特性Java开发Java经验技巧共6页.pdf.zip
java annotation手册 关于annotation是什么东西,百度一下会有很多关于它的描述,在这里就不用再copy了,我们还是以一个示例开始吧,在开始示例之前,我们先来一个小故事。
Java Annotation入门,本文初步告诉你怎么使用java anotatin
NULL 博文链接:https://xing-kenny.iteye.com/blog/1338865
如果你想知道java annotation是什么?你可以看看
java annotation 从JDK5.0后提供的功能。里面包含的实例,我是为了学习这个,所以放到这个上面作为中转。从别人博客里面摘取,博客地址里面附有。
javaAnnotation.pdf
Java Annotation学习整理
JavaAnnotation实例.docx