Annotation实例
来源:互联网 发布:淘宝改默认地址怎么改 编辑:程序博客网 时间:2024/06/05 15:28
http://www.javaeye.com/topic/36659
说的不错,mark一下。
- package com.rx.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 Description {
- String value();
- }
- package com.rx.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 Name {
- String originate();
- String community();
- }
- package com.rx.annotation;
- @Description("javaeye,做最棒的软件开发交流社区")
- public class JavaEyer {
- @Name(originate="创始人:robbin",community="javaEye")
- public String getName()
- {
- return null;
- }
- @Name(originate="创始人:江南白衣",community="springside")
- public String getName2()
- {
- return "借用两位的id一用,写这一个例子,请见谅!";
- }
- public void test() {
- System.out.println("测试.....");
- }
- }
- package com.rx.annotation;
- 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 = "com.rx.annotation.JavaEyer";
- Class annotationClass = Class.forName(CLASS_NAME);
- boolean flag = annotationClass.isAnnotationPresent(Description.class);
- if(flag)
- {
- Description des = (Description)annotationClass.getAnnotation(Description.class);
- System.out.println("描述:" + des.value());
- System.out.println("-----------------");
- }
- Method[] method = annotationClass.getMethods();
- //把JavaEyer这一类有利用到@Name的全部方法保存到Set中去
- 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);
- System.out.println(name.originate());
- System.out.println("创建的社区:"+name.community());
- }
- }
- }
运行测试结果
- 描述:javaeye,做最棒的软件开发交流社区
- -----------------
- 创始人:robbin
- 创建的社区:javaEye
- 创始人:江南白衣
- 创建的社区:springside
其他参考文献:http://www.blogjava.net/nokiaguy/archive/2008/05/09/199456.html
- Annotation实例
- Hibernate4 Annotation实例
- spring中annotation实例
- Hibernate4.1.9 Annotation 实例
- Annotation 入门实例
- Hibernate4 Annotation实例
- java Annotation 自定义实例
- Shiro Annotation保护实例
- Java 自定义annotation实例
- Annotation入门实例,一分钟了解Annotation
- J2SE5.0实例---注释(annotation)
- 学习Hibernate Annotation使用实例
- Hibernate Annotation使用实例---JPA
- spring scheduled annotation 实例一
- spring scheduled annotation 实例二
- Hibernate Annotation (Hibernate 注解实例)
- Spring的annotation注入实例
- 注解(Annotation)从实例开始
- Professional SQL Server 2005 CLR Programming: with Stored Procedures, Functions, Triggers, Aggregate
- Web 设计:实现干净代码的12条定律
- Expert One-on-One Visual Basic 2005 Design and Development
- Ubuntu Linux Bible
- Maya 8 at a Glance
- Annotation实例
- Raw Workflow from Capture to Archives: A Complete Digital Photographer's Guide to Raw Imaging
- Introducing Maya 8: 3D for Beginners
- 解决JSP中文COOKIE问题
- 开业大吉
- Redhat enterprise linux 5 install and configration(Yum change to CentOS)
- Beginning Ruby on Rails
- Professional Web 2.0 Programming
- 免费下载《走进逐浪》精美电子杂志