java自定义注解及注解应用   
               添加时间:2013-6-16 点击量: 
 
              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 /--标注机关器
     CTRUCTOR,
    / 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());
				}
			}
		}
	}
}
 运行成果:

无论对感情还是对生活,“只要甜不要苦”都是任性而孩子气的,因为我们也不完美,我们也会伤害人。正因为我们都不完美,也因为生活从不是事事如意,所以对这些“瑕疵”的收纳才让我们对生活、对他人的爱变得日益真实而具体。—— 汪冰《世界再亏欠你,也要敢于拥抱幸福》
                     
                  
     
  
 
    
    
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 /--标注机关器
     CTRUCTOR,
    / 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());
}
}
}
}
}
运行成果:

无论对感情还是对生活,“只要甜不要苦”都是任性而孩子气的,因为我们也不完美,我们也会伤害人。正因为我们都不完美,也因为生活从不是事事如意,所以对这些“瑕疵”的收纳才让我们对生活、对他人的爱变得日益真实而具体。—— 汪冰《世界再亏欠你,也要敢于拥抱幸福》




