当前位置: 移动技术网 > IT编程>开发语言>Java > JAVA 注解详解及简单实例

JAVA 注解详解及简单实例

2019年07月22日  | 移动技术网IT编程  | 我要评论

男模特,闹鬼公寓逃生攻略,一体

java 注解详解及简单实例

何为注解

注解(annotation)又称为元数据,在jdk1.5后引入,它的作用是:

生成文档  这是注解的原始用途,可以通过注解生成javadoc文档

跟踪代码的依赖性  可以通过注解替代配置文件,简化项目的配置。现有的许多框架都采用这个功能减少自己的配置。

编译检查  在编译时进行格式检查,例如@override

基础注解

java目前内置了三种标准注解,以及四种元注解。四种元注解负责创建其他的注解。

三种标准注解

@override,表示当前的方法覆盖超类中的方法

@deprecated,如果程序员使用被这个注解注释的元素,则编译器会进行提示

@suppress warnings,忽略编译器的警告

四种元注解

@target,表示注解的适用范围,例如@target(elementtype.field).

elementtype的枚举值有

constructor,用于构造方法

field,用于字段声明,包括常量

local_variable,用于局部变量

method,用于方法

package,用于包声明

parameter,用于参数声明

type,用于类,接口

@retention,表示注解的保留级别,例如@retention(retentionpolicy.runtime).

retentionpolicy的枚举值有

source,注解将被编译器丢弃

class,注解可以在class文件中使用,但是会被vm丢弃

runtime,在vm的运行期间也会保留

@document,将次注解在javadoc文件中可见

@inherited,允许子类继承父类中的注解

自定义注解

创建注解

package com.rainman.annotation;

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)
public @interface methodlog {

  string method() default "do something";

  string opeator() default "someone";

}

@interface标明该类型是一个注解的定义

对于“string method() default "dong something";”来说string是注解参数的数据类型,method是参数名,default用来设置默认值

注解的使用

package com.rainman.controller;

import com.rainman.annotation.methodlog;

public class celebratecontroller {

  @methodlog(method="celebrate birthday",operator="we")
  public void celebratebirthday(){

  }

  @methodlog(operator = "we")
  public void celebratenewyears(){

  }

  @methodlog(method="celebrate harvest")
  public void celebrateharvest(){

  }
  
  @methodlog
  public void yeah(){
    
  }
}

注解使用的格式为"@annotation(para=xxxx,...)",annotation是注解名,para是注解中设置的参数名,参数如果不设置值,则会使用默认值

设置注解解析器

package com.rainman.annotation;

import com.rainman.controller.celebratecontroller;
import java.lang.reflect.method;


public class methodlogparse {

  public static void parse(class classtype) throws exception{
    method[] array = classtype.getmethods();
    for(method method : array){
      system.out.println("=================="+method.getname()+"=================");
      if(method.isannotationpresent(methodlog.class)){
        methodlog methodlog = method.getdeclaredannotation(methodlog.class);
        string str = string.valueof(methodlog.operator());
        string str1 = string.valueof(methodlog.method());

        system.out.println(str + " " + str1);
      }

    }
  }

  public static void main(string[] args){
    try {
      methodlogparse.parse(celebratecontroller.class);
    }catch(exception e){
      e.printstacktrace();
    }
  }
}

输出结果为

==================celebratebirthday=================
we celebrate birthday
==================yeah=================
someone do something
==================celebratenewyears=================
we do something
==================celebrateharvest=================
someone celebrate harvest
==================wait=================
==================wait=================
==================wait=================
==================equals=================
==================tostring=================
==================hashcode=================
==================getclass=================
==================notify=================
==================notifyall=================

通过反射机制,遍历方法,然后获取方法的注解。注解可以注解类,方法,字段等等,所以也可以通过类似的方式获取这些对象的注解,然后进行处理。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网