Annotation在java的世界正铺天盖地展开,有空写这一篇简单的annotations的文章,算是关于Annotation入门的文章吧,希望能各位们能抛砖,共同学习......
不讲废话了,实践才是硬道理.
第一部分:了解一下java1.5起默认的三个annotation类型:
一个是@Override:只能用在方法之上的,用来告诉别人这一个方法是改写父类的。
一个是@Deprecated:建议别人不要使用旧的API的时候用的,编译的时候会用产生警告信息,可以设定在程序里的所有的元素上.
一个是@SuppressWarnings:这一个类型可以来暂时把一些警告信息消息关闭.
如果不清楚上面三个类型的具体用法,各位可以baidu或google一下的,很简单的。
第二部分:讲一下annotation的概念先,再来讲一下怎样设计自己的annotation.
首先在jdk自带的java.lang.annotation包里,打开如下几个源文件:
1、源文件Target.java
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%20%20%20%40Documented%0A%20%20%20%40Retention(RetentionPolicy.RUNTIME)%0A%20%20%20%40Target(ElementType.ANNOTATION_TYPE)%0A%20%20%20public%20%40interface%20Target%20%7B%0A%20%20%20%20%20%20ElementType%5B%5D%20value()%3B%0A%20%20%20%7D"></embed>
- @Documented
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.ANNOTATION_TYPE)
- public @interface Target {
- ElementType[] value();
- }
其中的@interface是一个关键字,在设计annotations的时候必须把一个类型定义为@interface,而不能用class或interface关键字(会不会觉得sun有点吝啬,偏偏搞得与interface这么像).
2、源文件Retention.java
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%20%20%20%40Documented%0A%20%20%20%40Retention(RetentionPolicy.RUNTIME)%0A%20%20%20%40Target(ElementType.ANNOTATION_TYPE)%0A%20%20%20public%20%40interface%20Retention%20%7B%0A%20%20%20%20%20%20RetentionPolicy%20value()%3B%0A%20%20%20%7D"></embed>
- @Documented
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.ANNOTATION_TYPE)
- public @interface Retention {
- RetentionPolicy value();
- }
看到这里,大家可能都模糊了,都不知道在说什么,别急,往下看一下.
在上面的文件都用到了RetentionPolicy,ElementType这两个字段,你可能就会猜到这是两个java文件.的确,这两个文件的源代码如下:
3、源文件RetentionPolicy.java
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%20%20%20%20public%20enum%20RetentionPolicy%20%7B%0A%20%20%20%20%20SOURCE%2C%0A%20%20%20%20%20CLASS%2C%0A%20%20%20%20%20RUNTIME%0A%20%20%20%20%7D"></embed>
- public enum RetentionPolicy {
- SOURCE,
- CLASS,
- RUNTIME
- }
这是一个enum类型,共有三个值,分别是SOURCE,CLASS 和 RUNTIME.
SOURCE代表的是这个Annotation类型的信息只会保留在程序源码里,源码如果经过了编译之后,Annotation的数据就会消失,并不会保留在编译好的.class文件里面。
ClASS的意思是这个Annotation类型的信息保留在程序源码里,同时也会保留在编译好的.class文件里面,在执行的时候,并不会把这一些信息加载到虚拟机(JVM)中去.注意一下,当你没有设定一个Annotation类型的Retention值时,系统默认值是CLASS.
第三个,是RUNTIME,表示在源码、编译好的.class文件中保留信息,在执行的时候会把这一些信息加载到JVM中去的.
举一个例子,如@Override里面的Retention设为SOURCE,编译成功了就不要这一些检查的信息;相反,@Deprecated里面的Retention设为RUNTIME,表示除了在编译时会警告我们使用了哪个被Deprecated的方法,在执行的时候也可以查出该方法是否被Deprecated.
4、源文件ElementType.java
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%20%20%20public%20enum%20ElementType%20%7B%0A%20%20%20%20TYPE%2C%20FIELD%2C%20METHOD%2C%20PARAMETER%2C%20CONSTRUCTOR%2C%0A%20%20%20%20LOCAL_VARIABLE%2C%20ANNOTATION_TYPE%2CPACKAGE%0A%20%20%20%7D"></embed>
- public enum ElementType {
- TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR,
- LOCAL_VARIABLE, ANNOTATION_TYPE,PACKAGE
- }
@Target里面的ElementType是用来指定Annotation类型可以用在哪一些元素上的.说明一下:TYPE(类型), FIELD(属性), METHOD(方法), PARAMETER(参数), CONSTRUCTOR(构造函数),LOCAL_VARIABLE(局部变量), ANNOTATION_TYPE,PACKAGE(包),其中的TYPE(类型)是指可以用在Class,Interface,Enum和Annotation类型上.
另外,从1的源代码可以看出,@Target自己也用了自己来声明自己,只能用在ANNOTATION_TYPE之上.
如果一个Annotation类型没有指明@Target使用在哪些元素上,那么它可以使用在任何元素之上,这里的元素指的是上面的八种类型.
举几个正确的例子:
@Target(ElementType.METHOD)
@Target(value=ElementType.METHOD)
@Target(ElementType.METHOD,ElementType.CONSTRUCTOR)
具体参考一下javadoc文档
上面一下1和2的源文件,它们都使用了@Documented,@Documented的目的就是让这一个Annotation类型的信息能够显示在javaAPI说明文档上;没有添加的话,使用javadoc生成API文档的时候就会找不到这一个类型生成的信息.
另外一点,如果需要把Annotation的数据继承给子类,那么就会用到@Inherited这一个Annotation类型.
第三部分:下面讲的设计一个最简单的Annotation例子,这一例子共用四个文件;
1、Description.java
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%20%20%20package%20lighter.iteye.com%3B%0A%0A%20%20%20import%20java.lang.annotation.Documented%3B%0A%20%20%20import%20java.lang.annotation.ElementType%3B%0A%20%20%20import%20java.lang.annotation.Retention%3B%0A%20%20%20import%20java.lang.annotation.RetentionPolicy%3B%0A%20%20%20import%20java.lang.annotation.Target%3B%0A%0A%20%20%20%40Target(ElementType.TYPE)%0A%20%20%20%40Retention(RetentionPolicy.RUNTIME)%0A%20%20%20%40Documented%0A%20%20%20public%20%40interface%20Description%20%7B%0A%20%20%20%20%20%20%20String%20value()%3B%0A%20%20%20%7D"></embed>
- package lighter.iteye.com;
-
- 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 Description {
- String value();
- }
说明:所有的Annotation会自动继承java.lang.annotation这一个接口,所以不能再去继承别的类或是接口.
最重要的一点,Annotation类型里面的参数该怎么设定:
第一,只能用public或默认(default)这两个访问权修饰.例如,String value();这里把方法设为defaul默认类型.
第二,参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和String,Enum,Class,annotations等数据类型,以及这一些类型的数组.例如,String value();这里的参数成员就为String.
第三,如果只有一个参数成员,最好把参数名称设为"value",后加小括号.例:上面的例子就只有一个参数成员.
2、Name.java
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%20%20%20package%20lighter.iteye.com%3B%0A%0A%20%20%20import%20java.lang.annotation.Documented%3B%0A%20%20%20import%20java.lang.annotation.ElementType%3B%0A%20%20%20import%20java.lang.annotation.Retention%3B%0A%20%20%20import%20java.lang.annotation.RetentionPolicy%3B%0A%20%20%20import%20java.lang.annotation.Target%3B%0A%0A%20%20%20%20%2F%2F%E6%B3%A8%E6%84%8F%E8%BF%99%E9%87%8C%E7%9A%84%40Target%E4%B8%8E%40Description%E9%87%8C%E7%9A%84%E4%B8%8D%E5%90%8C%2C%E5%8F%82%E6%95%B0%E6%88%90%E5%91%98%E4%B9%9F%E4%B8%8D%E5%90%8C%0A%20%20%20%40Target(ElementType.METHOD)%0A%20%20%20%40Retention(RetentionPolicy.RUNTIME)%0A%20%20%20%40Documented%0A%20%20%20public%20%40interface%20Name%20%7B%0A%20%20%20%20%20%20%20String%20originate()%3B%0A%20%20%20%20%20%20%20String%20community()%3B%0A%20%20%20%7D"></embed>
- package lighter.iteye.com;
-
- 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 Name {
- String originate();
- String community();
- }
3、JavaEyer.java
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20lighter.iteye.com%3B%0A%0A%40Description(%22javaeye%2C%E5%81%9A%E6%9C%80%E6%A3%92%E7%9A%84%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91%E4%BA%A4%E6%B5%81%E7%A4%BE%E5%8C%BA%22)%0Apublic%20class%20JavaEyer%20%7B%0A%09%40Name(originate%3D%22%E5%88%9B%E5%A7%8B%E4%BA%BA%3Arobbin%22%2Ccommunity%3D%22javaEye%22)%0A%09public%20String%20getName()%0A%09%7B%0A%09%09return%20null%3B%0A%09%7D%0A%09%0A%09%40Name(originate%3D%22%E5%88%9B%E5%A7%8B%E4%BA%BA%3A%E6%B1%9F%E5%8D%97%E7%99%BD%E8%A1%A3%22%2Ccommunity%3D%22springside%22)%0A%09public%20String%20getName2()%0A%09%7B%0A%09%09return%20%22%E5%80%9F%E7%94%A8%E4%B8%A4%E4%BD%8D%E7%9A%84id%E4%B8%80%E7%94%A8%2C%E5%86%99%E8%BF%99%E4%B8%80%E4%B8%AA%E4%BE%8B%E5%AD%90%2C%E8%AF%B7%E8%A7%81%E8%B0%85!%22%3B%0A%09%7D%0A%7D"></embed>
- package lighter.iteye.com;
-
- @Description("javaeye,做最棒的软件开发交流社区")
- public class JavaEyer {
- @Name(originate="创始人:robbin",community="javaEye")
- public String getName()
- {
- return null;
- }
-
- @Name(originate="创始人:江南白衣",community="springside")
- public String getName2()
- {
- return "借用两位的id一用,写这一个例子,请见谅!";
- }
- }
4、最后,写一个可以运行提取JavaEyer信息的类TestAnnotation
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%20package%20lighter.iteye.com%3B%0A%0A%20%20import%20java.lang.reflect.Method%3B%0A%20%20import%20java.util.HashSet%3B%0A%20%20import%20java.util.Set%3B%0A%0A%20%20public%20class%20TestAnnotation%20%7B%0A%09%2F**%0A%09%20*%20author%20lighter%0A%09%20*%20%E8%AF%B4%E6%98%8E%3A%E5%85%B7%E4%BD%93%E5%85%B3%E5%A4%A9Annotation%E7%9A%84API%E7%9A%84%E7%94%A8%E6%B3%95%E8%AF%B7%E5%8F%82%E8%A7%81javaDoc%E6%96%87%E6%A1%A3%0A%09%20*%2F%0A%20%20%20%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20throws%20Exception%20%7B%0A%20%20%20%20%20%20%20String%20%20CLASS_NAME%20%3D%20%22lighter.iteye.com.JavaEyer%22%3B%0A%20%20%20%20%20%20%20Class%20%20test%20%3D%20Class.forName(CLASS_NAME)%3B%0A%20%20%20%20%20%20%20Method%5B%5D%20method%20%3D%20test.getMethods()%3B%0A%20%20%20%20%20%20%20boolean%20flag%20%3D%20test.isAnnotationPresent(Description.class)%3B%0A%20%20%20%20%20%20%20%20if(flag)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%09Description%20des%20%3D%20(Description)test.getAnnotation(Description.class)%3B%0A%20%20%20%20%20%20%20%20%09System.out.println(%22%E6%8F%8F%E8%BF%B0%3A%22%2Bdes.value())%3B%0A%20%20%20%20%20%20%20%20%09System.out.println(%22-----------------%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%E6%8A%8AJavaEyer%E8%BF%99%E4%B8%80%E7%B1%BB%E6%9C%89%E5%88%A9%E7%94%A8%E5%88%B0%40Name%E7%9A%84%E5%85%A8%E9%83%A8%E6%96%B9%E6%B3%95%E4%BF%9D%E5%AD%98%E5%88%B0Set%E4%B8%AD%E5%8E%BB%0A%20%20%20%20%20%20%20%20Set%3CMethod%3E%20set%20%3D%20new%20HashSet%3CMethod%3E()%3B%0A%20%20%20%20%20%20%20%20for(int%20i%3D0%3Bi%3Cmethod.length%3Bi%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%09boolean%20otherFlag%20%3D%20method%5Bi%5D.isAnnotationPresent(Name.class)%3B%0A%20%20%20%20%20%20%20%20%09if(otherFlag)%20set.add(method%5Bi%5D)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20for(Method%20m%3A%20set)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%09Name%20name%20%3D%20m.getAnnotation(Name.class)%3B%0A%20%20%20%20%20%20%20%20%09System.out.println(name.originate())%3B%0A%20%20%20%20%20%20%20%20%09System.out.println(%22%E5%88%9B%E5%BB%BA%E7%9A%84%E7%A4%BE%E5%8C%BA%3A%22%2Bname.community())%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%7D%0A%7D"></embed>
- package lighter.iteye.com;
-
- import java.lang.reflect.Method;
- import java.util.HashSet;
- import java.util.Set;
-
- public class TestAnnotation {
-
-
-
-
- public static void main(String[] args) throws Exception {
- String CLASS_NAME = "lighter.iteye.com.JavaEyer";
- Class test = Class.forName(CLASS_NAME);
- Method[] method = test.getMethods();
- boolean flag = test.isAnnotationPresent(Description.class);
- if(flag)
- {
- Description des = (Description)test.getAnnotation(Description.class);
- System.out.println("描述:"+des.value());
- System.out.println("-----------------");
- }
-
-
- Set<Method> set = new HashSet<Method>();
- for(int i=0;i<method.length;i++)
- {
- boolean otherFlag = method[i].isAnnotationPresent(Name.class);
- if(otherFlag) set.add(method[i]);
- }
- for(Method m: set)
- {
- Name name = m.getAnnotation(Name.class);
- colo
分享到:
相关推荐
- **JPA Annotation:** Springside3中实体类的定义使用了JPA注解,如`@Entity`、`@Table`等,这些注解直接定义在实体类上,使得代码更加简洁且易于维护。 - **实体类字段映射:** 如`@Column`用于指定表中的列名,...
- **事务零配置**:通过`<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>`启用基于注解的事务管理。 #### SpringSide用例 - **实体层**:实体类通常继承自`IdEntity`...
在SpringSide中,可以通过`@ManyToOne`注解来实现。 - **多对多映射**:用于表示两个实体之间的多对多关系,例如用户和群组之间的关联。这通常需要一个中间表来进行关联。 ##### 2. RBAC模型 - **RBAC (Role-Based ...
这个"spring+hibernate annotation 完整示例,带数据库脚本"的项目,旨在提供一个全面的示例,帮助开发者了解如何在实际项目中结合Spring和Hibernate使用注解来实现数据访问。下面我们将深入探讨这些知识点。 1. **...
以上内容涵盖了SpringSide应用中CRUD操作的基本步骤,从数据库设计到业务层和控制层的实现,以及相应的测试用例编写,这些都是构建一个完整SpringSide应用不可或缺的部分。通过这些实践,开发者可以更高效地管理数据...