JAVA注解基础

何为注解

注解(Annotation)又称为元数据,在JDK1.5后引入,它的做用是:java

生成文档  这是注解的原始用途,能够经过注解生成JavaDoc文档程序员

跟踪代码的依赖性  能够经过注解替代配置文件,简化项目的配置。现有的许多框架都采用这个功能减小本身的配置。框架

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

基础注解

Java目前内置了三种标准注解,以及四种元注解。四种元注解负责建立其余的注解。spa

三种标准注解

@Override,表示当前的方法覆盖超类中的方法.net

@Deprecated,若是程序员使用被这个注解注释的元素,则编译器会进行提示code

@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=================

经过反射机制,遍历方法,而后获取方法的注解。注解能够注解类,方法,字段等等,因此也能够经过相似的方式获取这些对象的注解,而后进行处理。

相关文章
相关标签/搜索