Java Annotation学习笔记

本文深入介绍了Java中的注解(Annotation),包括其定义、用途及如何应用于类、方法、字段和参数的不同场景。通过实例演示了如何自定义注解,并利用反射获取注解信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Annotation是什么

    Annotation是类,方法或字段的一种注解或元数据,当程序被JVM编译的时候,annotation会和类编译在一起,作为一种元数据去描述被修饰的数据。

2. Annotation的定义    

package annotationApplication;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotation {
    public String name();

    public int age();
}
其中,Retention中的字段表明,该注解能在运行时通过反射得到。Target表明该注解用于何处。此处为类或者接口处可以使用。


3. Annotation的使用(类、接口范围)

annotation的定义

package annotationDefinition;

import annotationApplication.MyAnnotation;

@MyAnnotation(name = "Mikel Arteta", age = 29)
public class MyAnnotationUsage {
}


annotation的使用

package annotationDefinition;

import annotationApplication.MyAnnotation;

@MyAnnotation(name = "Mikel Arteta", age = 29)
public class MyAnnotationUsage {
}


下面是我定义的使用类注解的类的查找器:找到并输出注解类的annotation的value

package annotationDefinition;

import annotationApplication.MyAnnotation;

import java.lang.annotation.Annotation;

public class MyAnnotationFinder {
    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> myAnnotationUsage = Class.forName("annotationDefinition.MyAnnotationUsage");
        Annotation[] annotations = myAnnotationUsage.getAnnotations();
        for(Annotation annotation : annotations) {
            MyAnnotation annotation1 = (MyAnnotation)annotation;
            System.out.println(annotation1.name());
            System.out.println(annotation1.age());
        }
    }
}

运行结果:

annotation's name value -- > Mikel Arteta
annotation's age value -- > 29

4. Annotation的使用(方法范围)

annotation的定义

package annotationApplication;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodAnnotation {
    public String methodName();
    public String methodReturnType();
}


annotation的使用

package annotationDefinition;

import annotationApplication.MethodAnnotation;

public class MethodAnnotationUsage {

    @MethodAnnotation(methodName = "testMethod", methodReturnType = "void")
    public void testMethod(){
    }
}

下面是我定义的使用类注解的类的查找器:找到并输出注解类的annotation的value

package annotationDefinition;

import annotationApplication.MethodAnnotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class MethodAnnotationFinder {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        Class<?> clazz = Class.forName("annotationDefinition.MethodAnnotationUsage");
        Method method = clazz.getDeclaredMethod("testMethod");
        Annotation annotation = method.getAnnotation(MethodAnnotation.class);
        MethodAnnotation annotation1 = (MethodAnnotation) annotation;
        System.out.println("annotationed method name -- > " + annotation1.methodName());
        System.out.println("annotationed method's return type -- > " + annotation1.methodReturnType());
    }
}

运行结果:

annotationed method name -- > testMethod
annotationed method's return type -- > void

5. Annotation的使用(field范围)

annotation的定义

package annotationApplication;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FieldAnnotation {
    public String fieldType();
    public String fieldModifier();
}

annotation的使用

package annotationDefinition;

import annotationApplication.FieldAnnotation;

public class FieldAnnotationUsage {
    @FieldAnnotation(fieldType = "int", fieldModifier = "public")
    public double i = 0.01;

    @FieldAnnotation(fieldType = "String", fieldModifier = "private")
    private String testString;
}

下面是我定义的使用类注解的类的查找器:找到并输出注解类的annotation的value

package annotationDefinition;

import annotationApplication.FieldAnnotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class FieldAnnotationFinder {
    public static void main(String args[]) {
        Class<?> clazz = FieldAnnotationUsage.class;
        Field[] fields = clazz.getDeclaredFields();
        Annotation[] annotation = null;

        for (int i = 0; i < fields.length; i++) {
            annotation = fields[i].getDeclaredAnnotations();
            for (int j = 0; j < annotation.length; j++) {
                FieldAnnotation fieldAnnotation = (FieldAnnotation) annotation[j];
                System.out.println(" field's annotation type value -- > " + fieldAnnotation.fieldType());
                System.out.println(" field's annotation modifier value -- > " + fieldAnnotation.fieldModifier());
            }
            System.out.println(" annotationed field's name -- > " + fields[i].getName());
        }
    }
}

运行结果:

 field's annotation type value -- > int
 field's annotation modifier value -- > public
 annotationed field's name -- > i
 field's annotation type value -- > String
 field's annotation modifier value -- > private
 annotationed field's name -- > testString

5. Annotation的使用(Parameter范围)

annotation的范围

package annotationApplication;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface ParameterAnnotation {
    public String parameterName();
    public String parameterType();
}

annotation的使用

package annotationDefinition;

import annotationApplication.ParameterAnnotation;

public class ParameterAnnotationUsage {
    public void testMethod(@ParameterAnnotation(parameterName = "pname", parameterType = "int") int value) {
        value = 10;
    }
}


下面是我定义的使用类注解的类的查找器:找到并输出注解类的annotation的value

package annotationDefinition;

import annotationApplication.ParameterAnnotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class ParameterAnnotationFinder {

    public void parameterAnnotationFinder() throws NoSuchMethodException {
        Class<?> clazz = ParameterAnnotationUsage.class;
        Method method = clazz.getMethod("testMethod", int.class);
        Annotation[][] annotations = method.getParameterAnnotations();
        //Method.getParameterAnnotations() method returns a two-dimensional Annotation array,
        // containing an array of annotations for each method parameter.
        Class<?>[] parameterTyeps = method.getParameterTypes();

        int i = 0;
        for (Annotation[] annotation : annotations) {
            Class<?> parameterTyep = parameterTyeps[i++];
            for (Annotation annotation1 : annotation) {
                ParameterAnnotation parameterAnnotation1 = (ParameterAnnotation) annotation1;
                System.out.println("annotation paraName : " + parameterAnnotation1.parameterName());
                System.out.println("annotation paraType : " + parameterAnnotation1.parameterType());
                System.out.println("parameter Type : " + parameterTyep);
            }
        }
    }

    public static void main(String[] args) throws NoSuchMethodException {
        new ParameterAnnotationFinder().parameterAnnotationFinder();
    }
}

运行结果:

annotation paraName : pname
annotation paraType : int
parameter Type : int




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值