@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface A{
String value();
}
@Target:
注解使用范围
取值(ElementType)有:
1.CONSTRUCTOR:用于描述构造器
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();
}
2.定义注解使用类
package cn.veji.hibernate.po;
public class TestPrivilege {
@Privilege( { "a" })
public void a() {
}
@Privilege( { "b" })
public void b() {
}
@Privilege( { "c" })
public void c() {
}
}
3.读取注解值类
package cn.veji.hibernate.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import cn.veji.hibernate.po.Privilege;
import cn.veji.hibernate.po.TestPrivilege;
public class AnnotationUtil {
public static AnnotationUtil anno = null;
public static AnnotationUtil getInstance() {
if (anno == null) {
anno = new AnnotationUtil();
}
return anno;
}
/**
* 读取注解值
*
* @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;
}
public static void main(String[] args) throws Exception {
AnnotationUtil.getInstance().loadVlaue(Privilege.class, "value",
TestPrivilege.class.getName());
}
}
4.执行结果:
处理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