[英]Are annotation class files needed in the runtime class path?
如果 class 被注释了,那么该注释的定义是否必须在运行时类路径中才能使用 class? 例如,给定注释
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? (在我的测试中我可以,但是这种行为是由规范定义的吗?)
我问是因为存在相互矛盾的说法,是否使用库中的注释会创建对该库的依赖。
运行时注解是注解处理器在运行时处理的元信息。 如果在运行时访问注解,你肯定会在类路径中添加注解。 例如junit肯定需要class路径中的注解确定测试方法。
如果没有完成注解处理,就没有必要让它成为类路径。
是的,您可以在类路径中没有 Component.class 的情况下执行程序。 此处有更多详细信息: Why doesn't a missing annotation cause a ClassNotFoundException at runtime?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.