MK
摩柯社区 - 一个极简的技术知识社区
AI 面试

Java注解的使用与实践

2021-10-186.4k 阅读

Java注解基础

在Java编程中,注解(Annotation)是一种元数据形式,它为我们在代码中添加额外信息提供了一种便捷方式。这些信息可以在编译期、类加载期或者运行时被读取并使用,极大地增强了代码的灵活性和可维护性。

Java注解本质上是一种接口,它通过 @interface 关键字来定义。例如,我们定义一个简单的注解 MyAnnotation

public @interface MyAnnotation {
}

上述代码定义了一个空的注解 MyAnnotation。虽然它目前没有任何成员,但已经可以应用到Java元素(如类、方法、字段等)上了。

注解的应用场景

  1. 编译时处理:在编译期,注解可以用于生成代码、执行检查等操作。比如,@Override 注解用于标识子类中重写的方法,编译器会检查该方法是否真的重写了父类的方法,如果不是则报错。
class Parent {
    public void print() {
        System.out.println("Parent print");
    }
}

class Child extends Parent {
    @Override
    public void print() {
        System.out.println("Child print");
    }
}
  1. 运行时处理:在运行时,通过反射机制可以读取注解信息,并根据这些信息进行不同的操作。例如,在Spring框架中,大量使用注解来配置Bean、定义切面等。

注解的定义与元素

注解元素的定义

注解可以包含元素(也叫成员变量),这些元素为注解提供了参数化的能力。元素在注解中以无参数方法的形式定义,并且可以指定默认值。例如:

public @interface MyAnnotationWithElement {
    String value() default "default value";
    int count() default 1;
}

在上述注解 MyAnnotationWithElement 中,定义了两个元素:valuecount,分别有默认值 "default value"1

使用包含元素的注解

当使用包含元素的注解时,需要为元素赋值,除非使用默认值。例如:

@MyAnnotationWithElement(value = "custom value", count = 5)
class AnnotatedClass {
}

如果注解只有一个名为 value 的元素,在使用注解时可以省略 value = 部分,直接写值。例如:

public @interface SingleValueAnnotation {
    String value();
}

@SingleValueAnnotation("only value")
class AnotherAnnotatedClass {
}

元注解

元注解是用于注解其他注解的注解。Java提供了几种内置的元注解,它们在定义注解时起到了关键作用。

@Retention

@Retention 元注解用于指定注解的保留策略,即注解在什么阶段存在。它有三个取值:

  1. RetentionPolicy.SOURCE:注解只在源码阶段存在,编译时会被丢弃。例如,一些用于生成代码的临时注解可能只需要在源码阶段存在。
@Retention(RetentionPolicy.SOURCE)
public @interface SourceOnlyAnnotation {
}
  1. RetentionPolicy.CLASS:注解在编译后的字节码中存在,但在运行时JVM不会加载。这是默认的保留策略。大多数情况下,如果我们的注解只是用于编译期检查,不需要在运行时访问,可以使用此策略。
@Retention(RetentionPolicy.CLASS)
public @interface ClassRetentionAnnotation {
}
  1. RetentionPolicy.RUNTIME:注解在运行时仍然存在,通过反射可以读取注解信息。这种策略适用于那些需要在运行时根据注解进行特定操作的场景,如Spring的依赖注入注解。
@Retention(RetentionPolicy.RUNTIME)
public @interface RuntimeRetentionAnnotation {
}

@Target

@Target 元注解用于指定注解可以应用到哪些Java元素上。它的取值包括:

  1. ElementType.TYPE:可以应用到类、接口、枚举等类型上。
@Target(ElementType.TYPE)
public @interface TypeAnnotation {
}
  1. ElementType.METHOD:可以应用到方法上。
@Target(ElementType.METHOD)
public @interface MethodAnnotation {
}
  1. ElementType.FIELD:可以应用到字段(成员变量)上。
@Target(ElementType.FIELD)
public @interface FieldAnnotation {
}

此外,还有 ElementType.CONSTRUCTOR(构造函数)、ElementType.PARAMETER(方法参数)等取值,以满足不同的应用场景。

@Documented

@Documented 元注解表示被它注解的注解会被包含在JavaDoc中。如果我们希望某个注解的信息在生成的文档中体现,就可以使用这个元注解。

@Documented
public @interface DocumentedAnnotation {
}

@Inherited

@Inherited 元注解表示被它注解的注解具有继承性。即如果一个类被 @Inherited 注解的注解所标注,那么它的子类也会自动拥有该注解。不过需要注意的是,这个继承只对类有效,对接口无效。

@Inherited
public @interface InheritedAnnotation {
}

@InheritedAnnotation
class ParentClass {
}

class ChildClass extends ParentClass {
    // ChildClass 自动拥有 InheritedAnnotation
}

注解处理器

在编译期处理注解时,我们需要定义注解处理器。Java提供了 javax.annotation.processing 包来帮助我们实现这一功能。

定义注解处理器

首先,我们定义一个简单的注解 ProcessMe

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

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface ProcessMe {
    String message();
}

然后,定义一个注解处理器 ProcessMeProcessor

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import java.util.Set;

@SupportedAnnotationTypes("ProcessMe")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class ProcessMeProcessor extends AbstractProcessor {
    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        for (TypeElement annotation : annotations) {
            for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
                ProcessMe processMe = element.getAnnotation(ProcessMe.class);
                processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Processing " + element.getSimpleName() + " with message: " + processMe.message());
            }
        }
        return true;
    }
}

