自定义注解。
1.基本注解代码如下:
package com.annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Target({ElementType.TYPE,ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnot {/**定义基本属性*/String myAnnot(); // 类型属性名 }
测试应用其属性
package com.annotation; @MyAnnot(myAnnot = "user") public class Test {public static void main(String[] args) {MyAnnot d = Test.class.getAnnotation(MyAnnot.class); //采用反射获取MyAnnot注解System.out.println(d.myAnnot());} }
2.为属性指定缺省值(默认值)
语法:类型 属性名() default 默认值;
package com.annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/**** 注解类,接口,枚举*/ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface DBtable {String tableName() default "name"; //缺省值 默认 }
测试其应用属性
package com.annotation; @DBtable public class Test2 {public static void main(String[] args) {DBtable d = Test.class.getAnnotation(DBtable.class);System.out.println(d.tableName());} }
3.value属性 @MyAnnotation(value = "test3") 用value 的好处是可以简写为 @MyAnnotation( "test3")
package com.annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation {String color() default "bule";String value(); }
测试应用其属性
@MyAnnotation("test3") public class Test3 {public static void main(String[] args) {MyAnnotation m = Test3.class.getAnnotation(MyAnnotation.class);System.out.println(m.value());} }
4.高级注解
package com.annotation.enums;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface MetaAnnotation {String value(); }
package com.annotation.enums;/**** 交通信号灯颜色 枚举*/ public enum EumTrafficLamp {RED, //红YELLOW, //黄GREEN, //绿 }
package com.annotation.enums;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation1 {String color() default "blue"; //颜色,缺省属性String value();int[] arrayAttr() default {1,2,3}; ///添加一个int类型数组的属性,缺省属性EumTrafficLamp lamp()default EumTrafficLamp.RED; //添加一个枚举的属性,缺省属性MetaAnnotation annotationAttr() default @MetaAnnotation("xdp"); 为注解添加一个注解类型的属性,并指定注解属性的缺省值 }
测试应用属性如下
package com.annotation.enums;@MyAnnotation1(color = "red",value = "test",arrayAttr = {2},lamp = EumTrafficLamp.RED,annotationAttr=@MetaAnnotation("dps")) public class Test {public static void main(String[] args) {MyAnnotation1 myAnnotation1 = Test.class.getAnnotation(MyAnnotation1.class);System.out.println(myAnnotation1.color() +":"+myAnnotation1.value()+":"+myAnnotation1.arrayAttr().length+":"+myAnnotation1.lamp()+":"+myAnnotation1.annotationAttr().value());} }