深入理解Java自定义注解(二)-使用自定义注解

蔚落 2022-02-05 04:13 454阅读 0赞

java的注解处理器类主要是AnnotatedElement接口的实现类实现,为位于java.lang.reflect包下。由下面的class源码可知AnnotatedElement接口是所有元素的父接口,这时我们通过反射获得一个类的AnnotatedElement对象后,就可以通过下面表格的几个方法,访问Annotation信息。

public final class Class implements java.io.Serializable, GenericDeclaration, Type, AnnotatedElement {

总结AnnotatedElement的常用方法 输入图片说明

注意:getDeclaredAnnotations和getAnnotations得到的都是当前类上面所有的注解(不包括方法上注解和属性上注解),不同的是,前者不包括继承的。而getAnnotations得到的是包括继承的所有注解。

注解处理器使用示例

新建自定注解@ReqMapping

  1. import java.lang.annotation.ElementType;
  2. import java.lang.annotation.Retention;
  3. import java.lang.annotation.RetentionPolicy;
  4. import java.lang.annotation.Target;
  5. /**
  6. * Description:
  7. * Created by gaowei on 2018/1/3.
  8. */
  9. @Target({ElementType.METHOD,ElementType.TYPE})
  10. @Retention(RetentionPolicy.RUNTIME)
  11. public @interface ReqMapping {
  12. ReqMethod [] method() default {};
  13. String[] val() default "";
  14. }

新建一个枚举类

  1. public enum ReqMethod {
  2. GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
  3. }

新建自定义注解@ReqValue

  1. import java.lang.annotation.ElementType;
  2. import java.lang.annotation.Retention;
  3. import java.lang.annotation.RetentionPolicy;
  4. import java.lang.annotation.Target;
  5. /**
  6. * Description:
  7. * Created by gaowei on 2018/1/4.
  8. */
  9. @Target(ElementType.FIELD)
  10. @Retention(RetentionPolicy.RUNTIME)
  11. public @interface ReqValue {
  12. String value1() default "";
  13. String value2() default "";
  14. }

使用自定义注解

  1. @ReqMapping(method = ReqMethod.POST,val = "类")
  2. public class User {
  3. @ReqValue(value1 = "张三")
  4. private String userName;
  5. @ReqValue(value2 = "密码")
  6. private String pswd;
  7. @ReqMapping(method = ReqMethod.GET)
  8. public void get(){
  9. }
  10. @ReqMapping(method = ReqMethod.POST)
  11. public void post(){
  12. }
  13. @ReqMapping(val={"a","b"})
  14. public void other(){
  15. }
  16. }

注解处理器测试

  1. import java.lang.annotation.Annotation;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. /**
  5. * Description:
  6. * Created by gaowei on 2018/1/2.
  7. */
  8. public class TestAnnotation {
  9. public static void main(String[] args) {
  10. Class<User> clazz = User.class;
  11. //获得clazz(User)里面所有方法信息
  12. Method[] methods = clazz.getDeclaredMethods();
  13. //获得clazz(User)里面所有属性信息
  14. Field[] declaredFields = clazz.getDeclaredFields();
  15. System.out.println("methods注解个数:"+methods.length);
  16. System.out.println("declaredFields注解个数:"+declaredFields.length);
  17. //遍历循环所有方法信息
  18. for (Method method : methods) {
  19. //判断method是否含有指定元素的注解
  20. if (method.isAnnotationPresent(ReqMapping.class)) {
  21. //返回当前方法上的注解对象
  22. ReqMapping reqMapping = method.getAnnotation(ReqMapping.class);
  23. //获得注解的值
  24. System.out.println("方法注解值:"+reqMapping.method());
  25. //如果一个注解有多个值,通过遍历取出(特别注意:reqMapping.val(),这个val()是你定义注解的成员)
  26. String[] values = reqMapping.val();
  27. for (String value : values) {
  28. System.out.println(value);
  29. }
  30. }
  31. }
  32. //获得类里面所有方法的注解信息
  33. for (Field declaredField : declaredFields) {
  34. if(declaredField.isAnnotationPresent(ReqValue.class)){
  35. ReqValue reqValue = declaredField.getAnnotation(ReqValue.class);
  36. System.out.println("属性注解值:"+reqValue.value1());
  37. System.out.println("属性注解值:"+reqValue.value2());
  38. }
  39. }
  40. //获得类上的所有注解
  41. Annotation[] declaredAnnotations = clazz.getDeclaredAnnotations();
  42. for (Annotation declaredAnnotation : declaredAnnotations) {
  43. System.out.println("类注解值:"+ declaredAnnotation);
  44. }
  45. }
  46. }

发表评论

表情:
评论列表 (有 0 条评论,454人围观)

还没有评论,来说两句吧...

相关阅读