@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface A{ String value(); }
@Target:
注解使用范围
取值(ElementType)有:
2. ElementType. FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4. ElementType. METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7. ElementType. TYPE: 用于描述类、接口(包括注解类型) 或enum声明
@Retention:
@Retention定义了该Annotation被保留的时间长短:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为Annotation与class在使用上是被分离的)。使用这个meta-Annotation可以对 Annotation的“生命周期”限制。
作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)
取值(RetentionPoicy)有:
1.SOURCE:在源文件中有效(即源文件保留)
2.CLASS:在class文件中有效(即class保留)
3.RUNTIME:在运行时有效(即运行时保留)
1.自定义注解
package cn.veji.hibernate.po; 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 Privilege { String[] value(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
从上面代码可以看出Target 对应的作用域(Target可以接受多个参数,逗号分隔即可)
例如:
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR}) @Retention(RetentionPolicy.RUNTIME) public @interface MyTestIn { String author() default "kaelthas.wang"; String address() default "山东青岛"; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2.获取类注解属性
/** * 读取注解值 * * @param annotationClasss 处理Annotation类名称 * @param annotationField 处理Annotation类属性名称 * @param className 处理Annotation的使用类名称 * @return * @throws Exception */ @SuppressWarnings("all") public Map<String, String> loadVlaue(Class annotationClasss, String annotationField, String className) throws Exception { System.out.println("处理Annotation类名称 === "+annotationClasss.getName()); System.out.println("处理Annotation类属性名称 === "+annotationField); System.out.println("处理Annotation的调用类名称 === "+className); Map<String, String> map = new HashMap<String, String>(); Method[] methods = Class.forName(className).getDeclaredMethods(); for (Method method : methods) { if (method.isAnnotationPresent(annotationClasss)) { Annotation p = method.getAnnotation(annotationClasss); Method m = p.getClass() .getDeclaredMethod(annotationField, null); //这里根据属性参数类型进行强制类型转换 String[] values = (String[]) m.invoke(p, null); for (String key : values) { System.out.println("注解值 === " + key); map.put(key, key); } } } System.out.println("map数量 === " + map.size()); return map; }
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
3.获取方法注解属性值
这里的属性值为int 强制类型转换时候使用Integer
4.执行结果:@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR}) @Retention(RetentionPolicy.RUNTIME) public @interface MyTestIn { String author() default "kaelthas.wang"; String address() default "山东青岛"; }
package cn.veji.hibernate.po; 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 Privilege { String[] value(); }
@SuppressWarnings("all") public int loadVlaue(Class annotationClasss, String annotationField, String className) { System.out.println("处理Annotation类名称 === " + annotationClasss.getName()); System.out.println("处理Annotation类属性名称 === " + annotationField); System.out.println("处理Annotation的调用类名称 === " + className); Map<String, String> map = new HashMap<String, String>(); try { Method[] methods = Class.forName(className).getDeclaredMethods(); Class test = Class.forName(className); if (test.isAnnotationPresent(annotationClasss)) { Annotation p = test.getAnnotation(annotationClasss); Method m = p.getClass() .getDeclaredMethod(annotationField, null); return (Integer) m.invoke(p, null); } } catch (Exception e) { return -1; } return -1; }
处理Annotation类名称 === cn.veji.hibernate.po.Privilege 处理Annotation类属性名称 === value 处理Annotation的调用类名称 === cn.veji.hibernate.po.TestPrivilege 注解值 === c 注解值 === a 注解值 === b map数量 === 3
参考:http://xuwenjin666.iteye.com/blog/1637247
http://blog.csdn.net/hai_qing_xu_kong/article/details/51779695