转自:http://blog.csdn.net/x_yp/article/details/6229516
自定义注解:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER}) //用于字段,方法,参数 @Retention(RetentionPolicy.RUNTIME) //在运行时加载到Annotation到JVM中 public @interface Field_Method_Parameter_Annotation { Class<?> type() default void.class; //定义一个具有默认值的Class型成员 String describ(); //定义一个没有默认值的String成员 }
使用注解:
public class AnnotationTest { // 注释字段 @Field_Method_Parameter_Annotation(describ = "字段编号", type = int.class) int id; // 注释字段 @Field_Method_Parameter_Annotation(describ = "字段姓名", type = String.class) String name; public AnnotationTest() { } @Field_Method_Parameter_Annotation(describ = "获得编号", type = int.class) public int getId() { return id; } @Field_Method_Parameter_Annotation(describ = "设置编号") public void setId( // 注释参数 @Field_Method_Parameter_Annotation(describ = "设置编号", type = int.class) int id) { this.id = id; } @Field_Method_Parameter_Annotation(describ = "获得姓名", type = String.class) public String getName() { return name; } @Field_Method_Parameter_Annotation(describ = "设置姓名") public void setName( @Field_Method_Parameter_Annotation(describ = "姓名", type = String.class) String name) { this.name = name; } @Field_Method_Parameter_Annotation(describ = "设置编号和姓名") public void setIdAndName( // 注释参数 @Field_Method_Parameter_Annotation(describ = "设置编号", type = int.class) int id, @Field_Method_Parameter_Annotation(describ = "姓名", type = String.class) String name) { this.id = id; this.name = name; } @Field_Method_Parameter_Annotation(describ = "设置编号和姓名") public void setIdAndName2( // 注释参数 @Field_Method_Parameter_Annotation(describ = "设置编号", type = // int.class) int id, String name) { this.id = id; this.name = name; } }
处理注解:
import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Test { public static void main(String[] args) throws ClassNotFoundException { System.out.println("********字段的Annotation*************"); Field[] declaredFields = AnnotationTest.class.getDeclaredFields(); //获得所有的字段 for(int i=0;i<declaredFields.length;i++){ Field field = declaredFields[i]; System.out.print("字段" + field.getName() + "的Annotation:"); //获得字段描述 //查看是否具有指定类型的注释: if(field.isAnnotationPresent(Field_Method_Parameter_Annotation.class)){ Field_Method_Parameter_Annotation fa = field.getAnnotation(Field_Method_Parameter_Annotation.class); System.out.print(" " + fa.describ()); //获得字段描述 System.out.println(" " + fa.type()); //获得字段类型 } } System.out.println("********方法的Annotation********"); Method [] methods = AnnotationTest.class.getDeclaredMethods(); //获得所有的方法 for(int i=0;i<methods.length;i++){ Method method = methods[i]; //查看是否指定注释: System.out.println("方法" + method.getName() + "的Annotation:"); if(method.isAnnotationPresent(Field_Method_Parameter_Annotation.class)){ Field_Method_Parameter_Annotation ma = method.getAnnotation(Field_Method_Parameter_Annotation.class); System.out.print(" " + ma.describ()); //获得方法描述 System.out.println(" " + ma.type()); //获得方法类型 } System.out.println("方法" + method.getName() + "的参数的Annotation"); //获得所有参数注解 Annotation[][]parameterAnnotations = method.getParameterAnnotations(); if (parameterAnnotations.length == 0) { System.out.println("方法没有参数"); } for(int j = 0;j < parameterAnnotations.length;j++){ int length = parameterAnnotations[j].length; if(length==0){ System.out.println("没有添加Annotation参数"); }else{ for(int k=0;k<length;k++){ //获得指定的注释: Field_Method_Parameter_Annotation pa = (Field_Method_Parameter_Annotation)parameterAnnotations[j][k]; System.out.print(" " + pa.describ()); //获得参数描述 System.out.println(" " + pa.type()); //获得参数类型 } } } } } }
相关推荐
主要介绍了java注解annotation使用方法,注解可以定义到方法上,类上,一个注解相当与一个类,就相当于实例了一个对象,加上了注解,就相当于加了一个标志
本文针对java初学者或者annotation初次使用者全面地说明了annotation的使用方法、定义方式、分类。初学者可以通过以上的说明制作简单的annotation程序,但是对于一些高级的annotation应用(例如使用自定义annotation...
用注解的方式定义一个Servlet,就不用在web.xml中配置了。Servlet的访问URL是Servlet的必选属性,可以选择使用urlPatterns或者value定义。 像上面的AnnotationServlet可以描述成@WebServlet(name="Annotation...
java或Java框架中常用的注解及其作用详解:Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。 Java 语言中的类、方法、变量、参数和包等都可以被标注。和 Javadoc 不同,Java 标注可以通过...
针对java 初学者或者annotation 初次使用者全面地说明了annotation 的使用方法、定义 方式、分类。初学者可以通过以上的说明制作简单的annotation 程序,但是对于一些高级的 annotation 应用(例如使用自定义...
注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行...
Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。注解...
主要介绍了Java编程中Annotation注解对象的使用方法,注解以"@注解名"的方式被编写,与类、接口、枚举是在同一个层次,需要的朋友可以参考下
本文针对java初学者或者annotation初次使用者全面地 说明了annotation 的使用方法、定义方式、分类。初学者可以通过以上的说明制作简单的 annotation程序,但是对于一些高级的 annotation应用
我们在项目开发过程中,可能会对一些公用方法抽成工具类进行使用。如果我们把这些方法再抽成注解的形式,在一些业务情况下是否更灵活一些呢? 示例是纯净的,只引入了需要的架包,启动后直接访问:ip:port/user/test ...
一、枚举类的使用: 1.枚举类的理解:类的对象只有有限个,确定的。我们称此为枚举类 2.当需要定义一组常量时,强烈建议使用枚举类。...五、注解(Annotation)概述 二、常见的Annotation 示例: 六、JDK中的元
Java语言中的类、方法、变量、参数和包等都可以被标注。和Javadoc不同,Java标注可以通过反射获取标注内容。在编译生成类文件时,标注可以被嵌入到字节码中。Java虚拟机可以保留标注内容,在运行时可以获取到标注...
@Target:用于描述注解的使用范围,如果自定义注解不存在@Target,则表示该注解可以使用在任何程序元素之上。接收参数ElementType,其值如下: /**接口、类、枚举、注解**/ ElementType.TYPE /**字段、枚举的常量**/...
对于Java开发人员来说,在编写代码时,除了源程序以外,我们还会使用Javadoc标签对类、方法或成员变量进行注释,以便使用Javadoc工具生成和源代码配套的Javadoc文档。这些@param、@return等Javadoc标签是注解标签...
注解(Annotation) 相当于一种标记,在程序中加了注解就等于为程序打上了某种标记。以后,java编译器、开发工具和其它程序可以使用反射来了解你的类及各种元素上有无某种标记,找到有什么标记,就去干相应的事情。...
Java 注解(Annotation) JDK 从Java源码中提取的所有注解 注解 版本 说明 @WebParam 表示方法的参数 @Oneway 表示为只有输入消息而没有输出消息的 Web Service 单向操作 @WebResult 表示方法的...
Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。 Java 语言中的类、方法、变量、参数和包等都可以被标注。Java标注可以通过反射获取标注内容。在编译器生成类文件时,标注可以被嵌入到字节...
java注解(Annotation)[ˌ ænəˈ teɪʃn],又称java标注,也被称为元数据(关于数据的数据,描述数据的数据)(Metadata)[ˈ metədeɪtə],可用于修饰或者解释包、类、方法、属性、构造器,局部变量等数据信息...
java8流源码ls-注解 ls-annotation 是一个 java 字节码反编译器; 它提取并打印使用 java 注释的类、方法和字段的定义。 这个工具的目的是让带有相互依赖的注解的声明变得有意义,这种依赖的一个例子是 Spring Boot ...
相信大家对注解应该并不陌生,在现在信息飞速发展的年代,各种优秀的框架或许都离不开注解的使用,像我们在实现接口一个方法时,也会有@Override注解。注解说白了就是对程序做出解释,与我们在方法、类上的注释没有...