在上述代码中,ProcessMeProcessor 继承自 AbstractProcessor,并重写了 process 方法。@SupportedAnnotationTypes 注解指定了该处理器处理的注解类型,@SupportedSourceVersion 注解指定了支持的Java版本。

使用注解处理器

要使用这个注解处理器,我们需要将其打包成一个JAR文件,并在编译时添加到 processorpath 中。例如,在Maven项目中,可以这样配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>com.example</groupId>
                        <artifactId>annotation-processor</artifactId>
                        <version>1.0.0</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

然后,在代码中使用 ProcessMe 注解:

@ProcessMe(message = "This is a test")
class MyClass {
}

当编译 MyClass 时,注解处理器会输出处理信息。

运行时注解的反射处理

在运行时,我们可以通过反射来读取和处理注解信息。这使得我们可以根据注解动态地改变程序的行为。

读取类上的运行时注解

假设我们有一个运行时注解 RuntimeAnnotation

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 RuntimeAnnotation {
    String value();
}

定义一个类 AnnotatedRuntimeClass 并应用该注解:

@RuntimeAnnotation("runtime annotation value")
class AnnotatedRuntimeClass {
}

在运行时,我们可以通过反射读取该注解:

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class RuntimeAnnotationReader {
    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> clazz = Class.forName("AnnotatedRuntimeClass");
        if (clazz.isAnnotationPresent(RuntimeAnnotation.class)) {
            RuntimeAnnotation annotation = clazz.getAnnotation(RuntimeAnnotation.class);
            System.out.println("Annotation value: " + annotation.value());
        }
    }
}

上述代码通过 Class.forName 获取类对象,然后使用 isAnnotationPresent 方法检查类是否被 RuntimeAnnotation 注解,最后通过 getAnnotation 方法获取注解实例并读取其值。

读取方法和字段上的运行时注解

类似地,我们可以读取方法和字段上的运行时注解。定义一个新的注解 MethodAndFieldAnnotation

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, ElementType.FIELD})
public @interface MethodAndFieldAnnotation {
    String description();
}

定义一个类 AnnotatedMethodAndFieldClass

public class AnnotatedMethodAndFieldClass {
    @MethodAndFieldAnnotation(description = "This is a field annotation")
    private String field;

    @MethodAndFieldAnnotation(description = "This is a method annotation")
    public void method() {
    }
}

在运行时读取注解:

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class MethodAndFieldAnnotationReader {
    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException {
        Class<?> clazz = AnnotatedMethodAndFieldClass.class;
        Field field = clazz.getDeclaredField("field");
        if (field.isAnnotationPresent(MethodAndFieldAnnotation.class)) {
            MethodAndFieldAnnotation annotation = field.getAnnotation(MethodAndFieldAnnotation.class);
            System.out.println("Field annotation description: " + annotation.description());
        }

        Method method = clazz.getDeclaredMethod("method");
        if (method.isAnnotationPresent(MethodAndFieldAnnotation.class)) {
            MethodAndFieldAnnotation annotation = method.getAnnotation(MethodAndFieldAnnotation.class);
            System.out.println("Method annotation description: " + annotation.description());
        }
    }
}

上述代码分别获取类的字段和方法,并检查是否被 MethodAndFieldAnnotation 注解,然后读取注解的描述信息。

自定义注解在框架中的应用示例

以一个简单的依赖注入框架为例,我们可以使用自定义注解来实现对象的自动注入。

定义注解

首先,定义 Inject 注解用于标记需要注入的字段:

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 Inject {
}

定义 Component 注解用于标记可注入的组件:

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 Component {
}

实现依赖注入逻辑

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class DependencyInjector {
    private static final Map<Class<?>, Object> componentMap = new HashMap<>();

    public static void registerComponent(Object component) {
        Class<?> clazz = component.getClass();
        if (clazz.isAnnotationPresent(Component.class)) {
            componentMap.put(clazz, component);
        }
    }

    public static void injectDependencies(Object target) throws IllegalAccessException {
        Class<?> clazz = target.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            if (field.isAnnotationPresent(Inject.class)) {
                Class<?> fieldType = field.getType();
                Object dependency = componentMap.get(fieldType);
                if (dependency != null) {
                    field.setAccessible(true);
                    field.set(target, dependency);
                }
            }
        }
    }
}

使用自定义注解实现依赖注入

@Component
class MessageService {
    public String getMessage() {
        return "Hello, World!";
    }
}

class MessagePrinter {
    @Inject
    private MessageService messageService;

    public void printMessage() {
        System.out.println(messageService.getMessage());
    }
}

public class Main {
    public static void main(String[] args) throws IllegalAccessException {
        MessageService messageService = new MessageService();
        DependencyInjector.registerComponent(messageService);

        MessagePrinter messagePrinter = new MessagePrinter();
        DependencyInjector.injectDependencies(messagePrinter);

        messagePrinter.printMessage();
    }
}

在上述代码中,MessageService 被标记为 Component 并注册到 DependencyInjector 中,MessagePrintermessageService 字段被标记为 Inject,通过 DependencyInjector.injectDependencies 方法实现了依赖注入,最终 messagePrinter 能够正确打印出消息。

通过以上内容,我们深入了解了Java注解的定义、使用、元注解、注解处理器以及在运行时的反射处理,并且通过实际示例展示了自定义注解在框架中的应用。注解作为Java编程的重要特性,在提高代码的可维护性、灵活性和可读性方面发挥着巨大作用,在实际项目开发中应该合理利用注解来优化我们的代码结构和功能实现。