• 【转】 java自定义注解


     java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。
    注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。

    1、元注解

    元注解是指注解的注解。包括  @Retention @Target @Document @Inherited四种。


    1.1、@Retention: 定义注解的保留策略

    @Retention(RetentionPolicy.SOURCE)   //注解仅存在于源码中,在class字节码文件中不包含
    @Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
    @Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
     
    1.2、@Target:定义注解的作用目标
    其定义的源码为: 
    1. @Documented  
    2. @Retention(RetentionPolicy.RUNTIME)  
    3. @Target(ElementType.ANNOTATION_TYPE)  
    4. public @interface Target {  
    5.     ElementType[] value();  
    6. }  
    @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) ///   
     由以上的源码可以知道,他的elementType 可以有多个,一个注解可以为类的,方法的,字段的等等
    1.3、@Document:说明该注解将被包含在javadoc中
     
    1.4、@Inherited:说明子类可以继承父类中的该注解
     
    2、java 注解的自定义
          下面是自定义注解的一个例子
    1. @Documented  
    2. @Target({ElementType.TYPE,ElementType.METHOD})  
    3. @Retention(RetentionPolicy.RUNTIME)  
    4. public @interface Yts {  
    5.    public enum YtsType{util,entity,service,model}  
    6.      
    7.    public YtsType classType() default YtsType.util;  
    8. }  
    9.    
    1. @Documented  
    2. @Retention(RetentionPolicy.RUNTIME)  
    3. @Target(ElementType.METHOD)  
    4. @Inherited  
    5. public @interface HelloWorld {  
    6.     public String name()default "";  
    7. }  

    @Retention(RetentionPolicy.RUNTIME)

    定义的这个注解是注解会在class字节码文件中存在,在运行时可以通过反射获取到。

    @Target({ElementType.TYPE,ElementType.METHOD})

    因此这个注解可以是类注解,也可以是方法的注解

    这样一个注解就自定义好了,当然注解里面的成员可以为基本的数据类型,也可以为数据,Object等等

    3 注解是定义好了,那么怎么来得到,解析注解呢?

    java的反射机制可以帮助,得到注解,代码如下:

    1. public class ParseAnnotation {  
    2.   
    3.      public void parseMethod(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException{  
    4.   Object obj = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});  
    5.     for(Method method : clazz.getDeclaredMethods()){  
    6.         HelloWorld say = method.getAnnotation(HelloWorld.class);  
    7.         String name = "";  
    8.         if(say != null){  
    9.            name = say.name();  
    10.            method.invoke(obj, name);  
    11.         }  
    12.        Yts yts = (Yts)method.getAnnotation(Yts.class);  
    13.        if(yts != null){  
    14.           if(YtsType.util.equals(yts.classType())){  
    15.           System.out.println("this is a util method");  
    16.         }else{  
    17.             System.out.println("this is a other method");  
    18.             }  
    19.         }  
    20.       }  
    21.     }  
    22.     @SuppressWarnings("unchecked")  
    23.     public void parseType(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{  
    24.         Yts yts = (Yts) clazz.getAnnotation(Yts.class);  
    25.         if(yts != null){  
    26.             if(YtsType.util.equals(yts.classType())){  
    27.                 System.out.println("this is a util class");  
    28.             }else{  
    29.                 System.out.println("this is a other class");  
    30.             }  
    31.         }  
    32.     }  
    33.       
    34. }  

    前一个方法是解析得到方法注解的,后一个方法是得到类注解的

    以下是测试方法类

    1. @Yts(classType =YtsType.util)  
    2. public class SayHell {  
    3.   
    4.     @HelloWorld(name = " 小明 ")  
    5.     @Yts  
    6.     public void sayHello(String name){  
    7.         if(name == null || name.equals("")){  
    8.             System.out.println("hello world!");  
    9.         }else{  
    10.             System.out.println(name + "say hello world!");  
    11.         }  
    12.     }  
    13. }  
      1. public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException {  
      2.         ParseAnnotation parse = new ParseAnnotation();  
      3.         parse.parseMethod(SayHell.class);  
      4.         parse.parseType(SayHell.class);  
      5.     } 
  • 相关阅读:
    关于删除表记录 与 清空表记录
    alter table,复制, 单表查询
    表之间的关系
    表的约束条件
    表的数据类型
    创建表的完整语法
    MYSQL基本语句
    MYSQL安装及环境搭建
    支付宝二次封装、订单模块与表分析、订单模块接口分析、支付接口、重写序列化类的create方法、前后台回调接口配置、前台生成订单并跳转、前台支付成功页面、支付宝get回调参数、同步异步回调接口、上线前准备
    区间过滤、课程主页前端、课程详情页前端、课程单查接口、章节分类接口、七牛云视频托管、后台搜索接口、前台搜索页面完成、沙箱环境
  • 原文地址:https://www.cnblogs.com/sunson/p/3166975.html
Copyright © 2020-2023  润新知