如何使用java自定义注解?demo

1、Description.java

  package kzfy.bk.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();
  } 

2、Name.java

  package kzfy.bk.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与@Description里的不同,参数成员也不同
  @Target(ElementType.METHOD)
  @Retention(RetentionPolicy.RUNTIME)
  @Documented
  public @interface Name {
      String originate();
      String community();
  } 

3、MyTest.java

  package kzfy.bk.com;   

  @Description("我的第一个使用自定义注解的类!")
  public class MyTest{
      @Name(originate="空中飞鱼",community="java")
      public String getName(){
          return "java,我的人生路!";
      }   

      @Name(originate="博客园",community="写博客")
      public String getName2() {
          return "博客,新的征程!";
      }
  } 

4、运行提取MyTest的类TestAnnotation.java

  package kzfy.bk.com;   

  import java.lang.reflect.Method;
  import java.util.HashSet;
  import java.util.Set;   

  public class TestAnnotation {
      /**
       * Annotation的API的用法请参见javaDoc文档
       */
       public static void main(String[] args) throws Exception {
           String  CLASS_NAME ="kzfy.bk.com.MyTest";
           Class  test = Class.forName(CLASS_NAME);
           boolean flag = test.isAnnotationPresent(Description.class);
            if(flag){
                Description des = (Description)test.getAnnotation(Description.class);
                System.out.println("描述:"+des.value());
                System.out.println("-----------------");
            }   

            //把JavaEyer这一类有利用到@Name的全部方法保存到Set中去
            Method[] method = test.getMethods();
            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("orginate:"+name.originate());
                System.out.println("community:"+name.community());
            }
      }
} 
时间: 2024-08-24 22:06:43

如何使用java自定义注解?demo的相关文章

java自定义注解类

一.前言 今天阅读帆哥代码的时候,看到了之前没有见过的新东西, 比如java自定义注解类,如何获取注解,如何反射内部类,this$0是什么意思? 于是乎,学习并整理了一下. 二.代码示例 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Tar

深入 Java自定义注解

我们在使用Spring框架的时候,会经常使用类似:@Autowired 这样的注解.我们也可以自己定义一些注解.Java的注解主要在包:java.lang.annotation中实现. 1. 元注解 什么是元注解?你可以这样理解,元注解是自定义注解的注解.元注解主要包含4个.他们主要在java.lang.annotation中可以找到.我们自己要创建注解的时候必须要用到这些元注解.所以必须彻底理解这四个元注解的含义. 1. @Documented 2. @Inherited 3. @Retent

Java自定义注解反射校验数据

package com.annotations.ecargo; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUN

Java自定义注解

自定义注解类编写的一些规则: 1. Annotation型定义为@interface, 所有的Annotation会自动继承Java.lang.Annotation这一接口,并且不能再去继承别的类或是接口. 2. 参数成员只能用public或默认(default)这两个访问权修饰 3. 参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和String.Enum.Class.annotations等数据类型,以及这一些类

Java自定义注解的使用

最近学习了一下Java的自定义注解,终于知道了框架那些注解是咋个写出来的了,以后我也可以自己写框架,自己定义注解,听着是不是很牛的样子?不要心动,赶快行动,代码很简单,一起来学习一下吧! 这个例子是模仿框架的一个对sql拼装的例子,用注解实现对model也就是实体类的注释,就可以写出查询该字段的sql.好了,废话少说,一看代码便知.大家可以根据自己的需求修改. package com.annotation.demo; import java.lang.annotation.Documented;

java自定义注解并解读

不多说,先看例子,通过例子来说这个自定义注解. 自己定义了一个注解类testType: package com.zhudan.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(Retention

java自定义注解学习(二)_注解详解

上篇文章,我们简单的实现了一个自定义注解,相信大家对自定义注解有了个简单的认识,这篇,这样介绍下注解中的元注解和内值注解 整体图示 内置注解 @Override 重写覆盖 这个注解大家应该经常用到,主要在子类重写父类的方法,比如toString()方法 package com.kevin.demo; public class Demo1 { @Override public String toString(){ return "demo1"; } } @Deprecated 过时 @D

转!java自定义注解

转自:http://blog.csdn.net/yixiaogang109/article/details/7328466  Java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析和使用,起到说明.配置的功能.注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用.包含在 java.lang.annotation 包中. 1.元注解 元注解是指注解的注解.包括  @Retention @Target @Document @Inherited四种. 1.1.@Retention

java 自定义注解以及获得注解的值

1.自定义注解 import java.lang.annotation.*; @Documented @Target(ElementType.FIELD) @Inherited @Retention(RetentionPolicy.RUNTIME ) public @interface MyAnno { /** * 是否能为null * @return */ boolean isCanNull() default true; /** * 是否能为空字符串 * @return */ boolean