运行时类路径中是否需要注解类文件? [英] Are annotation class files needed in the runtime class path?

查看:26
本文介绍了运行时类路径中是否需要注解类文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个类用一个注解进行注解,该注解的定义是否必须在运行时类路径中才能使用该类?例如,给定注释

If a class is annotated with an annotation, does the definition of that annotation have to be in the runtime classpath in order to use the class? For example, given the annotation

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Retention;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Component {}

我可以执行程序吗

@Component
public class Test {
    public static void main(String[] args) {
        System.out.println("It worked!");
    }
}

在类路径中没有 Component.class?(在我的测试中我可以,但这种行为是由规范定义的吗?)

without having Component.class in the classpath? (In my test I could, but is this behavior defined by the spec?)

我问是因为有冲突声明使用库中的注释是否会产生对该库的依赖.

I ask because there are conflicting claims whether using an annotation from a library creates a dependency on that library.

推荐答案

运行时注解是注解处理器在运行时处理的元信息.如果在运行时可以访问注释,那么您肯定会在类路径中添加注释.比如junit肯定需要类路径中的注解来决定测试方法.

Runtime annotations are meta information to be processed by annotation processor at the runtime. If there is an access to annotation at runtime, you definitely add annotations in the classpath. For example junit definitely need the annotations in the class path determine test methods.

如果没有做注解的处理,就没有必要把它放在类路径中.

If there is no processing of annotation is done, there is no need to have it the classpath.

这篇关于运行时类路径中是否需要注解类文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