java自定义注解及注解使用

java注解是指附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。

其实际不会也不能影响代码的实际逻辑,仅仅起到辅助性标记的作

用。而对于java的注解jdk包java.lang.annotation有详细的介绍。

元注解是指注解的注解,JAVA JDK中提供了四种元注解,分别是:

@Document 标明该注解将会包含至javaDoc文档中。 JDK1.5 +

@Inherited 标明该注解可以由子类继承。 JDK1.5 +

@Retention 标明注解的保留策略。JDK1.5 +

JDK提供了三种保留策略:

@Retention(RetentionPolicy.SOURCE) -- 注解只存在于源代码中,字节码Class文件中将不存在该注解。

@Retention(RetentionPolicy.CLASS) -- 标明注解只会被编译器编译后保留在Class字节码文件中,而运行时无法获取。

@Retention(RetentionPolicy.RUNTIME)  -- 标明注解会保留在class字节码文件中,且运行时能通过反射机制获取。

JDK源码:

public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

@Target 标明注解的作用目标对象。

其作用对象主要有:

JDK源码

public enum ElementType {
    
  /** Class, interface (including annotation type), or enum declaration */ -- 标注类,接口,注解,枚举
TYPE, /** Field declaration (includes enum constants) */ --标注字段,枚举常量 FIELD, /** Method declaration */ -- 标注方法 METHOD, /** Parameter declaration */ -- 标注参数 PARAMETER, /** Constructor declaration */--标注构造器 CONSTRUCTOR, /** Local variable declaration */ -- 标注局部变量 LOCAL_VARIABLE, /** Annotation type declaration */ -- 标注注解 ANNOTATION_TYPE, /** Package declaration */ -- 标注包 PACKAGE }

了解到java注解的元注解后,如果需要自定义java注解,该怎么样定义?

1:自定义注解:

使用关键字:@interface

1.1 自定义一个类注解

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface AkClass {
	String value() default "hello";
}

 1.2 类注解的使用,如:

import demo.annontation.AkClass;
@AkClass()
public interface AkClassTest {
	
    public abstract void getAK();

}

 1.3 通过java反射机制获取和解析类注解,如:

 注意:只有当类注解使用了RetentionPolicy.RUNTIME保留策略,才能在运行时,通过java反射机制获取

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import demo.annontation.AkClass;
import demo.annontation.AkMethod;

/**
 * 掃描當前包裏面,哪些類被AkClass類注解標記過
 *
 */

public class Main {
	
	public static void main(String[] args) {
		//判斷某個類是否是注解
		System.out.println("對象是否為注解類型:"+AkClassTest.class.isAnnotation());
		System.out.println("調用類指定的類注解屬性值:"+AkClassTest.class.getAnnotation(AkClass.class).value());
		//獲取某個具體類的所有注解
		Annotation[] annotations = AkClassTest.class.getAnnotations();
		for(Annotation annotation : annotations){
			//判斷當前注解對象是否為自定義注解
			if(annotation.annotationType() == AkClass.class){
				System.out.println("類注解名稱:"+AkClass.class.getSimpleName());
				Method[] methods = AkClass.class.getDeclaredMethods();
				for(Method method : methods){
					System.out.println("類注解方法:"+method.getName());
				}
				System.out.println();
			}
		}
		System.out.println("獲取某個類中所有方法的所有方法注解");
		Method[] methods = AkClassTest.class.getMethods();
		for(Method method : methods){
			System.out.println("類方法名:"+method.getName());
			System.out.println("調用方法注解的屬值性:"+method.getAnnotation(AkMethod.class).value());
			Annotation[] mAnnotations = method.getAnnotations();
			for(Annotation mAnnotation : mAnnotations){
				if(mAnnotation.annotationType() == AkMethod.class){
					System.out.println("注解名:"+AkMethod.class.getSimpleName());
				}
			}
		}
	}
}

 

 运行结果:

 

posted on 2013-06-14 16:45  大山下的蚂蚁  阅读(1250)  评论(0编辑  收藏  举报

导航