zoukankan      html  css  js  c++  java
  • java 注解

    java中元注解有四个: @Retention @Target @Document @Inherited

    @Retention:注解的保留位置         
          @Retention(RetentionPolicy.SOURCE)   //注解仅存在于源码中,在class字节码文件中不包含
          @Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得
          @Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
      
    @Target:注解的作用目标
          @Target(ElementType.TYPE)   //接口、类、枚举、注解
          @Target(ElementType.FIELD) //字段、枚举的常量
          @Target(ElementType.METHOD) //方法
          @Target(ElementType.PARAMETER) //方法参数
          @Target(ElementType.CONSTRUCTOR)  //构造函数
          @Target(ElementType.LOCAL_VARIABLE)//局部变量
          @Target(ElementType.ANNOTATION_TYPE)//注解
          @Target(ElementType.PACKAGE) ///包   
     
    @Document:说明该注解将被包含在javadoc中
     
    @Inherited:说明子类可以继承父类中的该注解

    注解是用于建设基础jar包的一部分   项目都有自己的框架,若运用恰当,注解则为其中良好的一部分!
    package bean;

    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;

    /**
    * Created by wb-xxd249566 on 2017/4/12.
    */
    @Retention(RetentionPolicy.RUNTIME)
    public @interface AnnotationTest {

    }

    package bean;

    import java.lang.reflect.Method;

    /**
    * Created by wb-xxd249566 on 2017/4/12.
    */
    public class MyTest {

    @AnnotationTest
    public void doSomeThing(){
    System.out.println("do some thing");
    }

    public static void main(String[] args) throws Exception{
    Method method = MyTest.class.getMethod("doSomeThing",null);
    if (method.isAnnotationPresent(AnnotationTest.class))
    System.out.println(method.getAnnotation(AnnotationTest.class));
    }

    }


  • 相关阅读:
    此查询使用的不是 ANSI 外部联接运算符
    centos重启命令
    updatePanel 加载完成后回调JS
    建站推荐十个免费的CMS内容管理系统(Php+mysql)
    [转]最值得拥有的免费Bootstrap后台管理模板
    Got a packet bigger than 'max_allowed_packet' bytes”
    ECshop商城程序常见的96个小问题汇总
    linux 命令
    mysql 存储过程
    千万级记录的Discuz论坛导致MySQL CPU 100%的优化笔记
  • 原文地址:https://www.cnblogs.com/xxdfly/p/6699601.html
Copyright © 2011-2022 走看看