java注解技术学习

认识注解

就是Java代码中的特殊标记,比如说:@Override、@Test等,作用是:让其他程序根据注解信息来决定怎么执行该程序

image-20240317101128791

 public @interface AnnotationTest2 {
     String value();
     int age();
 }


 public @interface MyTest1 {
     String aaa();
     boolean bbb() default true;
     String ccc();
 }


 @AnnotationTest2(value = "111", age = 11)
 public class AnnotationTeat1 {
     @MyTest1(aaa = "111",bbb = false , ccc = "222")
     public void test1(){
 ​
     }
 }

value是一个特殊变量,所以在使用时可以直接赋值,但是有其他变量存在时,就不可以这样赋值,可是当其他变量有默认值时,就可以直接赋值

image-20240317110705329

元注解

指的是:修饰注解的注解

@Target声明被修饰的注解只能在哪些位置使用

image-20240317111036413

 
@Target({ElementType.TYPE,ElementType.FIELD})
 public @interface MyTest1 {
     String aaa();
     boolean bbb() default true;
     String ccc();
 }

@Retention声明注解的保留周期

image-20240317111412918

 @Retention(RetentionPolicy.RUNTIME)

注解的解析

判断类上、方法上、成员变量上是否存在注解,并把注解里的内容解析出来

 import org.junit.Test;
 ​
 import java.lang.reflect.Method;
 ​
 public class AnnotationTest {
     @Test
     public void ParseClass() throws Exception {
         //1.创建Class对象
         Class<Demo> demoClass = Demo.class;
         //2.解析类上面的注解
         if(demoClass.isAnnotationPresent(MyTest.class)){
             MyTest declaredAnnotation = demoClass.getDeclaredAnnotation(MyTest.class);
             System.out.println(declaredAnnotation.value());
             System.out.println(declaredAnnotation.aaa());
             System.out.println(declaredAnnotation.bbb());
         }
 ​
 ​
         //获取方法
         Method test1 = demoClass.getDeclaredMethod("test1");
         //获取解析类的注解
         if(test1.isAnnotationPresent(MyTest.class)){
             MyTest declaredAnnotation = test1.getDeclaredAnnotation(MyTest.class);
             System.out.println(declaredAnnotation.value());
             System.out.println(declaredAnnotation.aaa());
             System.out.println(declaredAnnotation.bbb());
         }
     }
 }


 @Target({ElementType.TYPE , ElementType.METHOD})
 @Retention(RetentionPolicy.RUNTIME)
 public @interface MyTest {
     String value();
     double aaa() default 100;
     String[] bbb();
 }


 public class AnnotationTest {
     @Test
     public void ParseClass() throws Exception {
         //1.创建Class对象
         Class<Demo> demoClass = Demo.class;
         //2.解析类上面的注解
         if(demoClass.isAnnotationPresent(MyTest.class)){
             MyTest declaredAnnotation = demoClass.getDeclaredAnnotation(MyTest.class);
             System.out.println(declaredAnnotation.value());
             System.out.println(declaredAnnotation.aaa());
             System.out.println(declaredAnnotation.bbb());
         }
 ​
 ​
         //获取方法
         Method test1 = demoClass.getDeclaredMethod("test1");
         //获取解析类的注解
         if(test1.isAnnotationPresent(MyTest.class)){
             MyTest declaredAnnotation = test1.getDeclaredAnnotation(MyTest.class);
             System.out.println(declaredAnnotation.value());
             System.out.println(declaredAnnotation.aaa());
             System.out.println(declaredAnnotation.bbb());
         }
     }
 }

  • 先写注解

  • 用Demo类给注解赋值

  • 最后调用方法,将注解解析

 package com.itheima.annotation;
 ​
 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 MyTest1 {
 ​
 }

 package com.itheima.annotation;
 ​
 ​
 import java.lang.reflect.Method;
 ​
 public class AnnotationTest1 {
     @MyTest1
     public void test1(){
         System.out.println(111);
     }
 ​
     public void test2(){
         System.out.println(222);
     }
 ​
     public static void main(String[] args) throws Exception {
         AnnotationTest1 annotationTest1 = new AnnotationTest1();
         //1.创建Class对象
         Class<AnnotationTest1> annotationTest1Class = AnnotationTest1.class;
         //2.提取对象的所有方法
         Method[] declaredMethod = annotationTest1Class.getDeclaredMethods();
         //3.遍历看谁有注解
         for (Method method : declaredMethod){
             if(method.isAnnotationPresent(MyTest1.class)){
                 //出发当前方法执行
                 method.invoke(annotationTest1);
             }
         }
     }
 }

注解的一个作用,对方法做一个标记

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杰哥的狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值