1.了解和入门注解的应用
Java5注解很重要,现在Sturts2 Spring Hibernate 都使用注解的方式, 我们常见注解:@SuppressWarnings("deprecation") Override Desprected 使用注解:就是告诉虚拟机,通过反射来处理该行代码;注解使用的范围很广,包括 包、类、字段、方法、方法参数和局部变量
2.注解的定义与反射调用
使用注解的流程:
1、先建立一个@annotation 2、定义一个类, 应用注解类 3、对应用注解的类进行反射操作。
@Retention:注解从建立到加载运行都一直保持着。 注解存在着三种状态:java源文件-》.class文件-》内存中的字节码; Retention保持着class文件到内存中的字节码。在编译间:RetentionPolicy.SOURCE RetentionPolicy.CLASS RetentionPolicy.CLASS CLASS 编译器将把注释记录在类文件中,但在运行时 VM 不需要保留注释。 RUNTIME 编译器将把注释记录在类文件中,在运行时 VM 将保留注释,因此可以反射性地读取。 SOURCE 编译器要丢弃的注释。 表明注解可以使用范围的注解类@Target Target可以指定的注解范围列表 ANNOTATION_TYPE 注释类型声明 CONSTRUCTOR 构造方法声明 FIELD 字段声明(包括枚举常量) LOCAL_VARIABLE 局部变量声明 METHOD 方法声明 PACKAGE 包声明 PARAMETER 参数声明 TYPE 类、接口(包括注释类型)或枚举声明
3.为注解增加各种属性
1、@interface Annotation 中添加一个属性值, 2、在使用了该Annotation 的注解上为属性赋值 3、 取值的时候, 按正常的类调用方法的方式一样。 例子:
@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.FIELD,ElementType.TYPE})public@interfaceBtbuAnnotation { String name();//定义了一个属性}
/** * Java注解 *@author dell */@BtbuAnnotation(name="btbu")publicclass AnnotationTest { publicstaticvoid main(String[] args) {boolean b = AnnotationTest.class.isAnnotationPresent(BtbuAnnotation.class);if(b){BtbuAnnotationanno = AnnotationTest.class.getAnnotation(BtbuAnnotation.class); System.out.println(anno.name()); } }}
4.为注解增加高强功能
为注解添加数组、枚举、数组属性、注解、class
/** * 枚举类型的抽象方法 * @author dell * */ publicenumTrafficLamp{ RED(30){ public TrafficLamp nextLamp(){ returnGREEN; } }, GREEN(45){ public TrafficLamp nextLamp(){ returnYELLOW; } }, YELLOW(5){ public TrafficLamp nextLamp(){ returnRED; } }; //抽象方法并且必须位于枚举元素之后 publicabstractTrafficLampnextLamp(); privateinttime; //使用构造方法将其实例化 private TrafficLamp(int time){ this.time = time; } }}packageorg.nagi.annotation;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.RUNTIME)public@interfaceMyAnnotation { String age();}packageorg.nagi.annotation;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;/** *自定义的注解 *@author dell *元注解 */@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.FIELD,ElementType.TYPE})public@interfaceBtbuAnnotation { String name();//定义了一个属性 String school() default"btbu";//默认缺省值 int [] arrayls()default {};//数组 EnumTest.TrafficLamp lamd() default EnumTest.TrafficLamp.RED;//枚举 MyAnnotation annotation(); //注解 Class a(); //class}packageorg.nagi.annotation;importorg.nagi.annotation.EnumTest.TrafficLamp;/** * Java注解 *@author dell */@BtbuAnnotation(a=String.class,name="btbu",lamd=TrafficLamp.GREEN,annotation=@MyAnnotation(age="21"))publicclass AnnotationTest { publicstaticvoid main(String[] args) { boolean b = AnnotationTest.class.isAnnotationPresent(BtbuAnnotation.class); if(b){ BtbuAnnotation anno = AnnotationTest.class.getAnnotation(BtbuAnnotation.class); System.out.println(anno.name());//取得属性值 System.out.println(anno.arrayls());//得到数组 System.out.println(anno.lamd());//得到枚举值 System.out.println(anno.a());//得到class值 System.out.println(anno.annotation().age());//得到注解值 } }}
运行结果:btbu [I@75d65c GREEN classjava.lang.String 21
充满了恐惧的声音,一种不确定的归宿的流动。