use Annotation in JDK 5
来源:互联网 发布:mysql主从数据库 编辑:程序博客网 时间:2024/05/22 05:19
写一点关于JDK5的最新的技术Annotation,关于 Annotation的应用最近真的是火的不得了,tapestry、spring等框架都已经在用Annotation,听说最近struts2.0也应用了 Annotation,hibernate也不甘示弱,应用Annotation并且可以为。NET持久----NHIBERNATE已经出炉。。。。。
网上其实有很多讲Annotation,我也来凑个热闹,let getting start with Annotation!!!!!!!
一、为什么使用Annotation:
在JAVA应用中,我们常遇到一些需要使用模版代码。例如,为了编写一个JAX-RPC web service,我们必须提供一对接口和实现作为模版代码。如果使用annotation对远程访问的方法代码进行修饰的话,这个模版就能够使用工具自动生成。
另外,一些API需要使用与程序代码同时维护的附属文件。例如,JavaBeans需要一个BeanInfo Class与一个Bean同时使用/维护,而EJB则同样需要一个部署描述符。此时在程序中使用annotation来维护这些附属文件的信息将十分便利而且减少了错误。
二、Annotation工作方式:
在5.0 版之前的Java平台已经具有了一些ad hoc annotation机制。比如,使用transient修饰符来标识一个成员变量在序列化子系统中应被忽略。而@deprecated这个 javadoc tag也是一个ad hoc annotation用来说明一个方法已过时。从Java5.0版发布以来,5.0平台提供了一个正式的annotation功能:允许开发者定义、使用自己的annoatation类型。此功能由一个定义annotation类型的语法和一个描述annotation声明的语法,读取annotaion 的API,一个使用annotation修饰的class文件,一个annotation处理工具(apt)组成。
annotation并不直接影响代码语义,但是它能够工作的方式被看作类似程序的工具或者类库,它会反过来对正在运行的程序语义有所影响。annotation可以从源文件、class文件或者以在运行时反射的多种方式被读取。
当然annotation在某种程度上使javadoc tag更加完整。一般情况下,如果这个标记对java文档产生影响或者用于生成java文档的话,它应该作为一个javadoc tag;否则将作为一个annotation。
上面2点是抄网上的,关键是先把JAVA反射学会
下面是我写的一个例子:
create a annotation named MyAnnotation under the package "com":
package com;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
int num();
String name();
}
note:
@Retention(RetentionPolicy.RUNTIME)这个meta-annotation表示了此类型的 annotation将被虚拟机保留使其能够在运行时通过反射被读取。而@Target(ElementType.METHOD)表示此类型的 annotation只能用于修饰方法声明。
int num()在网上被称为方法,方法返回值的类型被限制在以下的范围: primitives、String、Class、enums、annotation和前面类型的数组,但是你可以理解为FIELD。
annotation建好了,我们在建一个测试类:
package com;
import java.lang.annotation.Annotation;
public class Test {
/**
* @author james
*/
@MyAnnotation(num=10,name="james")
public void getAnnotation(){};
public static void main(String[] args) throws SecurityException, NoSuchMethodException, ClassNotFoundException {
Test test = new Test();
try{
Annotation[] annotations = Class.forName("com.Test").getMethod("getAnnotation").getAnnotations();
for(Annotation a:annotations){
System.out.println(a);
if(((MyAnnotation)a).num()==10){
System.out.println("I found num is 10");
}
if(((MyAnnotation)a).name().equals("james")){
System.out.println("I found james");
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Annotation[] annotations = Class.forName("com.Test").getMethod("getAnnotation").getAnnotations()就是利用反射得到TEST类的getAnnotation()方法中的注释。
((MyAnnotation)a).num()得到注释的相关属性,这样得到属性后就可以做我们想做的事了。
- use Annotation in JDK 5
- Use annotation in Java
- JDK 5 Annotation\注解\注释\自定义注解
- JDK 5 Annotation\注解\注释\自定义注解
- JDK 5 Annotation\注解\注释\自定义注解
- Annotation-JDK的元Annotation
- JDK的元Annotation
- jdk中Annotation 使用
- Google App Engine问题之Your project must be configured to use a JDK in order to use JSPs
- [转]Use ReiserFS in CentOS 5(lvm)
- 详解JDK 5 Annotation 注解之@Target的用法介绍
- how to write your annotation types and make use of built-in annotations to control their behavior
- Annotation--学习:JDK内建Annotation
- java Annotation(1)--JDK内置Annotation
- Cannot use the diamond generic notation when running in JDK inferior to 1.7 mode
- Annotation+in+Java
- @Autowired Annotation in Spring
- Annotation in spring
- C#读取纯真IP数据库的代码
- tomcat数据库 jndi文件配置
- HTML 4.0 语言快速参考
- 超级挑战
- Oracle中的动态sql
- use Annotation in JDK 5
- HOW TO:获取文件类型图标和类型说明
- ASP.NET2.0雷霆之怒盗链者的祝福
- 安装与建立 Qt 桌面开发运行环境
- 通过执行一个字符串对一个参数附值
- 序列化Serialize函数
- 诚恳的问微电子有没有必要学MFC?
- 关于春游杂言碎语
- 调用hibernate的save(Object object)时出现"索引中丢失in 或 out...