java注解实例
2016-02-22 21:54
579 查看
【0】README
0.1)本文idea 转自 http://blog.csdn.net/lifetragedy/article/details/7394910
0.2) java 注解的理论基础知识,参见 http://blog.csdn.net/pacosonswjtu/article/details/50719361
0.3)for complete source code about java annotation , please visit https://github.com/pacosonTang/core-java-volume/tree/master/coreJavaAdvanced/chapter10/annotation
【1】自定义Annotation概念篇
1.1)来看一个最简单的annotation
1.1.1) Annotation 需要声明为@interface这样的东西
1.1.2) @Target(ElementType.TYPE)
代码这个annotation必须且一定要注释在什么语句上面
ElementType.TYPE代表此Annotation必须声明在public class Student{…}的上面,而不能写在任何的method{}(方法)或者是field(属性)的上方。
@Target: 表示该注解可以用于什么地方。可用ElementType枚举类型主要有:(干货——@Target注解的作用)
TYPE : 类、接口或enum声明
FIELD: 域(属性)声明
METHOD: 方法声明
PARAMETER: 参数声明
CONSTRUCTOR: 构造方法声明
LOCAL_VARIABLE:局部变量声明
ANNOTATION_TYPE:注释类型声明
PACKAGE: 包声明
1.1.3) Retention:如果设为了RUNTIME,代表此annotation的具体实现可以在运行时用类反射来实现
我们看到了,annotation一般为一个@interface,也没啥具体的implementation(实现)(干货——Retention注解的作用)
怎么实现这个annotation呢?类反射
@Retention: 表示需要在什么级别保存该注解信息。
可用RetentionPolicy枚举类型主要有:
SOURCE: 注解将被编译器丢弃。
CLASS : 注解在class文件中可能。但会被VM丢弃。
RUNTIME: VM将在运行时也保存注解(如果需要通过反射读取注解,则
使用该值)。
1.1.4) @Documented
@Documented: 将此注解包含在Javadoc中。(干货——Documented注解的作用)
2)看个荔枝(注解有两个元素)
3)关键是来看这两个自定义annotation的用法:
----------------------------------------------------------------------------------------【2】自定义annotation高级篇2.1)首先,网上的一些关于自定义annotation教程所举的例子都不太好!就2个例子,然后一帮子人在那边到处COPY这两个例子然后到处转发,搞得来大家云里雾里一头雾水,同时一群企业的面试官也就喜欢拿这个自定义annotation来作面试题,好像会个annotation就能给Senior software engineer了。其实Annotation就是类反射加点枚举,比个数据结构里的冒泡排序还简单,没这么夸张,关键是例子举的不好,现在来看看下面这个例子。(干货——其实Annotation就是类反射加点枚举)2.2)通过例子来看一个简单的Annotation好了,现在开始我们自己的例子,设有一CLASS叫Student,其中有三个fields:private String name = "";private int age = 0;private String studentId = "";相应的每一个field有一对的set, get方法 然后我在每个set方法上造一个annotation叫ValueBind的注解,其作用是:只要set方法上带有ValueBind注解,它就会根据这个字段的类型把一个默认值,自动赋给Student类中相对应的field。先来看一下Student类:
够简单的吧!
首先这个annotation只能被标注在方法上
其次它含有两个值,一个是enum类型,一个是String类型
2.4) 利用JAVA类反射来实现我们的Annotation
现在来看我们真正的实现(用类反射来实现)
2.4.1)运行后的显示结果为:
0.1)本文idea 转自 http://blog.csdn.net/lifetragedy/article/details/7394910
0.2) java 注解的理论基础知识,参见 http://blog.csdn.net/pacosonswjtu/article/details/50719361
0.3)for complete source code about java annotation , please visit https://github.com/pacosonTang/core-java-volume/tree/master/coreJavaAdvanced/chapter10/annotation
【1】自定义Annotation概念篇
1.1)来看一个最简单的annotation
package com.corejava.chapter10.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 MyAnnotation1 { String value(); }
1.1.1) Annotation 需要声明为@interface这样的东西
1.1.2) @Target(ElementType.TYPE)
代码这个annotation必须且一定要注释在什么语句上面
ElementType.TYPE代表此Annotation必须声明在public class Student{…}的上面,而不能写在任何的method{}(方法)或者是field(属性)的上方。
@Target: 表示该注解可以用于什么地方。可用ElementType枚举类型主要有:(干货——@Target注解的作用)
TYPE : 类、接口或enum声明
FIELD: 域(属性)声明
METHOD: 方法声明
PARAMETER: 参数声明
CONSTRUCTOR: 构造方法声明
LOCAL_VARIABLE:局部变量声明
ANNOTATION_TYPE:注释类型声明
PACKAGE: 包声明
1.1.3) Retention:如果设为了RUNTIME,代表此annotation的具体实现可以在运行时用类反射来实现
我们看到了,annotation一般为一个@interface,也没啥具体的implementation(实现)(干货——Retention注解的作用)
怎么实现这个annotation呢?类反射
@Retention: 表示需要在什么级别保存该注解信息。
可用RetentionPolicy枚举类型主要有:
SOURCE: 注解将被编译器丢弃。
CLASS : 注解在class文件中可能。但会被VM丢弃。
RUNTIME: VM将在运行时也保存注解(如果需要通过反射读取注解,则
使用该值)。
1.1.4) @Documented
@Documented: 将此注解包含在Javadoc中。(干货——Documented注解的作用)
2)看个荔枝(注解有两个元素)
package com.corejava.chapter10.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 MyAnnotation2 { String desc(); boolean isAnnotation(); }
3)关键是来看这两个自定义annotation的用法:
package com.corejava.chapter10.annotation; @MyAnnotation1("this is the annotation1") public class AnnotationDemo { @MyAnnotation2(desc="this is the annotation2", isAnnotation=true) public void sayHello() { System.out.println("hello world!"); } }
package com.corejava.chapter10.annotation; import com.corejava.chapter10.annotation.ValueBind.FieldType; public class Student { private String name = ""; private int age = 0; private String studentid = ""; @ValueBind(type=FieldType.STRING,value="xiaotang") public void seName(String name) { this.name = name; } @ValueBind(type=FieldType.INT,value="110") public void setAge(int age) { this.age = age; } @ValueBind(type=FieldType.STRING,value="2014210") public void setStudentid(String studentid) { this.studentid = studentid; } public String getName() { return name; } public String getStudentid() { return studentid; } public int getAge() { return age; } }
2.3)自定义一个ValueBind的Annotation
这个@ValueBoind就是我的自定义的annotation,里面有两个值,来看这个annotation是怎么做的吧:package com.corejava.chapter10.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 ValueBind { enum FieldType { STRING, INT }; FieldType type(); String value(); }
够简单的吧!
首先这个annotation只能被标注在方法上
其次它含有两个值,一个是enum类型,一个是String类型
2.4) 利用JAVA类反射来实现我们的Annotation
现在来看我们真正的实现(用类反射来实现)
package com.corejava.chapter10.annotation; import java.lang.reflect.Method; public class PersistStudent { public static void main(String[] args) throws Exception { Object obj = Class.forName("com.corejava.chapter10.annotation.Student").newInstance(); try { Method[] methods = obj.getClass().getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { if(methods[i].isAnnotationPresent(ValueBind.class)) { ValueBind annotation = methods[i].getAnnotation(ValueBind.class); String type = String.valueOf(annotation.type()); String value = annotation.value(); if(type.equals("INT")) { methods[i].invoke(obj, new Integer[]{new Integer(value)}); } else { methods[i].invoke(obj, new String[]{value}); } } } Student stu = (Student)obj; System.out.println("studentId = " + stu.getStudentid() + "\nstudentName = " + stu.getName() + "\nstudentAge" + " = " + stu.getAge()); } catch (Exception e) { e.printStackTrace(); } } }
2.4.1)运行后的显示结果为:
相关文章推荐
- java自动拆装箱,接口和抽象类
- java 创建文件和文件夹
- 编译型和解释型的区别&&Java从编译到执行的过程
- JAVA 内部静态类-避免内存泄漏的原因
- java中的静态变量
- java36.Socket通信------ServerSocket的建立与使用示例
- java 以字符为单位读取文件(3)
- springMVC-interceptors
- eclipse导入java工程不如直接导入java文件方便
- JAVA中关于set()和get()方法的理解及使用
- java 以字节为单位读取文件(2)
- Eclipse配置外部Maven
- JAVA_面向对象_笔记_01
- JAVA_面向对象_笔记_01
- Spring定时任务的几种实现
- java的动态代理机制详解
- eclipse ant jacoco
- JavaWeb----Struts2
- 堆排序-java
- Java